-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathsourcemaps.spec.ts
More file actions
350 lines (306 loc) · 11.7 KB
/
sourcemaps.spec.ts
File metadata and controls
350 lines (306 loc) · 11.7 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { SourceMapConsumer } from "source-map";
import * as tstl from "../../../src";
import { LuaTarget, transpileString } from "../../../src";
import { couldNotResolveRequire } from "../../../src/transpilation/diagnostics";
import * as util from "../../util";
import { lineAndColumnOf } from "./utils";
test.each([
{
code: `
const abc = "foo";
const def = "bar";
const xyz = "baz";
`,
assertPatterns: [
{ luaPattern: "abc", typeScriptPattern: "abc" },
{ luaPattern: "def", typeScriptPattern: "def" },
{ luaPattern: "xyz", typeScriptPattern: "xyz" },
{ luaPattern: '"foo"', typeScriptPattern: '"foo"' },
{ luaPattern: '"bar"', typeScriptPattern: '"bar"' },
{ luaPattern: '"baz"', typeScriptPattern: '"baz"' },
],
},
{
code: `
function abc() {
return def();
}
function def() {
return "foo";
}
`,
assertPatterns: [
{ luaPattern: "function abc(", typeScriptPattern: "function abc() {" },
{ luaPattern: "function def(", typeScriptPattern: "function def() {" },
{ luaPattern: "return def(", typeScriptPattern: "return def(" },
{ luaPattern: "end", typeScriptPattern: "function def() {" },
],
},
{
code: `
const enum abc { foo = 2, bar = 4 };
const xyz = abc.foo;
`,
assertPatterns: [
{ luaPattern: "xyz", typeScriptPattern: "xyz" },
{ luaPattern: "2", typeScriptPattern: "abc.foo" },
],
},
{
code: `
// @ts-ignore
import { Foo } from "foo";
Foo;
`,
assertPatterns: [
{ luaPattern: 'require("foo")', typeScriptPattern: '"foo"' },
{ luaPattern: "Foo", typeScriptPattern: "Foo" },
],
},
{
code: `
// @ts-ignore
import * as Foo from "foo";
Foo;
`,
assertPatterns: [
{ luaPattern: 'require("foo")', typeScriptPattern: '"foo"' },
{ luaPattern: "Foo", typeScriptPattern: "Foo" },
],
},
{
code: `
// @ts-ignore
class Bar extends Foo {
constructor() {
super();
}
}
`,
assertPatterns: [
{ luaPattern: "Bar = __TS__Class()", typeScriptPattern: "class Bar" },
{ luaPattern: "Bar.name =", typeScriptPattern: "class Bar" },
{ luaPattern: "__TS__ClassExtends(", typeScriptPattern: "extends" }, // find use of function, not import
{ luaPattern: "Foo", typeScriptPattern: "Foo" },
{ luaPattern: "function Bar.prototype.____constructor", typeScriptPattern: "constructor" },
],
},
{
code: `
class Foo {
}
`,
assertPatterns: [{ luaPattern: "function Foo.prototype.____constructor", typeScriptPattern: "class Foo" }],
},
{
code: `
class Foo {
bar = "baz";
}
`,
assertPatterns: [{ luaPattern: "function Foo.prototype.____constructor", typeScriptPattern: "class Foo" }],
},
{
code: `
declare const arr: string[];
for (const element of arr) {}
`,
assertPatterns: [
{ luaPattern: "arr", typeScriptPattern: "arr)" },
{ luaPattern: "element", typeScriptPattern: "element" },
],
},
{
code: `
declare function getArr(this: void): string[];
for (const element of getArr()) {}
`,
assertPatterns: [
{ luaPattern: "for", typeScriptPattern: "for" },
{ luaPattern: "getArr()", typeScriptPattern: "getArr()" },
{ luaPattern: "element", typeScriptPattern: "element" },
],
},
{
code: `
declare const arr: string[]
for (let i = 0; i < arr.length; ++i) {}
`,
assertPatterns: [
{ luaPattern: "i = 0", typeScriptPattern: "i = 0" },
{ luaPattern: "i < #arr", typeScriptPattern: "i < arr.length" },
{ luaPattern: "i + 1", typeScriptPattern: "++i" },
],
},
])("Source map has correct mapping (%p)", async ({ code, assertPatterns }) => {
const file = util
.testModule(code)
.ignoreDiagnostics([couldNotResolveRequire.code])
.expectToHaveNoDiagnostics()
.getMainLuaFileResult();
const consumer = await new SourceMapConsumer(file.luaSourceMap);
for (const { luaPattern, typeScriptPattern } of assertPatterns) {
const luaPosition = lineAndColumnOf(file.lua, luaPattern);
const mappedPosition = consumer.originalPositionFor(luaPosition);
const typescriptPosition = lineAndColumnOf(code, typeScriptPattern);
expect(mappedPosition).toMatchObject(typescriptPosition);
}
});
test.each([
{
fileName: "/proj/foo.ts",
config: {},
expectedSourcePath: "foo.ts", // ts and lua will be emitted to same directory
},
{
fileName: "/proj/src/foo.ts",
config: {
outDir: "/proj/dst",
},
expectedSourcePath: "../src/foo.ts", // path from proj/dst outDir to proj/src/foo.ts
},
{
fileName: "/proj/src/foo.ts",
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
expectedSourcePath: "../src/foo.ts", // path from proj/dst outDir to proj/src/foo.ts
},
{
fileName: "/proj/src/sub/foo.ts",
config: { rootDir: "/proj/src", outDir: "/proj/dst" },
expectedSourcePath: "../../src/sub/foo.ts", // path from proj/dst/sub outDir to proj/src/sub/foo.ts
},
{
fileName: "/proj/src/sub/main.ts",
config: { rootDir: "/proj/src", outDir: "/proj/dst", sourceRoot: "bin/binsub/binsubsub" },
expectedSourcePath: "../../src/sub/main.ts", // path from proj/dst/sub outDir to proj/src/sub/foo.ts
fullSource: "bin/src/sub/main.ts",
},
])("Source map has correct sources (%p)", async ({ fileName, config, fullSource, expectedSourcePath }) => {
const file = util.testModule`
const foo = "foo"
`
.setOptions(config)
.setMainFileName(fileName)
.getMainLuaFileResult();
const sourceMap = JSON.parse(file.luaSourceMap);
expect(sourceMap.sources).toHaveLength(1);
expect(sourceMap.sources[0]).toBe(expectedSourcePath);
const consumer = await new SourceMapConsumer(file.luaSourceMap);
expect(consumer.sources).toHaveLength(1);
expect(consumer.sources[0]).toBe(fullSource ?? expectedSourcePath);
});
test.each([
{ configSourceRoot: undefined, mapSourceRoot: "" },
{ configSourceRoot: "src", mapSourceRoot: "src/" },
{ configSourceRoot: "src/", mapSourceRoot: "src/" },
{ configSourceRoot: "src\\", mapSourceRoot: "src/" },
])("Source map has correct source root (%p)", ({ configSourceRoot, mapSourceRoot }) => {
const file = util.testModule`
const foo = "foo"
`
.setOptions({ sourceMap: true, sourceRoot: configSourceRoot })
.expectToHaveNoDiagnostics()
.getMainLuaFileResult();
const sourceMap = JSON.parse(file.luaSourceMap);
expect(sourceMap.sourceRoot).toBe(mapSourceRoot);
});
test.each([
{ code: 'const type = "foobar";', name: "type" },
{ code: 'const and = "foobar";', name: "and" },
{ code: 'const $$$ = "foobar";', name: "$$$" },
{ code: "const foo = { bar() { this; } };", name: "this" },
{ code: "function foo($$$: unknown) {}", name: "$$$" },
{ code: "class $$$ {}", name: "$$$" },
{ code: 'namespace $$$ { const foo = "bar"; }', name: "$$$" },
])("Source map has correct name mappings (%p)", async ({ code, name }) => {
const file = util.testModule(code).expectToHaveNoDiagnostics().getMainLuaFileResult();
const consumer = await new SourceMapConsumer(file.luaSourceMap);
const typescriptPosition = lineAndColumnOf(code, name);
let mappedName: string | undefined;
consumer.eachMapping(mapping => {
if (mapping.originalLine === typescriptPosition.line && mapping.originalColumn === typescriptPosition.column) {
mappedName = mapping.name;
}
});
expect(mappedName).toBe(name);
});
test("sourceMapTraceback saves sourcemap in _G", () => {
const code = `
function abc() {
return "foo";
}
return (globalThis as any).__TS__sourcemap;
`;
const builder = util
.testFunction(code)
.setOptions({ sourceMapTraceback: true, luaLibImport: tstl.LuaLibImportKind.Inline });
const sourceMap = builder.getLuaExecutionResult();
const transpiledLua = builder.getMainLuaCodeChunk();
expect(sourceMap).toEqual(expect.any(Object));
const sourceMapFiles = Object.keys(sourceMap);
expect(sourceMapFiles).toHaveLength(1);
const mainSourceMap = sourceMap[sourceMapFiles[0]];
const assertPatterns = [
{ luaPattern: "function abc(", typeScriptPattern: "function abc() {" },
{ luaPattern: 'return "foo"', typeScriptPattern: 'return "foo"' },
];
for (const { luaPattern, typeScriptPattern } of assertPatterns) {
const luaPosition = lineAndColumnOf(transpiledLua, luaPattern);
const mappedLine = mainSourceMap[luaPosition.line.toString()];
const typescriptPosition = lineAndColumnOf(code, typeScriptPattern);
expect(mappedLine).toEqual(typescriptPosition.line);
}
});
test("sourceMapTraceback gives traceback", () => {
const builder = util.testFunction`
return (debug.traceback as (this: void)=>string)();
`.setOptions({ sourceMapTraceback: true });
const traceback = builder.getLuaExecutionResult();
expect(traceback).toEqual(expect.any(String));
});
test("inline sourceMapTraceback gives traceback", () => {
const builder = util.testFunction`
return (debug.traceback as (this: void)=>string)();
`.setOptions({ sourceMapTraceback: true, luaLibImport: tstl.LuaLibImportKind.Inline });
const traceback = builder.getLuaExecutionResult();
expect(traceback).toEqual(expect.any(String));
});
test("Inline sourcemaps", () => {
const code = `
function abc() {
return def();
}
function def() {
return "foo";
}
return abc();
`;
const file = util
.testFunction(code)
.setOptions({ inlineSourceMap: true }) // We can't disable 'sourceMap' option because it's used for comparison
.disableSemanticCheck() // TS5053: Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.
.expectToMatchJsResult()
.getMainLuaFileResult();
const [, inlineSourceMapMatch] =
file.lua.match(/--# sourceMappingURL=data:application\/json;base64,([A-Za-z0-9+/=]+)/) ?? [];
const inlineSourceMap = Buffer.from(inlineSourceMapMatch, "base64").toString();
expect(inlineSourceMap).toBe(file.luaSourceMap);
});
test("loadstring sourceMapTraceback gives traceback", () => {
const loadStrCode = transpileString(
`function bar() {
const trace = (debug.traceback as (this: void)=>string)();
return trace;
}
return bar();`,
{ sourceMapTraceback: true, luaTarget: LuaTarget.Lua51 }
).file?.lua;
const builder = util.testModule`
const luaCode = \`${loadStrCode}\`;
return loadstring(luaCode, "foo.lua")();
`
.setTsHeader("declare function loadstring(this: void, string: string, chunkname: string): () => unknown")
.setOptions({ sourceMapTraceback: true, luaTarget: LuaTarget.Lua51 });
const traceback = builder.getLuaExecutionResult();
expect(traceback).toContain("foo.ts");
});