-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathplugins.spec.ts
More file actions
243 lines (219 loc) · 7.86 KB
/
plugins.spec.ts
File metadata and controls
243 lines (219 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import * as path from "path";
import * as util from "../../util";
import { Plugin } from "../../../src/transpilation/plugins";
import * as ts from "typescript";
test("printer", () => {
util.testModule``
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
.tap(builder => expect(builder.getMainLuaCodeChunk()).toMatch("-- Custom printer plugin:"));
});
test("printer in bundle", () => {
const { transpiledFiles } = util.testBundle`
import "./otherfile";
const foo = "foo";
`
.addExtraFile(
"otherfile.ts",
`
const bar = "bar";
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "printer.ts") }] })
.expectToHaveNoDiagnostics()
.getLuaResult();
expect(transpiledFiles).toHaveLength(1);
const lua = transpiledFiles[0].lua!;
expect(lua).toContain("-- Custom printer plugin: main.lua");
expect(lua).toContain("-- Custom printer plugin: otherfile.lua");
});
test("visitor", () => {
util.testFunction`
return false;
`
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "visitor.ts") }] })
.expectToEqual(true);
});
test("visitor using super", () => {
util.testFunction`
return "foo";
`
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "visitor-super.ts") }] })
.expectToEqual("bar");
});
test("passing arguments", () => {
util.testFunction`
return {};
`
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "arguments.ts"), option: true }] })
.expectToEqual({ name: path.join(__dirname, "arguments.ts"), option: true });
});
test("statement comments", () => {
util.testFunction`
let foo = "bar";
foo = "baz";
while (true) {}
`
.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();
});
test("beforeTransform plugin", () => {
const { transpiledFiles } = util.testModule`
console.log("Hello, World!");
`
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "beforeTransform.ts") }] })
.getLuaResult();
expect(transpiledFiles).toHaveLength(1);
// Expect emitted to output path set by the plugin
expect(transpiledFiles[0].outPath).toContain(path.join("plugin", "beforeTransform", "outdir"));
});
test("afterPrint plugin", () => {
const { transpiledFiles } = util.testModule`
console.log("Hello, World!");
`
.addExtraFile(
"extrafile.ts",
`
console.log("Hello, Mars!");
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "afterPrint.ts") }] })
.getLuaResult();
expect(transpiledFiles).toHaveLength(2);
for (const f of transpiledFiles) {
// Expect plugin inserted extra lua at start of file
expect(f.lua).toContain("-- Comment added by afterPrint plugin");
}
});
test("beforeEmit plugin", () => {
const { transpiledFiles } = util.testModule`
console.log("Hello, World!");
[].push(1,2,3); // Use lualib code
`
.addExtraFile(
"extrafile.ts",
`
console.log("Hello, Mars!");
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "beforeEmit.ts") }] })
.getLuaResult();
// 2 input files + 1 lualib_bundle
expect(transpiledFiles).toHaveLength(3);
expect(transpiledFiles.find(f => f.outPath.endsWith("lualib_bundle.lua"))).toBeDefined();
for (const f of transpiledFiles) {
// Expect plugin inserted extra lua at start of all files including lualib bundle
expect(f.lua).toContain("-- Comment added by beforeEmit plugin");
}
});
test("beforeEmit plugin bundle", () => {
const { transpiledFiles } = util.testBundle`
console.log("Hello, World!");
[].push(1,2,3); // Use lualib code
`
.addExtraFile(
"extrafile.ts",
`
console.log("Hello, Mars!");
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "beforeEmit.ts") }] })
.getLuaResult();
// 1 lua bundle output
expect(transpiledFiles).toHaveLength(1);
for (const f of transpiledFiles) {
// Expect bundle to be affected by plugin
expect(f.lua).toContain("-- Comment added by beforeEmit plugin");
}
});
test("afterEmit plugin", () => {
const { diagnostics } = util.testModule`
console.log("Hello, World!");
[].push(1,2,3); // Use lualib code
`
.addExtraFile(
"extrafile.ts",
`
console.log("Hello, Mars!");
`
)
.setOptions({ luaPlugins: [{ name: path.join(__dirname, "afterEmit.ts") }] })
.getLuaResult();
// Expect to see the diagnostic returned by the plugin in the output
const diagnostic = diagnostics.find(d => d.code === 1234);
expect(diagnostic).toBeDefined();
expect(diagnostic?.category).toBe(ts.DiagnosticCategory.Message);
expect(diagnostic?.messageText).toContain("After emit");
});
test("in memory plugin", () => {
const { diagnostics } = util.testModule``
.setOptions({
luaPlugins: [
{
plugin: {
afterEmit(program: ts.Program) {
return [
{
category: ts.DiagnosticCategory.Message,
messageText: "In memory plugin diagnostic message!",
code: 1234,
file: program.getSourceFiles()[0],
start: undefined,
length: undefined,
} satisfies ts.Diagnostic,
];
},
} satisfies Plugin,
},
],
})
.getLuaResult();
expect(diagnostics).toHaveLength(1);
expect(diagnostics[0].code).toBe(1234);
});
test("in memory plugin with factory", () => {
const { diagnostics } = util.testModule``
.setOptions({
luaPlugins: [
{
code: 1234,
plugin: options =>
({
afterEmit(program: ts.Program) {
return [
{
category: ts.DiagnosticCategory.Message,
messageText: "In memory plugin diagnostic message!",
code: options.code,
file: program.getSourceFiles()[0],
start: undefined,
length: undefined,
} satisfies ts.Diagnostic,
];
},
} satisfies Plugin),
},
],
})
.getLuaResult();
expect(diagnostics).toHaveLength(1);
expect(diagnostics[0].code).toBe(1234);
});