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
43 changes: 32 additions & 11 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,13 @@ export class LuaTransformer {

const symbol = this.checker.getSymbolAtLocation(statement.name);
const hasExports = symbol !== undefined && this.checker.getExportsOfModule(symbol).length > 0;
const nameIdentifier = this.transformIdentifier(statement.name as ts.Identifier);
const exportScope = this.getIdentifierExportScope(nameIdentifier);

// Non-module namespace could be merged if:
// - is top level
// - is nested and exported
const isNonModuleMergeable = !this.isModule && (!this.currentNamespace || exportScope);

// This is NOT the first declaration if:
// - declared as a module before this (ignore interfaces with same name)
Expand All @@ -1594,27 +1601,41 @@ export class LuaTransformer {
(symbol.declarations.findIndex(d => ts.isClassLike(d) || ts.isFunctionDeclaration(d)) === -1 &&
statement === symbol.declarations.find(ts.isModuleDeclaration));

const nameIdentifier = this.transformIdentifier(statement.name as ts.Identifier);
if (isNonModuleMergeable) {
// 'local NS = NS or {}' or 'exportTable.NS = exportTable.NS or {}'
const localDeclaration = this.createLocalOrExportedOrGlobalDeclaration(
nameIdentifier,
tstl.createBinaryExpression(
this.addExportToIdentifier(nameIdentifier),
tstl.createTableExpression(),
tstl.SyntaxKind.OrOperator
)
);

if (isFirstDeclaration) {
result.push(...localDeclaration);
} else if (isFirstDeclaration) {
// local NS = {} or exportTable.NS = {}
const localDeclaration = this.createLocalOrExportedOrGlobalDeclaration(
nameIdentifier,
tstl.createTableExpression()
);

result.push(...localDeclaration);
}

const exportScope = this.getIdentifierExportScope(nameIdentifier);
if (exportScope && hasExports && tsHelper.moduleHasEmittedBody(statement)) {
// local NS = exportTable.NS
const localDeclaration = this.createHoistableVariableDeclarationStatement(
this.createModuleLocalNameIdentifier(statement),
this.createExportedIdentifier(nameIdentifier, exportScope)
);
if (
(isNonModuleMergeable || isFirstDeclaration) &&
exportScope &&
hasExports &&
tsHelper.moduleHasEmittedBody(statement)
) {
// local NS = exportTable.NS
const localDeclaration = this.createHoistableVariableDeclarationStatement(
this.createModuleLocalNameIdentifier(statement),
this.createExportedIdentifier(nameIdentifier, exportScope)
);

result.push(localDeclaration);
}
result.push(localDeclaration);
}

// Set current namespace for nested NS
Expand Down
7 changes: 4 additions & 3 deletions test/translation/__snapshots__/transformation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ end
return ____exports"
`;

exports[`Transformation (modulesNamespaceNoExport) 1`] = `"TestSpace = {}"`;
exports[`Transformation (modulesNamespaceNoExport) 1`] = `"TestSpace = TestSpace or {}"`;

exports[`Transformation (modulesNamespaceWithMemberExport) 1`] = `
"local ____exports = {}
Expand Down Expand Up @@ -503,7 +503,7 @@ return ____exports"
exports[`Transformation (modulesVariableNoExport) 1`] = `"local foo = \\"bar\\""`;

exports[`Transformation (namespace) 1`] = `
"myNamespace = {}
"myNamespace = myNamespace or {}
do
local function nsMember(self)
end
Expand Down Expand Up @@ -537,6 +537,7 @@ function MergedClass.prototype.methodB(self)
self:methodA()
self:propertyFunc()
end
MergedClass = MergedClass or {}
do
function MergedClass.namespaceFunc(self)
end
Expand All @@ -549,7 +550,7 @@ MergedClass:namespaceFunc()"
`;

exports[`Transformation (namespaceNested) 1`] = `
"myNamespace = {}
"myNamespace = myNamespace or {}
do
local myNestedNamespace = {}
do
Expand Down
20 changes: 20 additions & 0 deletions test/unit/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,23 @@ test("Module merged with interface", () => {
const code = `return Foo.bar();`;
expect(util.transpileAndExecute(code, undefined, undefined, header)).toBe("foobar");
});

test("module merged across files", () => {
const testA = `
namespace NS {
export namespace Inner {
export const foo = "foo";
}
}
`;
const testB = `
namespace NS {
export namespace Inner {
export const bar = "bar";
}
}
`;
const { transpiledFiles } = util.transpileStringsAsProject({ "testA.ts": testA, "testB.ts": testB });
const lua = transpiledFiles.map(f => f.lua).join("\n") + "\nreturn NS.Inner.foo .. NS.Inner.bar";
expect(util.executeLua(lua)).toBe("foobar");
});
17 changes: 12 additions & 5 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export function transpileString(
return file.lua.trim();
}

export function transpileStringResult(
input: string | Record<string, string>,
export function transpileStringsAsProject(
input: Record<string, string>,
options: tstl.CompilerOptions = {}
): Required<tstl.TranspileStringResult> {
): tstl.TranspileResult {
const optionsWithDefaults = {
luaTarget: tstl.LuaTarget.Lua53,
noHeader: true,
Expand All @@ -34,9 +34,16 @@ export function transpileStringResult(
...options,
};

const { diagnostics, transpiledFiles } = tstl.transpileVirtualProject(
return tstl.transpileVirtualProject(input, optionsWithDefaults);
}

export function transpileStringResult(
input: string | Record<string, string>,
options: tstl.CompilerOptions = {}
): Required<tstl.TranspileStringResult> {
const { diagnostics, transpiledFiles } = transpileStringsAsProject(
typeof input === "string" ? { "main.ts": input } : input,
optionsWithDefaults
options
);

const file = transpiledFiles.find(({ fileName }) => /\bmain\.[a-z]+$/.test(fileName));
Expand Down