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
2 changes: 1 addition & 1 deletion src/transformation/visitors/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const transformModuleDeclaration: FunctionVisitor<ts.ModuleDeclaration> =
const isFirstDeclaration =
symbol === undefined ||
(!symbol.declarations.some(d => ts.isClassLike(d) || ts.isFunctionDeclaration(d)) &&
node === symbol.declarations.find(ts.isModuleDeclaration));
ts.getOriginalNode(node) === symbol.declarations.find(ts.isModuleDeclaration));

if (isNonModuleMergeable) {
// 'local NS = NS or {}' or 'exportTable.NS = exportTable.NS or {}'
Expand Down
20 changes: 20 additions & 0 deletions test/transpile/plugins/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ test("statement comments", () => {
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "add-comments.ts") }] })
.expectLuaToMatchSnapshot();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1013
test.each(["namespace", "module"])("%s with TS transformer plugin", moduleOrNamespace => {
util.testModule`
import { ns } from "module";
export const result = ns.returnsBool();
`
.addExtraFile(
"module.ts",
`
export ${moduleOrNamespace} ns {
export function returnsBool() {
return false;
}
}
`
)
.setOptions({ plugins: [{ transform: path.join(__dirname, "transformer-plugin.ts") }] })
.expectNoExecutionError();
});
15 changes: 15 additions & 0 deletions test/transpile/plugins/transformer-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* This is a TS tranformer plugin that replaces any return statement to 'return true'.
*/
import * as ts from "typescript";

const replaceNode = (node: ts.Node) => {
if (ts.isReturnStatement(node)) {
return ts.factory.createReturnStatement(ts.factory.createTrue());
}
};
const createTransformer = () => (context: ts.TransformationContext) => {
const visit = (node: ts.Node): ts.Node => replaceNode(node) ?? ts.visitEachChild(node, visit, context);
return (file: ts.SourceFile) => ts.visitNode(file, visit);
};
exports.default = createTransformer;