Skip to main content

Module fold

Module fold 

Source
Expand description

Fold consecutive destructure-with-defaults runs (#2824).

Collapses a run of consecutive let <name> = <src>?.<key> ?? <default> statements sharing the same <src> into a single destructuring bind:

  let timeout = cfg?.timeout ?? 30
  let retries = cfg?.retries ?? 3

becomes

  let { timeout = 30, retries = 3 } = cfg ?? {}

Behavior-preserving: cfg ?? {} guards a nil source — bare let { x = d } = nil throws, whereas cfg?.x ?? d yields d. Coalescing the source to {} first reproduces the ?./?? semantics exactly.

Aliased sites (let t = cfg?.timeout ?? d) are folded with the Harn dict pattern alias form ({ timeout: t = d }). Only consecutive statements sharing one source are merged; a blank line, comment, or other statement between two lets breaks the run.

Functions§

fold_destructure_defaults
Fold a source string’s consecutive same-source let x = src?.x ?? d runs of length ≥ 2 into merged destructures. Returns the rewritten source (identical when nothing folds). language must name a tree-sitter grammar (e.g. "harn", "typescript").