mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
`.join(sep)` over a checked-dynamic separator fenced with SC2020. JS substitutes the default comma only for the undefined VALUE; every other value goes through ToString, which prints "null" for null, the digits for a number or boolean, and throws the catchable TypeError for a symbol. Evaluate the separator once into a hidden local, test it against the undefined dyn kind, and answer either the comma literal or dyn.toStringCoerce of the value. Unlike the statically typed arms (string, and a union with an undefined arm), a dynamic separator cannot be resolved statically, so this is the runtime test the dynamic tier exists for. Fixes the 2 pi sites (@anthropic-ai/sdk's node.mjs, pi's interactive-mode.ts). Evidence: tests/corpus/array-join-dynamic-separator.ts, run under Node and natively in the plain and sanitized differential lanes, plus its order-parity baseline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
// Array.prototype.join over a checked-dynamic separator: only the
|
|
// undefined VALUE means the default comma; every other value is ToString
|
|
// (null prints "null", numbers and booleans their text), and the
|
|
// separator expression evaluates once.
|
|
|
|
function joinWith(sep: any): string {
|
|
return ["a", "b", "c"].join(sep);
|
|
}
|
|
console.log(joinWith(undefined));
|
|
console.log(joinWith(null));
|
|
console.log(joinWith("-"));
|
|
console.log(joinWith(0));
|
|
console.log(joinWith(false));
|
|
console.log(joinWith(""));
|
|
|
|
const held: any = "+";
|
|
console.log(["x", "y"].join(held), ["x", "y"].join(undefined));
|
|
const numbers = [1, 2, 3];
|
|
console.log(numbers.join(held), numbers.join(null as any));
|
|
|
|
let reads = 0;
|
|
function separator(): any {
|
|
reads += 1;
|
|
return "/";
|
|
}
|
|
console.log(["p", "q"].join(separator()), reads);
|
|
|
|
// A dynamic separator that turns out to be undefined keeps the default.
|
|
function maybe(flagValue: boolean): any {
|
|
return flagValue ? "-" : undefined;
|
|
}
|
|
console.log(["m", "n"].join(maybe(true)), ["m", "n"].join(maybe(false)));
|