Files
scriptc/tests/corpus/6187-js-match-or-empty-array.js
T
Chris Tate a48464a1a6 Inspect self-referential error subclasses without recursing; render their cycles as Node does
fieldedErrorReason checked each field of a fielded error class with a
fresh inspectSupport walk, so a field typed as the class itself
(`next: Chain`) recursed until the compiler's stack overflowed. It now
continues the caller's walk, where the class is a recursive shape.

The fielded error helper then rendered a cycle by expanding it to the
depth budget, where Node prints `<ref *1>` and `[Circular *1]`. Classes
whose fields reach the class or one of its bases now run the circular
machinery other cycle-capable objects use (circCheck before the depth
answer, seenPush, refWrap), which also covers a cycle through a
base-typed field of a subclass. New corpus program
6526-error-subclass-cycles.ts.

6187-js-match-or-empty-array.js compiles without its deferred fence now
that the match-or-empty union reads through its widened members: the
`@deferred-fences: 1` header goes, and the program calls the function
the fence used to cover.
2026-10-20 11:37:01 -05:00

21 lines
698 B
JavaScript

// `s.match(re) || []` types the empty array as an empty tuple that the
// typescript-go API panics to describe (moment.js's configFromStringAndFormat).
// The union reads through its widened members, so the whole program
// compiles, including the statements whose lowering asks about that type.
/** @param {string} s */
function firstDigit(s) {
return (s.match(/\d/) || [])[0];
}
/** @param {string} s */
function digitCount(s) {
let count = 0;
for (const ch of s) if (ch >= '0' && ch <= '9') count++;
return count;
}
const parsers = { firstDigit, digitCount };
console.log(typeof parsers.firstDigit, parsers.digitCount('a1b22'));
console.log(firstDigit('a1b22'), firstDigit('none'));