mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-23 06:10:02 +08:00
- Destructuring assignment now takes nested patterns (`({ p: { q } } = src)`, `[[a], b] = t`) and property/element targets (`[c.x, box.vals[i]] = arr`, `({ a: rec.f } = o)`, rest into members) — receivers temp at their pattern position, JS's reference-before-read order; defaults on member targets test against the target's own type. Defaults on nested targets keep a named fence (no single binding type to test against).
- Class instances destructure in assignment position like they already did in declarations: field slots and getter calls per element via the same fieldGetExpr dispatch, defaults, renames, and member targets; methods and undeclared names keep their pointed fences.
- Rest over class-instance sources packs the remaining instance FIELDS into a fresh record (JS's CopyDataProperties copies own enumerable properties — never prototype methods/accessors, matching the checker's rest type), in declarations and assignments; non-public fields, runtime-provided chains (Error/EventEmitter/stream), and key orders the packed shape cannot reproduce fence instead of diverging.
- Defaults on destructured getter results now lower (record and class accessors alike), with the getter result temped so the default's undefined test never calls it twice; numeric-literal pattern keys (`const { 2: x } = o`) fold to JS's canonical property name.
- Corpus 2536-2540 pin the new forms byte-identically against Node on both backends; the destructuring-fences diagnostics program keeps the honest residue (method extraction, non-public rest fields, nested-target defaults, index-signature rest).
- The ts7 order-parity baseline re-record backfills the entries the released file was missing (2490-2500 included) alongside the new fixtures; no existing entry changed.
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
// NESTED patterns in destructuring assignment to existing bindings:
|
|
// object-in-object, array-in-array, and mixed nesting — each level
|
|
// destructures its element's value through its own hidden temp.
|
|
const src = { p: { q: 1, r: 2 }, s: 3 };
|
|
let q = 0;
|
|
let r = 0;
|
|
let s = 0;
|
|
({ p: { q, r }, s } = src);
|
|
console.log(q, r, s);
|
|
|
|
// Deep nesting.
|
|
const deep = { o: { i: { v: 5, w: 6 } } };
|
|
let v = 0;
|
|
let w = 0;
|
|
({ o: { i: { v, w } } } = deep);
|
|
console.log(v, w);
|
|
|
|
// Arrays inside arrays (tuple sources type each level).
|
|
let a = 0;
|
|
let b = 0;
|
|
let c = 0;
|
|
[a, [b, c]] = [1, [2, 3]] as [number, [number, number]];
|
|
console.log(a, b, c);
|
|
|
|
// Records inside arrays and arrays inside records.
|
|
let x = 0;
|
|
let y = 0;
|
|
[{ x }, { x: y }] = [{ x: 7 }, { x: 8 }] as [{ x: number }, { x: number }];
|
|
console.log(x, y);
|
|
|
|
const holder = { list: [10, 11] as [number, number], name: "h" };
|
|
let n0 = 0;
|
|
let n1 = 0;
|
|
let name = "";
|
|
({ list: [n0, n1], name } = holder);
|
|
console.log(n0, n1, name);
|
|
|
|
// Nested rest: the sole-leading-rest unwrap consumes like the inner
|
|
// pattern alone.
|
|
let h0 = 0;
|
|
let h1 = 0;
|
|
const packed = [21, 22, 23];
|
|
[...[h0, h1]] = packed;
|
|
console.log(h0, h1);
|
|
|
|
// Nested defaults INSIDE the inner pattern still apply.
|
|
const partial: { pt: { u?: number } } = { pt: {} };
|
|
let u = -1;
|
|
({ pt: { u = 40 } } = partial);
|
|
console.log(u);
|