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
13 changes: 9 additions & 4 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,10 +1300,15 @@ export class LuaTransformer {

const symbol = this.checker.getSymbolAtLocation(statement.name);
const hasExports = symbol !== undefined && this.checker.getExportsOfModule(symbol).length > 0;
const isFirstDeclaration = symbol !== undefined
&& symbol.declarations[0] === statement
// TS allows an empty namespace before a class of the same name
&& symbol.declarations.findIndex(d => ts.isClassLike(d)) === -1;

// This is NOT the first declaration if:
// - declared as a module before this (ignore interfaces with same name)
// - declared as a class or function at all (TS requires these to be before module, unless module is empty)
const isFirstDeclaration =
symbol === undefined
|| (symbol.declarations.findIndex(d => ts.isClassLike(d) || ts.isFunctionDeclaration(d)) === -1
&& statement === symbol.declarations.find(ts.isModuleDeclaration));

if (isFirstDeclaration) {
const isExported = (ts.getCombinedModifierFlags(statement) & ts.ModifierFlags.Export) !== 0;
if (isExported && this.currentNamespace) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ test("Access this in module", () => {
const code = `return M.bar();`;
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foobar");
});

test("Module merged with interface", () => {
const header = `
interface Foo {}
module Foo {
export function bar() { return "foobar"; }
}`;
const code = `return Foo.bar();`;
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foobar");
});