Skip to content

fix: preserve nested table-array scoping - #329

Merged
DecimalTurn merged 52 commits into
latestfrom
dev-neset-aot
Oct 22, 2026
Merged

DecimalTurn merged 52 commits into
latestfrom
dev-neset-aot

Conversation

@DecimalTurn

@DecimalTurn DecimalTurn commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

This PR mainly combines nested AOT bugs and other minor bugs.

Fixed

  • Patching: prevent applying changes in the wrong place for nested table-array entries when removing sub-tables, resolving nested AOT paths, materialising emptied arrays, replacing values inside an entry and appending new entries.
  • Parsing: accept dotted keys that extend a sub-table path an earlier [[array]] entry defined with a header.
  • Patching: replacing a value two or more keys below its nearest [table] header no longer produces invalid TOML when a sibling header still extends the table in between.
  • Patching: removing a table no longer deletes the comment directly above the next header.
  • Patching: a new root key-value is inserted above the comment that introduces the first section, instead of between that comment and its header.

Changed

  • Patching: emptying a dotted-key parent now leaves an empty inline table instead of an empty table with header.

Copilot AI balanced review requested due to automatic review settings October 18, 2026 02:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty-array restoration can create incorrect nesting, and nested path lookup introduces quadratic scanning.

2 open issues
What changed in this PR

This PR improves nested TOML table-array patching so changes stay within the owning entry.

Changes:

  • Scope sub-table cleanup and nested path resolution to the correct entry.
  • Preserve parent indices when restoring emptied arrays.
  • Add regression tests and a changelog entry.
File Description
src/​patch.ts Adjusts cleanup scope and empty-array restoration.
src/​find-by-path.ts Adds scoped lookup through nested table arrays.
src/​__tests__/​patch.test.ts Adds three nested-entry regression tests.
CHANGELOG.md Documents the patching fixes.

Review effort: Balanced

Comment thread src/patch.ts Outdated
Comment thread src/find-by-path.ts
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Benchmark comparison

This branch measured against latest in one run on the same runner.
Ratios close to 1.0 are within run to run noise.

Fixture Benchmark Results

Time per iteration. Lower is better.

Parse, smol-toml spec example (toml-spec-example.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 (Date) 8.50 µs/iter 1x
🥈 smol-toml@1.9.1 (Temporal) 12.48 µs/iter 1.47x
🥉 @iarna/toml@3.0.0 21.78 µs/iter 2.56x
4 @decimalturn/toml-patch@3.3.1 (baseline) 34.74 µs/iter 4.09x
5 @decimalturn/toml-patch@3.3.1 (Date, current) 36.63 µs/iter 4.31x 0.95
6 @decimalturn/toml-patch@3.3.1 (Temporal, current) 43.51 µs/iter 5.12x

Parse, smol-toml 5MB mixed (5mb-mixed.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 (Date) 74.67 ms/iter 1x
🥈 smol-toml@1.9.1 (Temporal) 203.02 ms/iter 2.72x
🥉 @decimalturn/toml-patch@3.3.1 (Date, current) 346.06 ms/iter 4.63x 1.00
4 @decimalturn/toml-patch@3.3.1 (baseline) 346.67 ms/iter 4.64x
5 @decimalturn/toml-patch@3.3.1 (Temporal, current) 547.86 ms/iter 7.34x
- @iarna/toml@3.0.0 DNF DNF

Parse, iarna spec example v0.4.0 (0A-spec-01-example-v0.4.0.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 (Date) 47.52 µs/iter 1x
🥈 smol-toml@1.9.1 (Temporal) 59.43 µs/iter 1.25x
🥉 @iarna/toml@3.0.0 136.27 µs/iter 2.87x
4 @decimalturn/toml-patch@3.3.1 (baseline) 200.92 µs/iter 4.23x
5 @decimalturn/toml-patch@3.3.1 (Date, current) 209.57 µs/iter 4.41x 0.96
6 @decimalturn/toml-patch@3.3.1 (Temporal, current) 239.69 µs/iter 5.04x

Stringify, smol-toml spec example (toml-spec-example.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 5.70 µs/iter 1x
🥈 @iarna/toml@3.0.0 18.49 µs/iter 3.25x
🥉 @decimalturn/toml-patch@3.3.1 (baseline) 96.59 µs/iter 16.96x
4 @decimalturn/toml-patch@3.3.1 (current) 98.47 µs/iter 17.29x 0.98

Stringify, smol-toml 5MB mixed (5mb-mixed.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 63.23 ms/iter 1x
🥈 @iarna/toml@3.0.0 226.67 ms/iter 3.58x
🥉 @decimalturn/toml-patch@3.3.1 (baseline) 738.33 ms/iter 11.68x
4 @decimalturn/toml-patch@3.3.1 (current) 739.90 ms/iter 11.70x 1.00

Stringify, iarna spec example v0.4.0 (0A-spec-01-example-v0.4.0.toml)

Library Performance Slowdown Ratio
🥇 smol-toml@1.9.1 23.58 µs/iter 1x
🥈 @iarna/toml@3.0.0 95.42 µs/iter 4.05x
🥉 @decimalturn/toml-patch@3.3.1 (current) 472.95 µs/iter 20.06x 1.00
4 @decimalturn/toml-patch@3.3.1 (baseline) 475.02 µs/iter 20.15x

@DecimalTurn
DecimalTurn marked this pull request as ready for review October 18, 2026 22:15
- Build object replacements under an explicit parent with inlineTableStart 0
  so they stay a single row instead of a [key] section that fell through to
  a dotted rebuild colliding with the existing header.
- Insert replacement [[sections]] where the removed sub-table was, and
  otherwise after the entry's own sections rather than after unrelated
  tables in its scope.
Structural edits below an explicit [table] outside any AOT entry were
rebuilt from the root, emitting an inline table that collided with the
surviving parent header. Replace them inside the deepest explicit parent
instead, inserting [[sections]] where the old sub-table was.

Share the explicit-parent lookup, row replacement and section insertion
helpers with the AOT entry path.
Replacing a value more than one key below the deepest explicit [table]
(or below no explicit table at all) fell through to a rebuild that nested
the implicit intermediates as an inline table, e.g. `args = { deep = … }`,
which a surviving `[route.args.other]` header can't extend.

- Render the explicit-parent replacement row with a dotted key for any
  relative key length, in both the AOT entry and regular table paths.
- With no explicit ancestor, emit a root dotted row when a surviving header
  extends one of the implicit intermediates.
- Share the row re-keying with the AOT tail flattening.
New root key-values were placed directly above the first section header,
between the header and the comment block R2 assigns to it, so the comment
read as introducing the new key. Back up over that block, and route every
root insertion point through rootKeyValueInsertIndex so they agree.
Scoping key-value validation per AOT entry looked the scope up from the
key's full path, so `arr = 2` under `[parent-table]` after
`[[parent-table.arr]]` landed in the array's own entry scope and missed
that the array was already defined (toml-test
invalid/table/overwrite-array-in-parent). Scope by the key's container
instead, like the table header handlers.
@DecimalTurn
DecimalTurn merged commit 36e024e into latest Oct 22, 2026
35 checks passed
@DecimalTurn
DecimalTurn deleted the dev-neset-aot branch October 22, 2026 02:01
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.

2 participants