Skip to content

Commit 11a2c11

Browse files
authored
Merge pull request #44 from Perryvw/import-all-from-module
Added namespace imports
2 parents 793211e + 7c31cc9 commit 11a2c11

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Transpiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ export class LuaTranspiler {
168168

169169
transpileImport(node: ts.ImportDeclaration): string {
170170
const importPath = this.transpileExpression(node.moduleSpecifier);
171+
let importPathWithoutQuotes = importPath.replace(new RegExp("\"", "g"), "");
172+
171173
if (!node.importClause || !node.importClause.namedBindings) {
172174
throw new TranspileError("Default Imports are not supported, please use named imports instead!", node);
173175
}
174176

175177
const imports = node.importClause.namedBindings;
176178

177179
if (ts.isNamedImports(imports)) {
178-
let importPathWithoutQuotes = importPath.replace(new RegExp("\"", "g"), "");
179180
let fileImportTable = path.basename(importPathWithoutQuotes) + this.importCount
180181
let result = `local ${fileImportTable} = require(${this.getImportPath(importPathWithoutQuotes)})\n`
181182
this.importCount++;
@@ -187,6 +188,8 @@ export class LuaTranspiler {
187188
}
188189
});
189190
return result;
191+
} else if (ts.isNamespaceImport(imports)) {
192+
return `local ${imports.name.escapedText} = require(${this.getImportPath(importPathWithoutQuotes)})\n`;
190193
} else {
191194
throw new TranspileError("Unsupported import type.", node);
192195
}

test/integration/lua/modules.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export class LuaModuleTests {
4040
`local test0 = require("test")
4141
local TestClass = test0.TestClass`
4242
)
43+
@TestCase(
44+
`import * as Test from "test"`,
45+
46+
`local Test = require("test")`
47+
)
4348
@TestCase(
4449
`import {TestClass as RenamedClass} from "test"`,
4550

@@ -127,12 +132,19 @@ export class LuaModuleTests {
127132
return exports`
128133
)
129134
@Test("modules")
130-
public modules<T>(inp: string, expected: string) {
135+
public modules(inp: string, expected: string) {
131136
// Transpile
132137
let lua = util.transpileString(inp, util.dummyTypes.Object);
133138

134139
// Assert
135140
// Dont test for correct indention this allows easier test case definition
136141
Expect(dedent(lua)).toBe(dedent(expected));
137142
}
143+
144+
@Test("defaultImport")
145+
public defaultImport() {
146+
Expect(() => {
147+
let lua = util.transpileString(`import TestClass from "test"`, util.dummyTypes.Object);
148+
}).toThrowError(Error, "Default Imports are not supported, please use named imports instead!");
149+
}
138150
}

0 commit comments

Comments
 (0)