mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-21 05:50:55 +08:00
- 2530-2535 and 2541 lock in the destructuring forms the static tier already compiled but the corpus never pinned: object/array/tuple declarations (renames, defaults, holes, nesting, fresh rest tails), the options-object parameter pattern (element and whole-pattern defaults, tuple/rest parameter patterns), for-of heads (containers, defaults, rest, existing bindings), object rest freshness and key order, assignment basics (swaps, chains, expression value), and evaluation order (RHS once, elements left to right, lazy defaults that see earlier bindings). - All byte-identical against Node under both backends, and each reports 100% static coverage.
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// Object destructuring in declarations over statically-known record
|
|
// shapes: shorthand, renames, defaults (including a default referencing
|
|
// an EARLIER binding of the same pattern — JS's left-to-right rule),
|
|
// nesting, literal and foldable computed keys.
|
|
const obj = { a: 1, b: "two", c: true };
|
|
const { a, b } = obj;
|
|
const { a: renamed } = obj;
|
|
const { c: flag = false } = obj;
|
|
console.log(a, b, renamed, flag);
|
|
|
|
interface Opt {
|
|
p?: number;
|
|
q?: string;
|
|
}
|
|
const sparse: Opt = { q: "set" };
|
|
const { p = 42, q = "unset" } = sparse;
|
|
console.log(p, q);
|
|
|
|
// A later default may read an earlier binding (left-to-right).
|
|
const cfg: { tld?: string; host?: string } = {};
|
|
const { tld = "localhost", host = "www." + tld } = cfg;
|
|
console.log(tld, host);
|
|
|
|
// Nested patterns recurse through their own reads.
|
|
const nested = { inner: { u: 3, v: 4 }, s: 5 };
|
|
const { inner: { u, v }, s } = nested;
|
|
console.log(u, v, s);
|
|
|
|
// Literal and foldable computed keys resolve to static field names.
|
|
const keyed = { "space key": 1, 2: "two", plain: 3 };
|
|
const { "space key": spaced, 2: second, plain } = keyed;
|
|
console.log(spaced, second, plain);
|
|
|
|
const KEY = "plain" as const;
|
|
const { [KEY]: viaComputed } = keyed;
|
|
console.log(viaComputed);
|
|
|
|
// A default on a field with no undefined arm is dead code (never runs).
|
|
let evaluated = 0;
|
|
function sideEffect(): number {
|
|
evaluated += 1;
|
|
return -1;
|
|
}
|
|
const { a: whole = sideEffect() } = obj;
|
|
console.log(whole, evaluated);
|