Skip to content

Commit f9aa7c6

Browse files
committed
use TraceMap for more robust mapping checks
1 parent 577d9c0 commit f9aa7c6

File tree

1 file changed

+18
-16
lines changed
  • packages/svelte/tests/sourcemaps

1 file changed

+18
-16
lines changed

packages/svelte/tests/sourcemaps/test.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert } from 'vitest';
33
import { getLocator, locate } from 'locate-character';
44
import { suite, type BaseTest } from '../suite.js';
55
import { compile_directory } from '../helpers.js';
6-
import { decode } from '@jridgewell/sourcemap-codec';
6+
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
77

88
type SourceMapEntry =
99
| string
@@ -63,7 +63,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
6363
const input = fs.readFileSync(`${cwd}/input.svelte`, 'utf-8');
6464

6565
function compare(info: string, output: string, map: any, entries: SourceMapEntry[]) {
66-
const output_locator = getLocator(output);
66+
const output_locator = getLocator(output, { offsetLine: 1 });
6767

6868
/** Find line/column of string in original code */
6969
function find_original(entry: SourceMapEntry, idx = 0) {
@@ -80,7 +80,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
8080
source = input;
8181
}
8282

83-
const original = locate(source, source.indexOf(str, idx));
83+
const original = locate(source, source.indexOf(str, idx), { offsetLine: 1 });
8484
if (!original)
8585
throw new Error(`Could not find '${str}'${idx > 0 ? ` after index ${idx}` : ''} in input`);
8686
return original;
@@ -94,7 +94,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
9494
return generated;
9595
}
9696

97-
const decoded = decode(map.mappings);
97+
map = new TraceMap(map);
9898

9999
try {
100100
for (let entry of entries) {
@@ -116,17 +116,16 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
116116
}
117117

118118
// Find segment in source map pointing from generated to original
119-
const segments = decoded[generated.line];
120-
const segment = segments.find((segment) => segment[0] === generated.column);
121-
if (!segment && entry.strGenerated !== null) {
119+
const result = originalPositionFor(map, generated);
120+
if (result.line === null && entry.strGenerated !== null) {
122121
throw new Error(
123122
`Could not find segment for '${str}' in sourcemap (${generated.line}:${generated.column})`
124123
);
125-
} else if (segment && entry.strGenerated === null) {
124+
} else if (result.line !== null && entry.strGenerated === null) {
126125
throw new Error(
127126
`Found segment for '${str}' in sourcemap (${generated.line}:${generated.column}) but should not`
128127
);
129-
} else if (!segment) {
128+
} else if (result.line === null) {
130129
continue;
131130
}
132131

@@ -140,27 +139,30 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
140139
}
141140

142141
// Check that segment points to expected original
143-
assert.equal(segment[2], original.line, `mapped line did not match for '${str}'`);
144-
assert.equal(segment[3], original.column, `mapped column did not match for '${str}'`);
142+
assert.equal(result.line, original.line, `mapped line did not match for '${str}'`);
143+
assert.equal(result.column, original.column, `mapped column did not match for '${str}'`);
145144

146145
// Same for end of string
147146
const generated_end = generated.column + generated_str.length;
148-
const end_segment = segments.find((segment) => segment[0] === generated_end);
149-
if (!end_segment)
147+
const result_end = originalPositionFor(map, {
148+
line: generated.line,
149+
column: generated_end
150+
});
151+
if (result_end.line === null)
150152
throw new Error(
151153
`Could not find end segment for '${str}' in sourcemap (${generated.line}:${generated_end})`
152154
);
153155

154-
assert.equal(end_segment[2], original.line, `mapped line end did not match for '${str}'`);
156+
assert.equal(result_end.line, original.line, `mapped line end did not match for '${str}'`);
155157
assert.equal(
156-
end_segment[3],
158+
result_end.column,
157159
original.column + str.length,
158160
`mapped column end did not match for '${str}'`
159161
);
160162
}
161163
} catch (e) {
162164
console.log(`Source map ${info}:\n`);
163-
console.log(decoded);
165+
console.log(map._decoded);
164166
throw e;
165167
}
166168
}

0 commit comments

Comments
 (0)