diff --git a/CHANGELOG.md b/CHANGELOG.md index 541937b4..4e659644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Patching: correctly replace and delete tables split across dotted keys and headers, including nested inline-table paths. ([#335]) +- Patching: keep nested sub-table replacements scoped to the selected array-of-tables entry and prevent table redefinition ([27a02a7]). + ## [3.3.2] - 2026-10-31 ### Fixed @@ -530,5 +535,7 @@ This first forked version from [timhall/toml-patch](https://github.com/timhall/t [#329]: https://github.com/DecimalTurn/toml-patch/pull/329 [#332]: https://github.com/DecimalTurn/toml-patch/pull/332 [#333]: https://github.com/DecimalTurn/toml-patch/pull/333 +[#335]: https://github.com/DecimalTurn/toml-patch/issues/335 [0e66e68]: https://github.com/DecimalTurn/toml-patch/commit/0e66e68cbf42a07bc44445e46c3ea7bea97f95c1 [f97d571]: https://github.com/DecimalTurn/toml-patch/commit/f97d57194952b6603f0fcd686c32d3ae52a2c903 +[27a02a7]: https://github.com/DecimalTurn/toml-patch/commit/27a02a746533d82026fe6f1fb6771f8e1ba5ac27 \ No newline at end of file diff --git a/docs/Fuzz-Testing.md b/docs/Fuzz-Testing.md index 3efc738c..9bdc3d1c 100644 --- a/docs/Fuzz-Testing.md +++ b/docs/Fuzz-Testing.md @@ -107,7 +107,33 @@ general randomizer rarely creates the AOT ownership layouts it targets. Its default is one mutation per seed. Every four consecutive seeds cover all four mutation classes, while keeping failures easy to reproduce. Passing `--mutations 3` applies additional deterministic mutations after the - seed-selected first operation. + seed-selected first operation. It does not generate dotted-key assignments + or inline tables, delete arbitrary table properties, or replace a table + property with a scalar. Those are the key shapes and operations in + [issue #335](https://github.com/DecimalTurn/toml-patch/issues/335). +- **Latest clean sweep:** On 2026-11-14, seeds 0 through 100,000 inclusive + (100,001 seeds, one mutation per seed, `--fast-fail`) completed with zero + failures. + +### Variant 6: deletion and structural replacement fuzzing + +- **Implementation:** `src/__tests__/fuzz-patch6.ts` +- **Runner:** `scripts/fuzz-run6.ts`, `pnpm run fuzz6` +- **Purpose:** Generates block tables with dotted-key assignments, tables with + nested AOT descendants, and inline tables with overlapping dotted paths. The + seed rotates among replacing a header-defined table with a scalar, deleting + a table that owns an AOT descendant, and replacing a nested inline-table + path. +- **Why it exists:** These source shapes and edits target the failures reported + in [issue #335](https://github.com/DecimalTurn/toml-patch/issues/335), which + the AOT-only generator in variant 5 cannot produce. +- **Reproducing the three targeted cases:** Seeds 0, 1 and 2 select each case + once. Use `--to 2` to run all three without stopping at the first failure. +- **Coverage boundary:** Variant 6 focuses on structural deletion and + replacement cases. A clean run is evidence only for the generated source + shapes and mutations, not a proof that every structural edit is correct. +- **Latest clean sweep:** On 2026-11-14, seeds 0 through 100,000 inclusive + (100,001 seeds, one mutation per seed) passed after rebasing onto `fix-335`. ### Patch-lite fuzzing @@ -224,7 +250,8 @@ pnpm run fuzz5 The default is one mutation per seed, so each failure points to the seed's selected operation. Passing `--mutations 3` to `scripts/fuzz-run5.ts` applies -two more deterministic AOT mutations after that first operation. +two more deterministic AOT mutations after that first operation. Pass +`--progress N` to report the scanned seed range every N seeds. This runner behavior differs from the composable profiles above: `aot` and `aot-format` always remove an AOT entry first, while `aot-general` uses the @@ -515,4 +542,3 @@ The sweep-notes doc **must be committed** too — write it to `docs: add sweep notes for -` message. A *clean* window (0 failures) gets no notes file, but a window that needed fixes gets one, listing each seed, its distilled minimal repro, and the root cause + fix commit. - diff --git a/package.json b/package.json index 8cdd6220..d42cbe3c 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "fuzz3": "npx -y tsx scripts/fuzz-run3.ts --seed 0 --to 9999 --mutations 3", "fuzz4": "npx -y tsx scripts/fuzz-run4.ts --seed 0 --to 9999 --mutations 3", "fuzz5": "npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --progress 100", + "fuzz6": "npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 --progress 100", "bench:bundle-size": "node benchmark/bundle-size.mjs", "bench:parse": "node benchmark/parse-benchmark.mjs", "bench:stringify": "node benchmark/stringify-benchmark.mjs", diff --git a/scripts/fuzz-run5.ts b/scripts/fuzz-run5.ts index 1e15d78b..18f0bf9f 100644 --- a/scripts/fuzz-run5.ts +++ b/scripts/fuzz-run5.ts @@ -2,6 +2,8 @@ // // Run a range: // npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 +// Report progress every 100 seeds: +// npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --progress 100 // Stop at the first failure: // npx -y tsx scripts/fuzz-run5.ts --seed 0 --to 9999 --mutations 1 --fast-fail import { fuzzOne5 } from '../src/__tests__/fuzz-patch5'; @@ -16,13 +18,21 @@ const hasFlag = (name: string) => args.includes(`--${name}`); const seed = param('seed', 0); const to = param('to', seed); const mutations = param('mutations', 1); +const progress = param('progress', 0); const fastFail = hasFlag('fast-fail'); const failures: string[] = []; let scanned = 0; +if (!Number.isInteger(progress) || progress < 0) { + throw new Error('--progress must be a non-negative integer.'); +} + for (let current = seed; current <= to; current++) { scanned++; const result = fuzzOne5(current, mutations); + if (progress > 0 && scanned % progress === 0) { + console.log(`PROGRESS: scanned ${scanned} seeds (${seed}..${current})`); + } if (result.status !== 'ok') { const line = `SEED ${current} : ${result.status}${result.error ? ` | ${result.error}` : ''}`; failures.push(line); diff --git a/scripts/fuzz-run6.ts b/scripts/fuzz-run6.ts new file mode 100644 index 00000000..c21de9d2 --- /dev/null +++ b/scripts/fuzz-run6.ts @@ -0,0 +1,44 @@ +// Fuzz-runner variant for dotted-key/header overlaps and structural replacements. +// +// Run an inclusive range: +// npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 +// Stop at the first failure: +// npx -y tsx scripts/fuzz-run6.ts --seed 0 --to 9999 --mutations 1 --fast-fail +import { fuzzOne6 } from '../src/__tests__/fuzz-patch6'; + +const args = process.argv.slice(2); +const param = (name: string, fallback: number) => { + const index = args.indexOf(`--${name}`); + return index >= 0 ? Number(args[index + 1]) : fallback; +}; +const hasFlag = (name: string) => args.includes(`--${name}`); + +const seed = param('seed', 0); +const to = param('to', seed); +const mutations = param('mutations', 1); +const progress = param('progress', 0); +const fastFail = hasFlag('fast-fail'); +const failures: string[] = []; +let scanned = 0; + +if (!Number.isInteger(progress) || progress < 0) { + throw new Error('--progress must be a non-negative integer.'); +} + +for (let current = seed; current <= to; current++) { + scanned++; + const result = fuzzOne6(current, mutations); + if (progress > 0 && scanned % progress === 0) { + console.log(`PROGRESS: scanned ${scanned} seeds (${seed}..${current})`); + } + if (result.status !== 'ok') { + const line = `SEED ${current} : ${result.status}${result.error ? ` | ${result.error.split('\n')[0]}` : ''}`; + failures.push(line); + console.log(line); + if (fastFail) break; + } +} + +const end = seed + scanned - 1; +console.log(`FAILURES: ${failures.length} (scanned ${scanned} seeds: ${seed}..${end})`); +if (failures.length > 0) process.exit(1); diff --git a/scripts/report-dependents.mjs b/scripts/report-dependents.mjs index b3c36e1a..2bbcb534 100644 --- a/scripts/report-dependents.mjs +++ b/scripts/report-dependents.mjs @@ -34,7 +34,13 @@ * The npm requests require network access. A names file is useful when the * npm dependents page is unavailable or when the report should cover a fixed * set of packages. Use `--refresh-names` to require a live npm refresh instead - * of falling back to cached names when npm is unavailable. + * of falling back to cached names when npm is unavailable. If the saved HTML + * page is stale, open the Dependents page in a browser, wait for it to finish + * loading, and save it again as to: + * + * docs/dependents/@decimalturn_toml-patch - npm.html + * + * Page URL: https://www.npmjs.com/package/@decimalturn/toml-patch?activeTab=dependents */ import { readFile, writeFile } from 'node:fs/promises'; import { execFile } from 'node:child_process'; diff --git a/src/__tests__/fuzz-patch6.ts b/src/__tests__/fuzz-patch6.ts new file mode 100644 index 00000000..c28d0bbf --- /dev/null +++ b/src/__tests__/fuzz-patch6.ts @@ -0,0 +1,176 @@ +/** + * Structure-first patch fuzzer for deletions and table replacements. + * + * Each seed targets dotted-key/header overlap, deleting a table that owns an + * AOT descendant, or replacing a nested inline-table path. + */ +import { SeededRandom } from './randomizer'; +import { + deepEqual, + runFuzz, + type FuzzContext, + type GeneratedSource, + type MutationPlan, + type MutationRun, + type PatchFuzzResult +} from './fuzz-patch'; + +type StructuralScenario = + | { + kind: 'replace-header-table'; + path: string[]; + replacement: string; + source: string; + } + | { + kind: 'delete-table-with-aot'; + path: string[]; + source: string; + } + | { + kind: 'replace-inline-table'; + path: string[]; + replacement: number[]; + source: string; + }; + +function createScenario(seed: number, mutationIndex: number): StructuralScenario { + const rng = new SeededRandom(seed + mutationIndex * 100003 + 6000000); + const suffix = `${Math.abs(seed).toString(36)}_${mutationIndex.toString(36)}`; + const scenarioKind = ((seed + mutationIndex) % 3 + 3) % 3; + + if (scenarioKind === 0) { + const root = `table_${suffix}`; + const child = `child_${suffix}`; + const sibling = `sibling_${suffix}`; + const target = `target_${suffix}`; + const replacement = `value_${suffix}_${rng.nextInt(1000)}`; + const source = [ + `[${root}]`, + `${child}.${sibling} = ${JSON.stringify(`keep_${suffix}`)}`, + '', + `[${root}.${child}.${target}]`, + `value = ${rng.nextRange(-999, 999)}` + ].join('\n') + '\n'; + + return { + kind: 'replace-header-table', + path: [root, child, target], + replacement, + source + }; + } + + if (scenarioKind === 1) { + const root = `table_${suffix}`; + const child = `child_${suffix}`; + const sibling = `sibling_${suffix}`; + const aot = `items_${suffix}`; + const source = [ + `[${root}]`, + `${child}.${sibling} = ${JSON.stringify(`keep_${suffix}`)}`, + '', + `[[${root}.${child}.${aot}]]`, + `value = ${rng.nextRange(-999, 999)}` + ].join('\n') + '\n'; + + return { + kind: 'delete-table-with-aot', + path: [root, child], + source + }; + } + + const root = `inline_${suffix}`; + const child = `child_${suffix}`; + const target = `target_${suffix}`; + const left = `left_${suffix}`; + const right = `right_${suffix}`; + const replacement = [rng.nextRange(-99, 99), rng.nextRange(-99, 99)]; + const source = `${root} = { ${child}.${target}.${left} = ${rng.nextRange(-999, 999)}, ${child}.${target}.${right} = ${rng.nextRange(-999, 999)} }\n`; + + return { + kind: 'replace-inline-table', + path: [root, child, target], + replacement, + source + }; +} + +function recordAtPath(value: unknown, path: string[]): Record { + let current = value; + for (let index = 0; index < path.length; index++) { + if (!current || typeof current !== 'object' || Array.isArray(current) || current instanceof Date) { + throw new Error(`Expected a table at ${path.slice(0, index).join('.') || ''}.`); + } + current = (current as Record)[path[index]]; + } + + if (!current || typeof current !== 'object' || Array.isArray(current) || current instanceof Date) { + throw new Error(`Expected a table at ${path.join('.') || ''}.`); + } + return current as Record; +} + +function generateStructuralSource( + { seed, mutationCount }: Pick +): GeneratedSource { + const scenarios = Array.from({ length: mutationCount }, (_, index) => createScenario(seed, index)); + const inlineScenarios = scenarios.filter(scenario => scenario.kind === 'replace-inline-table'); + const sectionScenarios = scenarios.filter(scenario => scenario.kind !== 'replace-inline-table'); + return { + source: [...inlineScenarios, ...sectionScenarios].map(scenario => scenario.source).join('\n'), + description: 'dotted-key, AOT-deletion, and inline-table structural cases' + }; +} + +function mutateStructuralCases({ + updated, + context +}: { + original: unknown; + updated: unknown; + context: FuzzContext; +}): MutationRun { + const descriptions: string[] = []; + const plans: MutationPlan[] = []; + + for (let index = 0; index < context.mutationCount; index++) { + const scenario = createScenario(context.seed, index); + const path = scenario.path; + const parent = recordAtPath(updated, path.slice(0, -1)); + const key = path[path.length - 1]; + + switch (scenario.kind) { + case 'replace-header-table': + parent[key] = scenario.replacement; + descriptions.push(`replace header-defined table with scalar at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path, newValue: scenario.replacement }); + break; + case 'delete-table-with-aot': + delete parent[key]; + descriptions.push(`delete table with nested AOT descendant at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path }); + break; + case 'replace-inline-table': + parent[key] = scenario.replacement; + descriptions.push(`replace inline table subtree with array at ${path.join('.')}`); + plans.push({ kind: scenario.kind, path, newValue: scenario.replacement }); + break; + } + } + + return { updated, descriptions, plans }; +} + +export function fuzzOne6(seed: number, mutationCount: number): PatchFuzzResult { + return runFuzz(seed, { + mutationCount, + generateSource: generateStructuralSource, + generateFormat: () => undefined, + mutate: mutateStructuralCases, + compare: deepEqual, + variant: '6', + profile: 'structural-replacements' + }); +} diff --git a/src/__tests__/fuzz6.test.ts b/src/__tests__/fuzz6.test.ts new file mode 100644 index 00000000..c8d96c11 --- /dev/null +++ b/src/__tests__/fuzz6.test.ts @@ -0,0 +1,30 @@ +import { fuzzOne6 } from './fuzz-patch6'; + +test('fuzz6 rotates through the three targeted structural mutations', () => { + const results = [0, 1, 2].map(seed => fuzzOne6(seed, 1)); + + expect(results.map(result => result.mutationDescs?.[0]?.split(' at ')[0])).toEqual([ + 'replace header-defined table with scalar', + 'delete table with nested AOT descendant', + 'replace inline table subtree with array' + ]); +}); + +test('fuzz6 seed 0 round-trips replacing a header-defined table with a scalar', () => { + expect(fuzzOne6(0, 1).status).toBe('ok'); +}); + +test('fuzz6 seed 1 round-trips deleting a table that owns an AOT descendant', () => { + expect(fuzzOne6(1, 1).status).toBe('ok'); +}); + +test('fuzz6 seed 2 round-trips replacing a nested inline-table path', () => { + expect(fuzzOne6(2, 1).status).toBe('ok'); +}); + +test('fuzz6 seed 0 keeps root inline tables scoped correctly across three mutations', () => { + const result = fuzzOne6(0, 3); + + expect(result.status, result.error).toBe('ok'); + expect(result.mutationDescs).toHaveLength(3); +}); diff --git a/src/__tests__/patch.test.ts b/src/__tests__/patch.test.ts index f6046b63..46f7255e 100644 --- a/src/__tests__/patch.test.ts +++ b/src/__tests__/patch.test.ts @@ -4792,6 +4792,559 @@ describe('sub-table replacement known issues', () => { expect(patched).toBe(expected); }); + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 1: replace a sub-table that has its own [header] with a scalar + test('replaces a sub-table split between a dotted key and a deeper header', () => { + const existing = dedent` + [fruit] + apple.color = "red" + + [fruit.apple.texture] + smooth = true + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple.texture = 'smooth'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + apple.color = "red" + apple.texture = "smooth" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 2: delete a table whose array-of-tables child has its own [[header]] + test('deletes dotted keys and deeper array-of-table headers with their parent table', () => { + const existing = dedent` + [fruit] + apple.color = "red" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit.apple; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 3A: replace a two-level dotted table inside an inline table + test('removes sibling dotted keys when replacing a nested inline-table path', () => { + const existing = 'x = { a.b.c = 1, a.b.d = 2 }\n'; + const updated = parse(existing); + updated.x.a.b = [1, 2]; + + const patched = patch(existing, updated); + const expected = 'x = { a.b = [ 1, 2 ] }\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + + }); + + // See https://github.com/DecimalTurn/toml-patch/issues/335 + // Case 3B: replace a two-level dotted table in a block-table TOML document + test('removes sibling dotted keys when replacing a nested path in a block table', () => { + const existing = dedent` + [x] + a.b.c = 1 + a.b.d = 2 + ` + '\n'; + const updated = parse(existing); + updated.x.a.b = [1, 2]; + + const patched = patch(existing, updated); + const expected = dedent` + [x] + a.b = [ 1, 2 ] + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + // Follow-up ownership and nesting cases for issue #335. + describe('issue 335 follow-up ownership and nesting cases', () => { + + // This test was initially failing due to table redefinition issues. + // Addressed in 27a02a7 + test('replaces a dotted/header-defined sub-table in only the selected AOT entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + const updated = parse(existing); + updated.fruit[0].apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + apple.color = "red" + apple.texture = "matte" + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes a dotted parent and its nested AOT only from the selected entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[0].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [[fruit]] + id = "green" + apple.color = "green" + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces a header-defined table with a dotted key when its parent was defined by dotted keys', () => { + const existing = dedent` + [route] + args.id = "a" + [route.args.deep.leaf] + id = "a" + [route.args.other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + route: { args: { id: string; deep: { leaf: { id: string } } | string; other: { z: number } } }; + }; + updated.route.args.deep = 'none'; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + args.id = "a" + args.deep = "none" + [route.args.other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('quotes a dotted parent when replacing its header-defined child with a dotted key', () => { + const existing = dedent` + ["my.route"] + "my.args".id = "a" + ["my.route"."my.args".deep.leaf] + id = "a" + ["my.route"."my.args".other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + 'my.route': { + 'my.args': { id: string; deep: { leaf: { id: string } } | string; other: { z: number } }; + }; + }; + updated['my.route']['my.args'].deep = 'none'; + + const patched = patch(existing, updated); + const expected = dedent` + ["my.route"] + "my.args".id = "a" + "my.args".deep = "none" + ["my.route"."my.args".other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('replaces multiple header-defined tables with dotted keys under a dotted parent', () => { + const existing = dedent` + [route] + args.id = "a" + [route.args.deep.leaf] + id = "deep" + [route.args.wide.leaf] + id = "wide" + [route.args.other] + z = 1 + ` + '\n'; + + const updated = parse(existing) as { + route: { + args: { + id: string; + deep: { leaf: { id: string } } | string; + wide: { leaf: { id: string } } | number; + other: { z: number }; + }; + }; + }; + updated.route.args.deep = 'none'; + updated.route.args.wide = 2; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + args.id = "a" + args.deep = "none" + args.wide = 2 + [route.args.other] + z = 1 + ` + '\n'; + + expect(parse(patched)).toEqual(updated); + expect(patched).toBe(expected); + }); + + test('deletes a dotted parent and its descendant headers without recreating the table', () => { + const existing = dedent` + [route] + args.deep.token = "secret" + + [route.args.deep.cache] + enabled = true + + [route.meta] + name = "api" + ` + '\n'; + + const updated = parse(existing); + delete updated.route.args; + + const patched = patch(existing, updated); + const expected = dedent` + [route] + + [route.meta] + name = "api" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deleting a scalar removes its dotted and header descendants', () => { + const existing = dedent` + value = 2019-06-13T09:28:26 + value.child.enabled = true + + [value.settings] + mode = "old" + ` + '\n'; + + const updated = parse(existing) as { value?: unknown }; + delete updated.value; + + const patched = patch(existing, updated); + + expect(patched).toBe('\n'); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes dotted and header descendants from the first AOT entry only', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[0].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces dotted inline-table children in one array element without changing siblings', () => { + const existing = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + const updated = parse(existing); + updated.items[0].opts.deep = [1, 2]; + + const patched = patch(existing, updated); + const expected = + 'items = [{ id = "a", opts.deep = [1, 2] }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }]\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces a dotted/header-defined sub-table in the second AOT entry only', () => { + const existing = dedent` + [[fruit]] + id = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + ` + '\n'; + + const updated = parse(existing); + updated.fruit[1].apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit]] + id = "green" + apple.color = "green" + apple.texture = "matte" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes dotted and header-defined descendants only from the selected AOT entry', () => { + const existing = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + apple.color = "green" + + [fruit.apple.texture] + smooth = false + + [[fruit.apple.seeds]] + size = 2 + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit[1].apple; + + const patched = patch(existing, updated); + const expected = dedent` + [[fruit]] + id = "red" + apple.color = "red" + + [fruit.apple.texture] + smooth = true + + [[fruit.apple.seeds]] + size = 1 + + [[fruit]] + id = "green" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces dotted inline-table children in the middle array element only', () => { + const existing = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep.left = 3, opts.deep.right = 4 }, ' + + '{ id = "c", opts.deep.left = 5, opts.deep.right = 6 }]\n'; + const updated = parse(existing); + updated.items[1].opts.deep = [7, 8]; + + const patched = patch(existing, updated); + const expected = + 'items = [{ id = "a", opts.deep.left = 1, opts.deep.right = 2 }, ' + + '{ id = "b", opts.deep = [7, 8] }, ' + + '{ id = "c", opts.deep.left = 5, opts.deep.right = 6 }]\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces a table before an out-of-order sibling header', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple = 'Fuji'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit] + apple = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('replaces an out-of-order nested table without moving its sibling headers', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.apple.texture] + smooth = true + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + updated.fruit.apple.texture = 'matte'; + + const patched = patch(existing, updated); + const expected = dedent` + [fruit.apple] + name = "Fuji" + texture = "matte" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + + test('deletes out-of-order descendants without removing unrelated headers', () => { + const existing = dedent` + [fruit.apple] + name = "Fuji" + [animal] + name = "fox" + [fruit.orange] + name = "Navel" + ` + '\n'; + + const updated = parse(existing); + delete updated.fruit; + + const patched = patch(existing, updated); + const expected = dedent` + [animal] + name = "fox" + ` + '\n'; + + expect(patched).toBe(expected); + expect(parse(patched)).toEqual(updated); + }); + }); + // See https://github.com/DecimalTurn/toml-patch/issues/330 test.skip('separates replacement table array entries the same way whether the old sub-table key was explicit', () => { // Documents a feature not implemented yet: keeping the document's blank-line diff --git a/src/patch.ts b/src/patch.ts index 55f913f3..e32c1361 100644 --- a/src/patch.ts +++ b/src/patch.ts @@ -2356,7 +2356,8 @@ function applyChanges( // sweeps below must look in the REAL container (the AOT entry) or stale // children survive the collapse and re-parse fails with "Value already // defined" (fuzz seed 1674968). Resolve it structurally. - if (containerParent && isKeyValue(containerParent) && + if (containerParent && + (isKeyValue(containerParent) || isInlineItem(containerParent)) && isPrefixMatchedNode(containerParent, change.path.slice(0, -1))) { containerParent = findStructuralParent(original, containerParent); } @@ -2751,6 +2752,27 @@ function applyChanges( const tableParent = findStructuralParent(original, existing) ?? findParent(original, change.path); const aotOwner = findAotEntryForPath(original, change.path); + const explicitParent = findDeepestExplicitTable( + original, + existingTableKey, + aotOwner?.key.item.value.length ?? 0, + aotOwner + ); + if (explicitParent) { + const relativeKey = existingTableKey.slice(explicitParent.key.item.value.length); + const hasImplicitParent = relativeKey.length > 1 && + (explicitParent.items as TreeNode[]).some(item => + isKeyValue(item) && + item.key.value.length > 1 && + item.key.value[0] === relativeKey[0] + ); + if (hasImplicitParent && replaceBelowExplicitTable( + original, explicitParent, existingTableKey, jsValue, format, commentEligibleNodes, aotOwner + )) { + return; + } + } + // The table's key can be a strict prefix of OTHER document sections // (e.g. `[""]` collapsing to a scalar while `["".hv8lx]` is also a // document-level section extending that prefix). Those sections @@ -2785,6 +2807,24 @@ function applyChanges( commentEligibleNodes.add(freshKV); return; } + + const relativeParentKey = parentKey.slice(aotOwner.key.item.value.length); + const hasImplicitAotParent = relativeParentKey.length > 0 && + (aotOwner.items as TreeNode[]).some(item => + isKeyValue(item) && + item.key.value.length > relativeParentKey.length && + keyStartsWith(item.key.value, relativeParentKey) + ); + if (hasImplicitAotParent) { + const relativeKey = existingTableKey.slice(aotOwner.key.item.value.length); + const scopedRow = generateExplicitParentRow(relativeKey, jsValue, format); + if (scopedRow) { + removeMember(original, tableParent, existing); + insert(original, aotOwner, scopedRow, undefined); + commentEligibleNodes.add(scopedRow); + return; + } + } } if (parentKey.length > 0) { @@ -3459,6 +3499,7 @@ function applyChanges( for (const sibling of toRemove) { removeMember(original, parent, sibling); } + removeDocumentItemsExtendingPath(original, change.path, node); } } @@ -5451,7 +5492,8 @@ function replaceBelowExplicitTable( fullKey: string[], jsValue: any, format: TomlFormat, - commentEligibleNodes: WeakSet + commentEligibleNodes: WeakSet, + owner?: TableArray ): boolean { const relativeKey = fullKey.slice(parent.key.item.value.length); @@ -5468,7 +5510,7 @@ function replaceBelowExplicitTable( const sections = Array.from(parseTOML(toTOML(sectionDoc.items, format))); if (!sections.every(item => isTable(item) || isTableArray(item))) return false; - const toRemove = findDocumentItemsByKeyPrefix(document, fullKey); + const toRemove = findAotScopedDocumentItems(document, fullKey, owner); const removedIndex = toRemove.length > 0 ? document.items.indexOf(toRemove[0]) : -1; for (const node of toRemove) removeMember(document, document, node); for (const row of parentRows) removeMember(document, parent, row); @@ -5485,7 +5527,7 @@ function replaceBelowExplicitTable( const row = generateExplicitParentRow(relativeKey, jsValue, format); if (!row) return false; - for (const node of findDocumentItemsByKeyPrefix(document, fullKey)) { + for (const node of findAotScopedDocumentItems(document, fullKey, owner)) { removeMember(document, document, node); } upsertExplicitParentRow(document, parent, relativeKey, row); @@ -5586,7 +5628,7 @@ function removeSiblingsExtendingPrefix( } } -function removeDocumentItemsExtendingPath(document: Document, path: Path, keep: Block): void { +function removeDocumentItemsExtendingPath(document: Document, path: Path, keep: TreeNode): void { const cstPath = stripAotEntryIndices(document, path); const owner = findAotEntryForPath(document, path); for (const item of findAotScopedDocumentItems(document, cstPath, owner)) {