Skip to content

Commit f91705b

Browse files
committed
Decode range definitions
1 parent a53e254 commit f91705b

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/codec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ interface GeneratedRangeStartItem {
5858
flags: number;
5959
line?: number;
6060
column: number;
61+
definitionIdx?: number;
6162
}
6263

6364
interface GeneratedRangeEndItem {

src/decode/decode.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class Decoder {
5252
readonly #scopeStack: OriginalScope[] = [];
5353
readonly #rangeStack: GeneratedRange[] = [];
5454

55+
readonly #countToScope = new Map<number, OriginalScope>();
56+
#scopeCounter = 0;
57+
5558
constructor(scopes: string, names: string[]) {
5659
this.#encodedScopes = scopes;
5760
this.#names = names;
@@ -89,6 +92,7 @@ class Decoder {
8992
);
9093

9194
this.#scopeStack.push(scope);
95+
this.#countToScope.set(this.#scopeCounter++, scope);
9296
break;
9397
}
9498
case Tag.ORIGINAL_SCOPE_END: {
@@ -136,6 +140,14 @@ class Decoder {
136140
children: [],
137141
};
138142

143+
if (item.definitionIdx !== undefined) {
144+
this.#rangeState.defScopeIdx += item.definitionIdx;
145+
range.originalScope = this.#countToScope.get(
146+
this.#rangeState.defScopeIdx,
147+
);
148+
// TODO: Maybe throw if the idx is invalid?
149+
}
150+
139151
this.#rangeStack.push(range);
140152
break;
141153
}
@@ -176,6 +188,8 @@ class Decoder {
176188

177189
this.#scopes = [];
178190
this.#ranges = [];
191+
this.#scopeCounter = 0;
192+
this.#countToScope.clear();
179193

180194
return info;
181195
}
@@ -223,12 +237,18 @@ class Decoder {
223237
const line = flags & GeneratedRangeFlags.HAS_LINE
224238
? iter.nextUnsignedVLQ()
225239
: undefined;
240+
const column = iter.nextUnsignedVLQ();
241+
242+
const definitionIdx = flags & GeneratedRangeFlags.HAS_DEFINITION
243+
? iter.nextSignedVLQ()
244+
: undefined;
226245

227246
yield {
228247
tag,
229248
flags,
230249
line,
231-
column: iter.nextUnsignedVLQ(),
250+
column,
251+
definitionIdx,
232252
};
233253
break;
234254
}

src/roundtrip.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,23 @@ describe("round trip", () => {
101101

102102
assertCodec(builder.build());
103103
});
104+
105+
it("handles a single GeneratedRange with a definition scope", () => {
106+
builder.startScope(0, 0).endScope(10, 0).startRange(0, 0, { scope: 0 })
107+
.endRange(0, 10);
108+
109+
assertCodec(builder.build());
110+
});
111+
112+
it("handles multiple GeneratedRanges with different definition scopes", () => {
113+
builder.startScope(0, 0).endScope(10, 0).startScope(0, 0).endScope(20, 0)
114+
.startRange(0, 0)
115+
.startRange(0, 10, { scope: 0 })
116+
.endRange(0, 40)
117+
.startRange(0, 50, { scope: 1 })
118+
.endRange(0, 80)
119+
.endRange(0, 100);
120+
121+
assertCodec(builder.build());
122+
});
104123
});

0 commit comments

Comments
 (0)