Skip to content

Commit 4e6b5a2

Browse files
committed
try this
1 parent 64c830f commit 4e6b5a2

File tree

1 file changed

+29
-22
lines changed
  • packages/svelte/tests/sourcemaps

1 file changed

+29
-22
lines changed

packages/svelte/tests/sourcemaps/test.ts

Lines changed: 29 additions & 22 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 { LEAST_UPPER_BOUND, TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
6+
import { decode } from '@jridgewell/sourcemap-codec';
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, { offsetLine: 1 });
66+
const output_locator = getLocator(output);
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), { offsetLine: 1 });
83+
const original = locate(source, source.indexOf(str, idx));
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-
map = new TraceMap(map);
97+
const decoded = decode(map.mappings);
9898

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

118118
// Find segment in source map pointing from generated to original
119-
const result = originalPositionFor(map, generated);
120-
if (result.line === null && entry.strGenerated !== null) {
119+
const segments = decoded[generated.line];
120+
const segment = segments.find((segment) => segment[0] === generated.column);
121+
if (!segment && entry.strGenerated !== null) {
121122
throw new Error(
122123
`Could not find segment for '${str}' in sourcemap (${generated.line}:${generated.column})`
123124
);
124-
} else if (result.line !== null && entry.strGenerated === null) {
125+
} else if (segment && entry.strGenerated === null) {
125126
throw new Error(
126127
`Found segment for '${str}' in sourcemap (${generated.line}:${generated.column}) but should not`
127128
);
128-
} else if (result.line === null) {
129+
} else if (!segment) {
129130
continue;
130131
}
131132

@@ -139,31 +140,37 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
139140
}
140141

141142
// Check that segment points to expected original
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}'`);
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}'`);
144145

145146
// Same for end of string
146147
const generated_end = generated.column + generated_str.length;
147-
const result_end = originalPositionFor(map, {
148-
bias: LEAST_UPPER_BOUND,
149-
line: generated.line,
150-
column: generated_end
151-
});
152-
if (result_end.line === null)
153-
throw new Error(
154-
`Could not find end segment for '${str}' in sourcemap (${generated.line}:${generated_end})`
155-
);
148+
const end_segment = segments.find((segment) => segment[0] === generated_end);
149+
if (!end_segment) {
150+
// If the string is the last segment and it's the end of the line,
151+
// it's okay if there's no end segment (source maps save space by omitting it in that case)
152+
if (
153+
segments.indexOf(segment) !== segments.length - 1 ||
154+
/[\r\n]/.test(output[generated.character])
155+
) {
156+
throw new Error(
157+
`Could not find end segment for '${str}' in sourcemap (${generated.line}:${generated_end})`
158+
);
159+
} else {
160+
continue;
161+
}
162+
}
156163

157-
assert.equal(result_end.line, original.line, `mapped line end did not match for '${str}'`);
164+
assert.equal(end_segment[2], original.line, `mapped line end did not match for '${str}'`);
158165
assert.equal(
159-
result_end.column,
166+
end_segment[3],
160167
original.column + str.length,
161168
`mapped column end did not match for '${str}'`
162169
);
163170
}
164171
} catch (e) {
165172
console.log(`Source map ${info}:\n`);
166-
console.log(map._decoded);
173+
console.log(decoded);
167174
throw e;
168175
}
169176
}

0 commit comments

Comments
 (0)