Skip to content

Commit 5c10345

Browse files
committed
Strict/loose mode for free ORIGINAL_SCOPE_VARIABLES
1 parent 1e73604 commit 5c10345

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/decode/decode.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,24 @@ describe("decode", () => {
217217

218218
assertEquals(info.ranges, []);
219219
});
220+
221+
it("throws for free ORIGINAL_SCOPE_VARIABLES items in strict mode", () => {
222+
const encoder = new ItemEncoder();
223+
encoder.addUnsignedVLQs(Tag.ORIGINAL_SCOPE_VARIABLES);
224+
encoder.addSignedVLQs(0, 1).finishItem();
225+
const map = createMap(encoder.encode(), ["foo", "bar"]);
226+
227+
assertThrows(() => decode(map, { mode: DecodeMode.STRICT }));
228+
});
229+
230+
it("ignores free ORIGINAL_SCOPE_VARIABLES items in loose mode", () => {
231+
const encoder = new ItemEncoder();
232+
encoder.addUnsignedVLQs(Tag.ORIGINAL_SCOPE_VARIABLES);
233+
encoder.addSignedVLQs(0, 1).finishItem();
234+
const map = createMap(encoder.encode(), ["foo", "bar"]);
235+
236+
const info = decode(map, { mode: DecodeMode.LOOSE });
237+
238+
assertEquals(info.scopes, []);
239+
});
220240
});

src/decode/decode.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { TokenIterator } from "../vlq.ts";
2727
* STRICT mode will throw in the following situations:
2828
*
2929
* - Encountering ORIGINAL_SCOPE_END, or GENERATED_RANGE_END items that don't have matching *_START items.
30+
* - Encountering ORIGINAL_SCOPE_VARIABLES items outside a surrounding scope START/END.
31+
* - Encountering GENERATED_RANGE_BINDINGS items outside a surrounding range START/END.
3032
* - Miss-matches between the number of variables in a scope vs the number of value expressions in the ranges.
3133
* - Out-of-bound indices into the "names" array.
3234
*/
@@ -121,9 +123,10 @@ class Decoder {
121123
case Tag.ORIGINAL_SCOPE_VARIABLES: {
122124
const scope = this.#scopeStack.at(-1);
123125
if (!scope) {
124-
throw new Error(
126+
this.#throwInStrictMode(
125127
"Encountered ORIGINAL_SCOPE_VARIABLES without surrounding ORIGINAL_SCOPE_START",
126128
);
129+
continue;
127130
}
128131

129132
for (const variableIdx of item.variableIdxs) {

0 commit comments

Comments
 (0)