Skip to content

Commit 17b054b

Browse files
committed
Make column for same line original scope positions relative
Part of aligning with tc39/ecma426#188.
1 parent 384edda commit 17b054b

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/decode/decode.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function decode(
4747

4848
const DEFAULT_SCOPE_STATE = {
4949
line: 0,
50+
column: 0,
5051
name: 0,
5152
kind: 0,
5253
variable: 0,
@@ -214,9 +215,14 @@ class Decoder {
214215

215216
#handleOriginalScopeStartItem(item: OriginalScopeStartItem) {
216217
this.#scopeState.line += item.line;
218+
if (item.line === 0) {
219+
this.#scopeState.column += item.column;
220+
} else {
221+
this.#scopeState.column = item.column;
222+
}
217223
const scope: OriginalScope = {
218-
start: { line: this.#scopeState.line, column: item.column },
219-
end: { line: this.#scopeState.line, column: item.column },
224+
start: { line: this.#scopeState.line, column: this.#scopeState.column },
225+
end: { line: this.#scopeState.line, column: this.#scopeState.column },
220226
isStackFrame: false,
221227
variables: [],
222228
children: [],
@@ -256,6 +262,11 @@ class Decoder {
256262

257263
#handleOriginalScopeEndItem(line: number, column: number) {
258264
this.#scopeState.line += line;
265+
if (line === 0) {
266+
this.#scopeState.column += column;
267+
} else {
268+
this.#scopeState.column = column;
269+
}
259270

260271
const scope = this.#scopeStack.pop();
261272
if (!scope) {
@@ -265,7 +276,10 @@ class Decoder {
265276
return;
266277
}
267278

268-
scope.end = { line: this.#scopeState.line, column };
279+
scope.end = {
280+
line: this.#scopeState.line,
281+
column: this.#scopeState.column,
282+
};
269283

270284
if (this.#scopeStack.length > 0) {
271285
const parent = this.#scopeStack.at(-1)!;

src/encode/encoder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export class Encoder {
8181

8282
let flags = 0;
8383
const encodedLine = line - this.#scopeState.line;
84+
const encodedColumn = encodedLine === 0
85+
? column - this.#scopeState.column
86+
: column;
8487
this.#scopeState.line = line;
8588
this.#scopeState.column = column;
8689

@@ -103,7 +106,7 @@ export class Encoder {
103106
if (scope.isStackFrame) flags |= OriginalScopeFlags.IS_STACK_FRAME;
104107

105108
this.#encodeTag(EncodedTag.ORIGINAL_SCOPE_START).#encodeUnsigned(flags)
106-
.#encodeUnsigned(encodedLine).#encodeUnsigned(column);
109+
.#encodeUnsigned(encodedLine).#encodeUnsigned(encodedColumn);
107110
if (encodedName !== undefined) this.#encodeSigned(encodedName);
108111
if (encodedKind !== undefined) this.#encodeSigned(encodedKind);
109112
this.#finishItem();
@@ -130,12 +133,15 @@ export class Encoder {
130133
this.#verifyPositionWithScopeState(line, column);
131134

132135
const encodedLine = line - this.#scopeState.line;
136+
const encodedColumn = encodedLine === 0
137+
? column - this.#scopeState.column
138+
: column;
133139

134140
this.#scopeState.line = line;
135141
this.#scopeState.column = column;
136142

137143
this.#encodeTag(EncodedTag.ORIGINAL_SCOPE_END).#encodeUnsigned(encodedLine)
138-
.#encodeUnsigned(column).#finishItem();
144+
.#encodeUnsigned(encodedColumn).#finishItem();
139145
}
140146

141147
#encodeGeneratedRange(range: GeneratedRange): void {

src/roundtrip.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ describe("round trip", () => {
5757
assertCodec(builder.build());
5858
});
5959

60+
it("handles scopes that start on the same line", () => {
61+
builder.startScope(0, 5).startScope(0, 10).endScope(10, 5).endScope(10, 10);
62+
63+
assertCodec(builder.build());
64+
});
65+
6066
it("handles scope names", () => {
6167
builder.startScope(0, 0, { name: "foo" }).startScope(10, 0, { name: "bar" })
6268
.endScope(20, 0).endScope(30, 0);

0 commit comments

Comments
 (0)