-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Expand file tree
/
Copy pathnls-sourcemap.test.ts
More file actions
485 lines (421 loc) · 17.8 KB
/
Copy pathnls-sourcemap.test.ts
File metadata and controls
485 lines (421 loc) · 17.8 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import { suite, test } from 'node:test';
import * as esbuild from 'esbuild';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import { type RawSourceMap, SourceMapConsumer } from 'source-map';
import { nlsPlugin, createNLSCollector, finalizeNLS, postProcessNLS } from '../nls-plugin.ts';
import { adjustSourceMap } from '../private-to-property.ts';
// analyzeLocalizeCalls requires the import path to end with `/nls`
const NLS_STUB = [
'export function localize(key: string, message: string, ...args: any[]): string {',
'\treturn message;',
'}',
'export function localize2(key: string, message: string, ...args: any[]): { value: string; original: string } {',
'\treturn { value: message, original: message };',
'}',
].join('\n');
interface BundleResult {
js: string;
mapJson: RawSourceMap;
map: SourceMapConsumer;
cleanup: () => void;
}
/**
* Helper: create a temp directory with source files, bundle with NLS, and return
* the generated JS + parsed source map. The NLS stub is automatically placed at
* `vs/nls.ts` so test files can import from `../vs/nls` (when placed in `test/`).
*/
async function bundleWithNLS(
files: Record<string, string>,
entryPoint: string,
opts?: { postProcess?: boolean; minify?: boolean }
): Promise<BundleResult> {
const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'nls-sm-test-'));
const srcDir = path.join(tmpDir, 'src');
const outDir = path.join(tmpDir, 'out');
await fs.promises.mkdir(srcDir, { recursive: true });
await fs.promises.mkdir(outDir, { recursive: true });
// Write source files (always include the NLS stub at vs/nls.ts)
const allFiles = { 'vs/nls.ts': NLS_STUB, ...files };
for (const [name, content] of Object.entries(allFiles)) {
const filePath = path.join(srcDir, name);
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.writeFile(filePath, content);
}
const collector = createNLSCollector();
const result = await esbuild.build({
entryPoints: [path.join(srcDir, entryPoint)],
outfile: path.join(outDir, entryPoint.replace(/\.ts$/, '.js')),
bundle: true,
format: 'esm',
platform: 'neutral',
target: ['es2024'],
packages: 'external',
sourcemap: 'linked',
sourcesContent: true,
minify: opts?.minify ?? false,
write: false,
plugins: [
nlsPlugin({ baseDir: srcDir, collector }),
],
tsconfigRaw: JSON.stringify({
compilerOptions: {
experimentalDecorators: true,
useDefineForClassFields: false
}
}),
logLevel: 'warning',
});
let jsContent = '';
let mapContent = '';
for (const file of result.outputFiles!) {
if (file.path.endsWith('.js')) {
jsContent = file.text;
} else if (file.path.endsWith('.map')) {
mapContent = file.text;
}
}
// Optionally apply NLS post-processing (replaces placeholders with indices)
if (opts?.postProcess) {
const nlsResult = await finalizeNLS(collector, outDir);
const preNLSCode = jsContent;
const nlsProcessed = postProcessNLS(jsContent, nlsResult.indexMap, false);
jsContent = nlsProcessed.code;
// Adjust source map for NLS edits
if (nlsProcessed.edits.length > 0) {
const mapJson = JSON.parse(mapContent);
const adjusted = adjustSourceMap(mapJson, preNLSCode, nlsProcessed.edits);
mapContent = JSON.stringify(adjusted);
}
}
assert.ok(jsContent, 'Expected JS output');
assert.ok(mapContent, 'Expected source map output');
const mapJson = JSON.parse(mapContent);
const map = new SourceMapConsumer(mapJson);
const cleanup = () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
};
return { js: jsContent, mapJson, map, cleanup };
}
/**
* Find the 1-based line number in `text` that contains `needle`.
*/
function findLine(text: string, needle: string): number {
const lines = text.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(needle)) {
return i + 1; // 1-based
}
}
throw new Error(`Could not find "${needle}" in text`);
}
/**
* Find the 0-based column of `needle` within the line that contains it.
*/
function findColumn(text: string, needle: string): number {
const lines = text.split('\n');
for (const line of lines) {
const col = line.indexOf(needle);
if (col !== -1) {
return col;
}
}
throw new Error(`Could not find "${needle}" in text`);
}
suite('NLS plugin source maps', () => {
test('NLS plugin transforms localize calls into placeholders', async () => {
const source = [
'import { localize } from "../vs/nls";',
'export const msg = localize("testKey", "Test Message");',
].join('\n');
const { js, cleanup } = await bundleWithNLS(
{ 'test/verify.ts': source },
'test/verify.ts',
);
try {
assert.ok(js.includes('%%NLS:'),
'Bundle should contain %%NLS: placeholder.\nActual JS (first 500 chars):\n' + js.substring(0, 500));
} finally {
cleanup();
}
});
test('file without localize calls has correct source map', async () => {
const source = [
'export function add(a: number, b: number): number {',
'\treturn a + b;',
'}',
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'simple.ts': source },
'simple.ts',
);
try {
const bundleLine = findLine(js, 'return a + b');
const bundleCol = findColumn(js, 'return a + b');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 2, 'Should map to line 2 of original');
} finally {
cleanup();
}
});
test('sourcesContent should contain original source, not NLS-transformed', async () => {
const source = [
'import { localize } from "../vs/nls";',
'export const msg = localize("myKey", "Hello World");',
'export function greet(): string {',
'\treturn msg;',
'}',
].join('\n');
const { mapJson, cleanup } = await bundleWithNLS(
{ 'test/greeting.ts': source },
'test/greeting.ts',
);
try {
const sourcesContent: string[] = mapJson.sourcesContent ?? [];
const sources: string[] = mapJson.sources ?? [];
const greetingIdx = sources.findIndex((s: string) => s.includes('greeting'));
assert.ok(greetingIdx >= 0, 'Should find greeting.ts in sources');
const greetingContent = sourcesContent[greetingIdx];
assert.ok(greetingContent, 'Should have sourcesContent for greeting.ts');
assert.ok(!greetingContent.includes('%%NLS:'),
'sourcesContent should NOT contain NLS placeholder.\nActual:\n' + greetingContent);
assert.ok(greetingContent.includes('localize("myKey", "Hello World")'),
'sourcesContent should contain the exact original localize call.\nActual:\n' + greetingContent);
} finally {
cleanup();
}
});
test('NLS-affected nested file keeps a non-duplicated source path', async () => {
const source = [
'import { localize } from "../../vs/nls";',
'export const msg = localize("myKey", "Hello World");',
].join('\n');
const { mapJson, cleanup } = await bundleWithNLS(
{ 'nested/deep/file.ts': source },
'nested/deep/file.ts',
);
try {
const sources: string[] = mapJson.sources ?? [];
const nestedSource = sources.find((s: string) => s.endsWith('/nested/deep/file.ts'));
assert.ok(nestedSource, 'Should find nested/deep/file.ts in sources');
assert.ok(!nestedSource.includes('/nested/deep/nested/deep/file.ts'),
`Source path should not duplicate directory segments. Actual: ${nestedSource}`);
} finally {
cleanup();
}
});
test('line mapping correct for code after localize calls', async () => {
const source = [
'import { localize } from "../vs/nls";', // 1
'const label = localize("key1", "A long message");', // 2
'const label2 = localize("key2", "Another message");', // 3
'export function computeResult(x: number): number {', // 4
'\treturn x * 42;', // 5
'}', // 6
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/multi.ts': source },
'test/multi.ts',
);
try {
const bundleLine = findLine(js, 'return x * 42');
const bundleCol = findColumn(js, 'return x * 42');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 5, 'Should map back to line 5');
} finally {
cleanup();
}
});
test('column mapping for code on same line after localize call', async () => {
// The NLS placeholder is longer than the original key, so column offsets
// for tokens AFTER the localize call on the same line will drift if
// source map mappings point to the NLS-transformed source positions.
const source = [
'import { localize } from "../vs/nls";',
'const x = localize("k", "m"); const z = "FINDME"; export { x, z };',
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/coldrift.ts': source },
'test/coldrift.ts',
);
try {
assert.ok(js.includes('%%NLS:'), 'Bundle should contain NLS placeholders');
const bundleLine = findLine(js, 'FINDME');
const bundleCol = findColumn(js, '"FINDME"');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 2, 'Should map to line 2');
// The original column of "FINDME" in the source
const originalCol = findColumn(source, '"FINDME"');
// The mapped column should match the ORIGINAL source positions.
// Allow drift from TS->JS transform (const->var, export removal, etc.)
// but NOT the large NLS placeholder drift (~100+ chars) from before the fix.
const columnDrift = Math.abs(pos.column! - originalCol);
assert.ok(columnDrift <= 20,
`Column should be close to original. Expected ~${originalCol}, got ${pos.column} (drift: ${columnDrift}). ` +
`A drift > 20 indicates the NLS placeholder shift leaked into the source map.`);
} finally {
cleanup();
}
});
test('class with localize - method positions map correctly', async () => {
const source = [
'import { localize } from "../vs/nls";', // 1
'', // 2
'export class MyWidget {', // 3
'\tprivate readonly label = localize("widgetLabel", "My Cool Widget");', // 4
'', // 5
'\tconstructor(private readonly name: string) {}', // 6
'', // 7
'\tgetDescription(): string {', // 8
'\t\treturn this.name + ": " + this.label;', // 9
'\t}', // 10
'', // 11
'\tdispose(): void {', // 12
'\t\tconsole.log("disposed");', // 13
'\t}', // 14
'}', // 15
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/widget.ts': source },
'test/widget.ts',
);
try {
const bundleLine = findLine(js, '"disposed"');
const bundleCol = findColumn(js, 'console.log');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 13, 'Should map dispose method body to line 13');
} finally {
cleanup();
}
});
test('many localize calls - line mappings remain correct', async () => {
const source = [
'import { localize } from "../vs/nls";', // 1
'', // 2
'const a = localize("a", "Alpha");', // 3
'const b = localize("b", "Bravo with a longer message");', // 4
'const c = localize("c", "Charlie");', // 5
'const d = localize("d", "Delta is the fourth");', // 6
'const e = localize("e", "Echo");', // 7
'', // 8
'export function getAll(): string {', // 9
'\treturn [a, b, c, d, e].join(", ");', // 10
'}', // 11
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/many.ts': source },
'test/many.ts',
);
try {
const bundleLine = findLine(js, '.join(", ")');
const bundleCol = findColumn(js, '.join(", ")');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 10, 'Should map join() back to line 10');
} finally {
cleanup();
}
});
test('post-processed NLS - source map still has original content', async () => {
const source = [
'import { localize } from "../vs/nls";',
'export const msg = localize("greeting", "Hello World");',
].join('\n');
const { js, mapJson, cleanup } = await bundleWithNLS(
{ 'test/post.ts': source },
'test/post.ts',
{ postProcess: true }
);
try {
assert.ok(!js.includes('%%NLS:'), 'JS should not contain NLS placeholders after post-processing');
const sources: string[] = mapJson.sources ?? [];
const postIdx = sources.findIndex((s: string) => s.includes('post'));
assert.ok(postIdx >= 0, 'Should find post.ts in sources');
const postContent = (mapJson.sourcesContent ?? [])[postIdx];
assert.ok(postContent, 'Should have sourcesContent for post.ts');
assert.ok(postContent.includes('localize("greeting"'),
'sourcesContent should still contain original localize("greeting") call');
assert.ok(!postContent.includes('%%NLS:'),
'sourcesContent should not contain NLS placeholders');
} finally {
cleanup();
}
});
test('post-processed NLS - column mappings correct after placeholder replacement', async () => {
// NLS placeholders like "%%NLS:test/drift#k%%" are much longer than their
// replacements (e.g. "0"). Without source map adjustment the columns for
// tokens AFTER the replacement drift by the cumulative length delta.
const source = [
'import { localize } from "../vs/nls";', // 1
'export const a = localize("k1", "Alpha"); export const MARKER = "FINDME";', // 2
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/drift.ts': source },
'test/drift.ts',
{ postProcess: true }
);
try {
assert.ok(!js.includes('%%NLS:'), 'Placeholders should be replaced');
const bundleLine = findLine(js, 'FINDME');
const bundleCol = findColumn(js, '"FINDME"');
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source');
assert.strictEqual(pos.line, 2, 'Should map to line 2');
const originalCol = findColumn(source, '"FINDME"');
const columnDrift = Math.abs(pos.column! - originalCol);
assert.ok(columnDrift <= 20,
`Column drift after NLS post-processing should be small. ` +
`Expected ~${originalCol}, got ${pos.column} (drift: ${columnDrift}). ` +
`Large drift means postProcessNLS edits were not applied to the source map.`);
} finally {
cleanup();
}
});
test('minified bundle with NLS - end-to-end column mapping', async () => {
// With minification, the entire output is (roughly) on one line.
// Multiple NLS replacements compound their column shifts. A function
// defined after several localize() calls must still map correctly.
const source = [
'import { localize } from "../vs/nls";', // 1
'', // 2
'export const a = localize("k1", "Alpha message");', // 3
'export const b = localize("k2", "Bravo message that is quite long");', // 4
'export const c = localize("k3", "Charlie");', // 5
'export const d = localize("k4", "Delta is the fourth letter");', // 6
'', // 7
'export function computeResult(x: number): number {', // 8
'\treturn x * 42;', // 9
'}', // 10
].join('\n');
const { js, map, cleanup } = await bundleWithNLS(
{ 'test/minified.ts': source },
'test/minified.ts',
{ postProcess: true, minify: true }
);
try {
assert.ok(!js.includes('%%NLS:'), 'Placeholders should be replaced');
// Find the computeResult function in the minified output.
// esbuild minifies `x * 42` and may rename the parameter, so
// search for `*42` which survives both minification and renaming.
const needle = '*42';
const bundleLine = findLine(js, needle);
const bundleCol = findColumn(js, needle);
const pos = map.originalPositionFor({ line: bundleLine, column: bundleCol });
assert.ok(pos.source, 'Should have source for minified mapping');
assert.strictEqual(pos.line, 9,
`Should map "*42" back to line 9. Got line ${pos.line}.`);
} finally {
cleanup();
}
});
});