diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 2a223d84c..c19ee5cb2 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -168,6 +168,8 @@ export class LuaTranspiler { transpileImport(node: ts.ImportDeclaration): string { const importPath = this.transpileExpression(node.moduleSpecifier); + let importPathWithoutQuotes = importPath.replace(new RegExp("\"", "g"), ""); + if (!node.importClause || !node.importClause.namedBindings) { throw new TranspileError("Default Imports are not supported, please use named imports instead!", node); } @@ -175,7 +177,6 @@ export class LuaTranspiler { const imports = node.importClause.namedBindings; if (ts.isNamedImports(imports)) { - let importPathWithoutQuotes = importPath.replace(new RegExp("\"", "g"), ""); let fileImportTable = path.basename(importPathWithoutQuotes) + this.importCount let result = `local ${fileImportTable} = require(${this.getImportPath(importPathWithoutQuotes)})\n` this.importCount++; @@ -187,6 +188,8 @@ export class LuaTranspiler { } }); return result; + } else if (ts.isNamespaceImport(imports)) { + return `local ${imports.name.escapedText} = require(${this.getImportPath(importPathWithoutQuotes)})\n`; } else { throw new TranspileError("Unsupported import type.", node); } diff --git a/test/integration/lua/modules.spec.ts b/test/integration/lua/modules.spec.ts index c54f39b02..bcf2eb23b 100644 --- a/test/integration/lua/modules.spec.ts +++ b/test/integration/lua/modules.spec.ts @@ -40,6 +40,11 @@ export class LuaModuleTests { `local test0 = require("test") local TestClass = test0.TestClass` ) + @TestCase( + `import * as Test from "test"`, + + `local Test = require("test")` + ) @TestCase( `import {TestClass as RenamedClass} from "test"`, @@ -127,7 +132,7 @@ export class LuaModuleTests { return exports` ) @Test("modules") - public modules(inp: string, expected: string) { + public modules(inp: string, expected: string) { // Transpile let lua = util.transpileString(inp, util.dummyTypes.Object); @@ -135,4 +140,11 @@ export class LuaModuleTests { // Dont test for correct indention this allows easier test case definition Expect(dedent(lua)).toBe(dedent(expected)); } + + @Test("defaultImport") + public defaultImport() { + Expect(() => { + let lua = util.transpileString(`import TestClass from "test"`, util.dummyTypes.Object); + }).toThrowError(Error, "Default Imports are not supported, please use named imports instead!"); + } }