Skip to content

Commit d3cb08a

Browse files
committed
Added tests for new plugin hooks
1 parent 0f3721c commit d3cb08a

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { LuaLibFeature } from "./LuaLib";
88
export * from "./LuaPrinter";
99
export * from "./transformation/context";
1010
export * from "./transpilation";
11+
export { ProcessedFile } from "./transpilation/utils";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
afterPrint(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.ProcessedFile[]) {
6+
void(program);
7+
void(options);
8+
void(emitHost);
9+
10+
for (const file of result) {
11+
file.code = "-- Commented added by afterPrint plugin\n" + file.code;
12+
}
13+
}
14+
};
15+
16+
// eslint-disable-next-line import/no-default-export
17+
export default plugin;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from "typescript";
2+
import * as tstl from "../../../src";
3+
4+
const plugin: tstl.Plugin = {
5+
beforeTransform(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost) {
6+
void(program);
7+
void(emitHost);
8+
9+
// Modify settings
10+
options.outDir = "plugin/beforeTransform/outdir";
11+
}
12+
};
13+
14+
// eslint-disable-next-line import/no-default-export
15+
export default plugin;

test/transpile/plugins/plugins.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,40 @@ test.each(["namespace", "module"])("%s with TS transformer plugin", moduleOrName
7878
return false;
7979
}
8080
}
81-
`
81+
`
8282
)
8383
.setOptions({ plugins: [{ transform: path.join(__dirname, "transformer-plugin.ts") }] })
8484
.expectNoExecutionError();
8585
});
86+
87+
test("beforeTransform plugin", () => {
88+
const { transpiledFiles } = util.testModule`
89+
console.log("Hello, World!");
90+
`
91+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "beforeTransform.ts") }] })
92+
.getLuaResult();
93+
94+
expect(transpiledFiles).toHaveLength(1);
95+
// Expect emitted to output path set by the plugin
96+
expect(transpiledFiles[0].outPath).toContain(path.join("plugin", "beforeTransform", "outdir"));
97+
});
98+
99+
test("afterPrint plugin", () => {
100+
const { transpiledFiles } = util.testModule`
101+
console.log("Hello, World!");
102+
`
103+
.addExtraFile(
104+
"extrafile.ts",
105+
`
106+
console.log("Hello, Mars!");
107+
`
108+
)
109+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "afterPrint.ts") }] })
110+
.getLuaResult();
111+
112+
expect(transpiledFiles).toHaveLength(2);
113+
for (const f of transpiledFiles) {
114+
// Expect plugin inserted extra lua at start of file
115+
expect(f.lua).toContain("-- Commented added by afterPrint plugin");
116+
}
117+
});

0 commit comments

Comments
 (0)