Skip to content

Commit ab6099c

Browse files
committed
Add test for plugin
1 parent 551f04d commit ab6099c

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/transpilation/plugins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface Plugin {
5656
result: EmitFile[]
5757
) => ts.Diagnostic[] | void;
5858

59+
/**
60+
* This function is called when trying to resolve the .lua file corresponding to a Lua require statement. Allows you to provide
61+
* your own module resolution logic. If return value is undefined, regular module resolution is done.
62+
*/
5963
moduleResolution?: (
6064
moduleIdentifier: string,
6165
requiringFile: string,

src/transpilation/transpiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export class Transpiler {
9292

9393
for (const plugin of plugins) {
9494
if (plugin.afterEmit) {
95-
const beforeEmitPluginDiagnostics = plugin.afterEmit(program, options, this.emitHost, emitPlan) ?? [];
96-
diagnostics.push(...beforeEmitPluginDiagnostics);
95+
const afterEmitPluginDiagnostics = plugin.afterEmit(program, options, this.emitHost, emitPlan) ?? [];
96+
diagnostics.push(...afterEmitPluginDiagnostics);
9797
}
9898
}
9999

test/transpile/plugins/afterEmit.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ const plugin: tstl.Plugin = {
66
void program;
77
void options;
88
void emitHost;
9+
void result;
910

10-
for (const file of result) {
11-
console.log(`File was written to disk: ${file.outputPath}`);
12-
}
11+
const diagnostic = {
12+
category: ts.DiagnosticCategory.Message,
13+
messageText: "After emit diagnostic message!",
14+
code: 1234,
15+
file: program.getSourceFiles()[0],
16+
start: undefined,
17+
length: undefined,
18+
} satisfies ts.Diagnostic;
19+
return [diagnostic];
1320
},
1421
};
1522

test/transpile/plugins/plugins.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22
import * as util from "../../util";
3+
import * as ts from "typescript";
34

45
test("printer", () => {
56
util.testModule``
@@ -160,3 +161,24 @@ test("beforeEmit plugin bundle", () => {
160161
expect(f.lua).toContain("-- Comment added by beforeEmit plugin");
161162
}
162163
});
164+
165+
test("afterEmit plugin", () => {
166+
const { diagnostics } = util.testModule`
167+
console.log("Hello, World!");
168+
[].push(1,2,3); // Use lualib code
169+
`
170+
.addExtraFile(
171+
"extrafile.ts",
172+
`
173+
console.log("Hello, Mars!");
174+
`
175+
)
176+
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "afterEmit.ts") }] })
177+
.getLuaResult();
178+
179+
// Expect to see the diagnostic returned by the plugin in the output
180+
const diagnostic = diagnostics.find(d => d.code === 1234);
181+
expect(diagnostic).toBeDefined();
182+
expect(diagnostic?.category).toBe(ts.DiagnosticCategory.Message);
183+
expect(diagnostic?.messageText).toContain("After emit");
184+
});

0 commit comments

Comments
 (0)