Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ 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);
}

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++;
Expand All @@ -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);
}
Expand Down
14 changes: 13 additions & 1 deletion test/integration/lua/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,

Expand Down Expand Up @@ -127,12 +132,19 @@ export class LuaModuleTests {
return exports`
)
@Test("modules")
public modules<T>(inp: string, expected: string) {
public modules(inp: string, expected: string) {
// Transpile
let lua = util.transpileString(inp, util.dummyTypes.Object);

// Assert
// 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!");
}
}