Skip to content

Commit 16a61b7

Browse files
tomblindPerryvw
authored andcommitted
Fixed merging module into interface (#522)
1 parent 29047b5 commit 16a61b7

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/LuaTransformer.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,10 +1300,15 @@ export class LuaTransformer {
13001300

13011301
const symbol = this.checker.getSymbolAtLocation(statement.name);
13021302
const hasExports = symbol !== undefined && this.checker.getExportsOfModule(symbol).length > 0;
1303-
const isFirstDeclaration = symbol !== undefined
1304-
&& symbol.declarations[0] === statement
1305-
// TS allows an empty namespace before a class of the same name
1306-
&& symbol.declarations.findIndex(d => ts.isClassLike(d)) === -1;
1303+
1304+
// This is NOT the first declaration if:
1305+
// - declared as a module before this (ignore interfaces with same name)
1306+
// - declared as a class or function at all (TS requires these to be before module, unless module is empty)
1307+
const isFirstDeclaration =
1308+
symbol === undefined
1309+
|| (symbol.declarations.findIndex(d => ts.isClassLike(d) || ts.isFunctionDeclaration(d)) === -1
1310+
&& statement === symbol.declarations.find(ts.isModuleDeclaration));
1311+
13071312
if (isFirstDeclaration) {
13081313
const isExported = (ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export) !== 0;
13091314
if (isExported && this.currentNamespace) {

test/unit/modules.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ test("Access this in module", () => {
6363
const code = `return M.bar();`;
6464
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foobar");
6565
});
66+
67+
test("Module merged with interface", () => {
68+
const header = `
69+
interface Foo {}
70+
module Foo {
71+
export function bar() { return "foobar"; }
72+
}`;
73+
const code = `return Foo.bar();`;
74+
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foobar");
75+
});

0 commit comments

Comments
 (0)