Skip to content

Commit 1e73604

Browse files
committed
Implement strict/loose verification for generated range start/end
1 parent 58f5c95 commit 1e73604

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/decode/decode.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,44 @@ describe("decode", () => {
177177

178178
assertEquals(info.scopes, []);
179179
});
180+
181+
it("throws in strict mode when encountering an GENERATED_RANGE_END without START", () => {
182+
const encoder = new ItemEncoder();
183+
encoder.addUnsignedVLQs(Tag.GENERATED_RANGE_END);
184+
encoder.addSignedVLQs(42).finishItem();
185+
const map = createMap(encoder.encode(), []);
186+
187+
assertThrows(() => decode(map, { mode: DecodeMode.STRICT }));
188+
});
189+
190+
it("ignores GENERATED_RANGE_END items without START in loose mode", () => {
191+
const encoder = new ItemEncoder();
192+
encoder.addUnsignedVLQs(Tag.GENERATED_RANGE_END);
193+
encoder.addSignedVLQs(42).finishItem();
194+
const map = createMap(encoder.encode(), []);
195+
196+
const info = decode(map, { mode: DecodeMode.LOOSE });
197+
198+
assertEquals(info.ranges, []);
199+
});
200+
201+
it("throws for un-matched GENERATED_RANGE_START at the end in loose mode", () => {
202+
const encoder = new ItemEncoder();
203+
encoder.addUnsignedVLQs(Tag.GENERATED_RANGE_START, 0);
204+
encoder.addSignedVLQs(42).finishItem();
205+
const map = createMap(encoder.encode(), []);
206+
207+
assertThrows(() => decode(map, { mode: DecodeMode.STRICT }));
208+
});
209+
210+
it("ignores un-matched GENERATED_RANGE_START at the end in loose mode", () => {
211+
const encoder = new ItemEncoder();
212+
encoder.addUnsignedVLQs(Tag.GENERATED_RANGE_START, 0);
213+
encoder.addSignedVLQs(42).finishItem();
214+
const map = createMap(encoder.encode(), []);
215+
216+
const info = decode(map, { mode: DecodeMode.LOOSE });
217+
218+
assertEquals(info.ranges, []);
219+
});
180220
});

src/decode/decode.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ class Decoder {
203203

204204
const range = this.#rangeStack.pop();
205205
if (!range) {
206-
throw new Error(
206+
this.#throwInStrictMode(
207207
"Encountered GENERATED_RANGE_END without matching GENERATED_RANGE_START!",
208208
);
209+
continue;
209210
}
210211

211212
range.end = {
@@ -250,6 +251,11 @@ class Decoder {
250251
"Encountered ORIGINAL_SCOPE_START without matching END!",
251252
);
252253
}
254+
if (this.#rangeStack.length > 0) {
255+
this.#throwInStrictMode(
256+
"Encountered GENERATED_RANGE_START without matching END!",
257+
);
258+
}
253259

254260
const info = { scopes: this.#scopes, ranges: this.#ranges };
255261

0 commit comments

Comments
 (0)