Skip to content

refactor(core): dedupe mechanical duplication in renderer/layout.ts - #2516

Merged
kompiro merged 5 commits into
mainfrom
refactor/layout-dedupe-mechanical
Aug 16, 2026
Merged

kompiro merged 5 commits into
mainfrom
refactor/layout-dedupe-mechanical

Conversation

@kompiro

@kompiro kompiro commented Aug 15, 2026

Copy link
Copy Markdown
Owner

Summary

PR A of the #2512 layout.ts decomposition plan — the purely mechanical batch. Four commits, each independently green:

  1. computeLayers / groupStartLayersOf — the layering phase (grouped bands → forced kind tiers → topological fallback → direction hints) and the group-title-gap map were textually identical between layoutInner and layoutMultipleSystems; both now call one helper, so the two pipelines cannot drift on them (TPL-219, parallel-function parity).
  2. makeLayoutNode — the ten derived LayoutNode fields were written out as five near-identical object literals (ghost users, ghost rows, single-system placement, ghost-system services, multi-system placement). One shared constructor; label/annotation/owner/ghost resolution stays at each call site. Conditional spread keeps subLabel/ghost keys present only where the originals had them, so object shapes are unchanged.
  3. collapseAndAssignGroupLayers named options — 8 positional parameters with adjacent same-typed slots were the exact slot-swap hazard the LayoutOptions doc cites (Convert layout() tail to a LayoutOptions object (positional-param creep) [P2a follow-up] #1875). Also reattaches the function's JSDoc, orphaned 85 lines above it.
  4. Row centering / constants / dead code — shared centerRowsHorizontally (see proof below), GHOST_SYSTEM_GAP hoisted from two identical local declarations, constant-false isGhost plumbing deleted (kept ghost: false literals where they were set), and the multi path's containers-only dimension block annotated with fix(core): multi-system root view total dimensions ignore node and edge extents (potential clipping) #2513.

All helpers stay module-private in layout.ts; moving them into their own modules is PR D of the plan.

Equivalence note for centerRowsHorizontally

The single-system block sorted the row ids by x before summing the row width; the multi-system block summed first and sorted after. The row width is a plain sum over the same id set (order-independent), and both variants walk the ids sorted by x when re-placing, so one canonical order (sort, then sum) reproduces both exactly.

Verification

  • pnpm --filter @karasu-tools/core test — 3691 tests, 132 files, all green after every commit (layout.test.ts asserts exact coordinates; no snapshots).
  • pnpm typecheck / pnpm check:cycles / pnpm knip / pnpm lint / format — all green.
  • Byte-identity harness (the refactor(core): layoutMultipleSystems options object + shared group-collapse machinery #2057 method): every examples/**/*.krs (84 entries, with each project's .krs.style applied) rendered via buildAllViewsSvgProject in shape + icon modes on main and on this branch — 168/168 SVGs byte-identical (diff -r clean).

No changeset: internal refactor with zero user-visible change (per .claude/rules/changesets.md).

Refs #2512. Test perspective: TPL-219 (parallel-function parity).

🤖 Generated with Claude Code

…n layout

The layering phase (grouped bands -> forced kind tiers -> topological
fallback -> direction hints) and the group-title-gap start-layer map were
textually duplicated between layoutInner and layoutMultipleSystems. Extract
both into module-private helpers consumed by the two paths so they cannot
drift (TPL-219). Pure code motion; no behavior change.

Refs #2512
The ten derived LayoutNode fields were written out as five near-identical
object literals (ghost users, ghost rows, single-system placement,
ghost-system services, multi-system placement). Fold them into one
module-private constructor; label/annotation/owner/ghost resolution stays at
each call site. Conditional spread keeps subLabel/ghost keys present only
where the original literals had them, so object shapes are unchanged. Pure
code motion; no behavior change.

Refs #2512
…object

Eight positional parameters with adjacent same-typed slots are the exact
slot-swap hazard the LayoutOptions doc cites (#1875). Convert to a
destructured options object and reattach the function's JSDoc, which had
been orphaned 85 lines above it by an interleaved doc block. No behavior
change.

Refs #2512
…d isGhost

- Extract centerRowsHorizontally for the single- and multi-system placement
  phases. The two blocks sorted and summed in opposite orders, which is
  equivalent: the row width is a sum (order-independent) and both walk the
  row sorted by x when re-placing.
- Hoist the GHOST_SYSTEM_GAP = 80 constant declared locally in both ghost
  system placers to module scope.
- Delete the constant-false isGhost plumbing in layoutMultipleSystems: keep
  ghost: false literals where they were set, drop the never-taken
  'if (isGhost) le.ghost = true' branch.
- Annotate the multi path's containers-only dimension block with #2513, the
  issue tracking that divergence.

No behavior change.

Refs #2512
Code-review findings on this PR's diff:

- makeLayoutNode: drop the conditional spreads that preserved each call
  site's accidental key-presence differences. Every consumer reads ghost
  and subLabel by truthiness (svg-renderer, drawio exporter, routing
  filters), no test asserts key absence, and exactOptionalPropertyTypes is
  off, so a uniform shape (subLabel plain, ghost always present with ?? false
  default) is observably identical while removing the per-node spread temps
  and the three-way ghost spelling. layout.test.ts's ghost toBe(false) pin
  on the multi path still holds via the default.
- makeLayoutNode: owner becomes optional, removing the owner: undefined
  boilerplate at the two ghost call sites.
- computeLayers: build nodeIds only in the topological fallback branch; on
  grouped / kind-tier-forced canvases the array was dead work.

All 3691 core tests green; 168/168 example SVGs byte-identical vs main.

Refs #2512
@kompiro
kompiro merged commit a6069fb into main Aug 16, 2026
9 checks passed
@kompiro
kompiro deleted the refactor/layout-dedupe-mechanical branch August 16, 2026 06:46

This branch was successfully deployed

1 active deployment
preview — 71a49ffc Deployed Aug 15, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant