Files
scriptc/tests/corpus/optional-node-list-iteration.ts
Chris Tate 82d7d9a237 feat(compiler): use a statically compilable TypeScript AST model
- Replace SDK AST materialization with checked native node, list, and text decoding.
- Preserve checker handle identity and snapshot reuse across the frontend.
- Compile generic visitors and guarded node-list iteration without dynamic execution.
2026-09-28 00:40:27 -05:00

31 lines
1007 B
TypeScript

class Item {
readonly value: number;
constructor(value: number) { this.value = value; }
}
const entries: (Item | Item[] | undefined)[] = [new Item(1), [new Item(2), new Item(3)], undefined];
function lookup(index: number): Item | Item[] | undefined {
return entries[index];
}
for (let index = 0; index < 4; index++) {
const value = lookup(index);
if (Array.isArray(value)) {
for (const item of value) console.log("list", item.value);
} else if (value !== undefined) console.log("node", value.value);
else console.log("missing");
}
// A captured assignment can invalidate a checker narrow. The loop must
// validate the actual payload instead of reading an absent array pointer.
let captured: Item | Item[] | undefined = lookup(1);
function clear(): void { captured = undefined; }
if (Array.isArray(captured)) {
clear();
try {
for (const item of captured) console.log("unreachable", item.value);
} catch (error) {
if (error instanceof Error) console.log(error.name);
}
}