Skip to content

Commit 306c28f

Browse files
committed
fix: close the last scope range where the generated code ends
The scopes writer closed the open range at column 0 of the last line. When the output does not end with a newline, a range that starts on that line began after that point, so its end encoded a negative column delta, which the unsigned VLQ writer turned into 4294967292 and an invalid `scopes` field. Pass the generated end column through `finish`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01ThsbPWvW5vnYWEaFs2Q22F
1 parent a894b28 commit 306c28f

5 files changed

Lines changed: 61 additions & 15 deletions

File tree

‎lib/helpers/getFromStreamChunks.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ module.exports.getMap = (source, options) => {
142142
names: /** @type {string[]} */ (potentialNames),
143143
};
144144
if (scopesWriter !== undefined) {
145-
scopesWriter.finish(map, generated.generatedLine);
145+
scopesWriter.finish(
146+
map,
147+
generated.generatedLine,
148+
generated.generatedColumn,
149+
);
146150
}
147151
return map;
148152
};
@@ -236,7 +240,11 @@ module.exports.getSourceAndMap = (inputSource, options) => {
236240
names: /** @type {string[]} */ (potentialNames),
237241
};
238242
if (scopesWriter !== undefined) {
239-
scopesWriter.finish(map, generated.generatedLine);
243+
scopesWriter.finish(
244+
map,
245+
generated.generatedLine,
246+
generated.generatedColumn,
247+
);
240248
}
241249
}
242250
return { source: source !== undefined ? source : code, map };

‎lib/helpers/scopes.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const forEachMapping = (mappings, onSegment) => {
143143
* far into it the map reaches. Both lines are counted from zero, so a caller
144144
* whose lines start at one subtracts before feeding a segment in.
145145
* @param {number=} sourceCount number of entries in the map's `sources`, when known
146-
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, finish: (lastLine: number) => SourceScope[] }} collector
146+
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, finish: (lastLine: number, lastColumn?: number) => SourceScope[] }} collector
147147
*/
148148
const createScopeCollector = (sourceCount) => {
149149
/** @type {Map<number, SourceScope>} */
@@ -201,8 +201,8 @@ const createScopeCollector = (sourceCount) => {
201201
openScope = scope;
202202
openSource = sourceIndex;
203203
},
204-
finish(lastLine) {
205-
closeRun(lastLine, 0);
204+
finish(lastLine, lastColumn = 0) {
205+
closeRun(lastLine, lastColumn);
206206
return [...scopes.values()].sort((a, b) => a.sourceIndex - b.sourceIndex);
207207
},
208208
};
@@ -389,8 +389,9 @@ const addScopesToSourceMap = (sourceMap, getBindings) => {
389389
/**
390390
* Builds the `scopes` field while a map is being written, so the segments are
391391
* read as they are produced rather than decoded back out of `mappings`. Lines
392-
* are the one-based ones the chunk stream reports.
393-
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, addSource: (sourceIndex: number, scopeBindings?: ScopeBindings) => void, finish: (map: RawSourceMap, generatedLine: number) => void }} writer
392+
* are the one-based ones the chunk stream reports; `finish` takes the position
393+
* the generated code ends at, where the last range closes.
394+
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, addSource: (sourceIndex: number, scopeBindings?: ScopeBindings) => void, finish: (map: RawSourceMap, generatedLine: number, generatedColumn?: number) => void }} writer
394395
*/
395396
const createScopesWriter = () => {
396397
const collector = createScopeCollector();
@@ -410,9 +411,9 @@ const createScopesWriter = () => {
410411
bindingsBySource.set(sourceIndex, scopeBindings);
411412
}
412413
},
413-
finish(map, generatedLine) {
414+
finish(map, generatedLine, generatedColumn = 0) {
414415
if (bindingsBySource.size === 0) return;
415-
const scopes = collector.finish(generatedLine - 1);
416+
const scopes = collector.finish(generatedLine - 1, generatedColumn);
416417
let any = false;
417418
for (const scope of scopes) {
418419
const bindings = bindingsBySource.get(scope.sourceIndex);

‎lib/helpers/streamAndGetSourceAndMap.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const streamAndGetSourceAndMap = (
144144
names: /** @type {string[]} */ (potentialNames),
145145
};
146146
if (scopesWriter !== undefined) {
147-
scopesWriter.finish(map, generatedLine || 0);
147+
scopesWriter.finish(map, generatedLine || 0, generatedColumn || 0);
148148
}
149149
}
150150

‎test/scopes.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
const {
1212
addScopesToSourceMap,
1313
collectSourceScopes,
14+
createScopeCollector,
1415
encodeScopes,
1516
} = require("../lib/helpers/scopes");
1617

@@ -177,6 +178,37 @@ describe("scopes", () => {
177178
streamed.scopes,
178179
);
179180
});
181+
182+
it("closes the last range where output without a trailing newline ends", () => {
183+
// "x();" and "a;" share one generated line: lib.js's range starts at
184+
// column 4 and has to end at column 6, not at column 0 before it.
185+
const source = new ConcatSource(
186+
new OriginalSource("x();", "entry.js"),
187+
new OriginalSource("a;", "lib.js", BINDINGS),
188+
);
189+
const options = { columns: true, scopes: true };
190+
const map = mapOf(source, options);
191+
192+
const collector = createScopeCollector();
193+
collector.add(0, 0, 0, 0);
194+
collector.add(0, 4, 1, 0);
195+
const scopes = collector.finish(0, 6);
196+
assert.deepStrictEqual(scopes[1].rangeStarts, [{ line: 0, column: 4 }]);
197+
assert.deepStrictEqual(scopes[1].rangeEnds, [{ line: 0, column: 6 }]);
198+
for (const [name, value] of BINDINGS) {
199+
scopes[1].variables.push(name);
200+
scopes[1].values.push(value);
201+
}
202+
const names = [...mapOf(source, { columns: true }).names];
203+
assert.strictEqual(
204+
encodeScopes(scopes, map.sources.length, names),
205+
map.scopes,
206+
);
207+
assert.strictEqual(
208+
/** @type {RawSourceMap} */ (source.sourceAndMap(options).map).scopes,
209+
map.scopes,
210+
);
211+
});
180212
});
181213

182214
describe("addScopesToSourceMap", () => {

‎types/helpers/scopes.d.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function collectSourceScopes(
7272
* far into it the map reaches. Both lines are counted from zero, so a caller
7373
* whose lines start at one subtracts before feeding a segment in.
7474
* @param {number=} sourceCount number of entries in the map's `sources`, when known
75-
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, finish: (lastLine: number) => SourceScope[] }} collector
75+
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, finish: (lastLine: number, lastColumn?: number) => SourceScope[] }} collector
7676
*/
7777
export function createScopeCollector(sourceCount?: number | undefined): {
7878
add: (
@@ -81,13 +81,14 @@ export function createScopeCollector(sourceCount?: number | undefined): {
8181
sourceIndex: number,
8282
originalLine: number,
8383
) => void;
84-
finish: (lastLine: number) => SourceScope[];
84+
finish: (lastLine: number, lastColumn?: number) => SourceScope[];
8585
};
8686
/**
8787
* Builds the `scopes` field while a map is being written, so the segments are
8888
* read as they are produced rather than decoded back out of `mappings`. Lines
89-
* are the one-based ones the chunk stream reports.
90-
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, addSource: (sourceIndex: number, scopeBindings?: ScopeBindings) => void, finish: (map: RawSourceMap, generatedLine: number) => void }} writer
89+
* are the one-based ones the chunk stream reports; `finish` takes the position
90+
* the generated code ends at, where the last range closes.
91+
* @returns {{ add: (generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number) => void, addSource: (sourceIndex: number, scopeBindings?: ScopeBindings) => void, finish: (map: RawSourceMap, generatedLine: number, generatedColumn?: number) => void }} writer
9192
*/
9293
export function createScopesWriter(): {
9394
add: (
@@ -97,7 +98,11 @@ export function createScopesWriter(): {
9798
originalLine: number,
9899
) => void;
99100
addSource: (sourceIndex: number, scopeBindings?: ScopeBindings) => void;
100-
finish: (map: RawSourceMap, generatedLine: number) => void;
101+
finish: (
102+
map: RawSourceMap,
103+
generatedLine: number,
104+
generatedColumn?: number,
105+
) => void;
101106
};
102107
/**
103108
* Encodes the scope tree into the `scopes` field of the proposal, appending any

0 commit comments

Comments
 (0)