From 36327eae8bb0225f23001ab743c8b187966d4e1b Mon Sep 17 00:00:00 2001 From: abiwab Date: Sat, 21 Nov 2026 20:12:24 +0100 Subject: [PATCH] fix(format): keep every formatter rule within its line --- .changeset/formatter-line-breaks.md | 9 +++ fuzz/known-failures.json | 78 +++++-------------- .../197-header-breaks-on-timer.gram | 2 + .../regressions/197-header-swallows-step.gram | 2 + fuzz/regressions/197-headers-merge.gram | 4 + .../197-idempotent-brace-header.gram | 2 + .../197-ingredient-joins-amount.gram | 2 + .../197-ingredient-joins-empty-braces.gram | 2 + .../197-ingredient-joins-unit.gram | 2 + .../197-reference-joins-amount.gram | 5 ++ fuzz/regressions/197-steps-merge.gram | 3 + fuzz/regressions/197-unclosed-brace.gram | 2 + fuzz/tests/pathological.test.ts | 8 +- packages/format/src/index.ts | 14 +++- packages/format/tests/formatter.test.ts | 32 ++++++++ 15 files changed, 96 insertions(+), 71 deletions(-) create mode 100644 .changeset/formatter-line-breaks.md create mode 100644 fuzz/regressions/197-header-breaks-on-timer.gram create mode 100644 fuzz/regressions/197-header-swallows-step.gram create mode 100644 fuzz/regressions/197-headers-merge.gram create mode 100644 fuzz/regressions/197-idempotent-brace-header.gram create mode 100644 fuzz/regressions/197-ingredient-joins-amount.gram create mode 100644 fuzz/regressions/197-ingredient-joins-empty-braces.gram create mode 100644 fuzz/regressions/197-ingredient-joins-unit.gram create mode 100644 fuzz/regressions/197-reference-joins-amount.gram create mode 100644 fuzz/regressions/197-steps-merge.gram create mode 100644 fuzz/regressions/197-unclosed-brace.gram diff --git a/.changeset/formatter-line-breaks.md b/.changeset/formatter-line-breaks.md new file mode 100644 index 00000000..552febc6 --- /dev/null +++ b/.changeset/formatter-line-breaks.md @@ -0,0 +1,9 @@ +--- +"@gram-lang/format": patch +--- + +**Formatter**: `gram format` and format-on-save no longer join text across a line break, which could change a recipe or break it: + - `Add @flour` followed by `{200 g}` on the next line stays an ingredient with no amount, instead of becoming `@flour{200 g}`. + - A `{` left open at the end of a line is left alone, instead of being joined to the next line into a file that no longer parses. + - Braces spread over several lines no longer pull a step into the section header above it or merge two steps. + - Formatting a file full of unclosed braces now takes time in step with its length, instead of growing with its square. diff --git a/fuzz/known-failures.json b/fuzz/known-failures.json index e05f3a03..a0b21216 100644 --- a/fuzz/known-failures.json +++ b/fuzz/known-failures.json @@ -1,64 +1,4 @@ [ - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.activeBreakdown", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "## {\n}" - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.registry.ingredients", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "@m\n{g}" - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].ingredients[]", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "@n\n{1}" - }, - { - "signature": "format-preserves-meaning | formatted source no longer parses | parser:header_trailing", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "## {\n~{0}." - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.miseEnPlace[].duration", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "->&dough\n\n## l\n@t\n{0%&dough}" - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.prepBreakdown[]", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "@s\n{}(n)" - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.schedule.blocks[]", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "~{5m}{\n\n~{2m}" - }, - { - "signature": "format-preserves-meaning | formatted source no longer parses | parser:throwBracesError", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "@r\n{" - }, - { - "signature": "format-idempotent | second pass reported changes on an unchanged output", - "invariant": "format-idempotent", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "{1\n##}" - }, - { - "signature": "format-preserves-meaning | compilation differs after formatting at $.miseEnPlace[].section", - "invariant": "format-preserves-meaning", - "issue": "https://git.gram-lang.org/gram-lang/gram/issues/197", - "example": "## {\n## \n## n\n@r}" - }, { "signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].title", "invariant": "format-preserves-meaning", @@ -142,5 +82,23 @@ "invariant": "format-preserves-meaning", "issue": "https://git.gram-lang.org/gram-lang/gram/issues/207", "example": "@use \"a\" as &a\n//" + }, + { + "signature": "format-preserves-meaning | compilation differs after formatting at $.metrics.activeBreakdown", + "invariant": "format-preserves-meaning", + "issue": "https://git.gram-lang.org/gram-lang/gram/issues/200", + "example": "## S\n" + }, + { + "signature": "format-preserves-meaning | compilation differs after formatting at $.schedule.blocks[]", + "invariant": "format-preserves-meaning", + "issue": "https://git.gram-lang.org/gram-lang/gram/issues/200", + "example": "~{5\"}\n\n~{7'}" + }, + { + "signature": "format-preserves-meaning | compilation differs after formatting at $.sections[].ingredients[]", + "invariant": "format-preserves-meaning", + "issue": "https://git.gram-lang.org/gram-lang/gram/issues/208", + "example": "@l<@N" } ] diff --git a/fuzz/regressions/197-header-breaks-on-timer.gram b/fuzz/regressions/197-header-breaks-on-timer.gram new file mode 100644 index 00000000..2456caf3 --- /dev/null +++ b/fuzz/regressions/197-header-breaks-on-timer.gram @@ -0,0 +1,2 @@ +## { +~{0}. \ No newline at end of file diff --git a/fuzz/regressions/197-header-swallows-step.gram b/fuzz/regressions/197-header-swallows-step.gram new file mode 100644 index 00000000..c2bf48bd --- /dev/null +++ b/fuzz/regressions/197-header-swallows-step.gram @@ -0,0 +1,2 @@ +## { +} \ No newline at end of file diff --git a/fuzz/regressions/197-headers-merge.gram b/fuzz/regressions/197-headers-merge.gram new file mode 100644 index 00000000..26160170 --- /dev/null +++ b/fuzz/regressions/197-headers-merge.gram @@ -0,0 +1,4 @@ +## { +## a +## n +@r} \ No newline at end of file diff --git a/fuzz/regressions/197-idempotent-brace-header.gram b/fuzz/regressions/197-idempotent-brace-header.gram new file mode 100644 index 00000000..66ff3927 --- /dev/null +++ b/fuzz/regressions/197-idempotent-brace-header.gram @@ -0,0 +1,2 @@ +{1 +##} \ No newline at end of file diff --git a/fuzz/regressions/197-ingredient-joins-amount.gram b/fuzz/regressions/197-ingredient-joins-amount.gram new file mode 100644 index 00000000..4b494e6d --- /dev/null +++ b/fuzz/regressions/197-ingredient-joins-amount.gram @@ -0,0 +1,2 @@ +@n +{1} \ No newline at end of file diff --git a/fuzz/regressions/197-ingredient-joins-empty-braces.gram b/fuzz/regressions/197-ingredient-joins-empty-braces.gram new file mode 100644 index 00000000..9959066a --- /dev/null +++ b/fuzz/regressions/197-ingredient-joins-empty-braces.gram @@ -0,0 +1,2 @@ +@s +{}(n) \ No newline at end of file diff --git a/fuzz/regressions/197-ingredient-joins-unit.gram b/fuzz/regressions/197-ingredient-joins-unit.gram new file mode 100644 index 00000000..16358263 --- /dev/null +++ b/fuzz/regressions/197-ingredient-joins-unit.gram @@ -0,0 +1,2 @@ +@m +{g} \ No newline at end of file diff --git a/fuzz/regressions/197-reference-joins-amount.gram b/fuzz/regressions/197-reference-joins-amount.gram new file mode 100644 index 00000000..81af98d3 --- /dev/null +++ b/fuzz/regressions/197-reference-joins-amount.gram @@ -0,0 +1,5 @@ +->&dough + +## l +@t +{0%&dough} \ No newline at end of file diff --git a/fuzz/regressions/197-steps-merge.gram b/fuzz/regressions/197-steps-merge.gram new file mode 100644 index 00000000..fbb850e3 --- /dev/null +++ b/fuzz/regressions/197-steps-merge.gram @@ -0,0 +1,3 @@ +~{5m}{ + +~{2m} \ No newline at end of file diff --git a/fuzz/regressions/197-unclosed-brace.gram b/fuzz/regressions/197-unclosed-brace.gram new file mode 100644 index 00000000..1a8f097c --- /dev/null +++ b/fuzz/regressions/197-unclosed-brace.gram @@ -0,0 +1,2 @@ +@r +{ \ No newline at end of file diff --git a/fuzz/tests/pathological.test.ts b/fuzz/tests/pathological.test.ts index 4f2fa45b..ce1eb12e 100644 --- a/fuzz/tests/pathological.test.ts +++ b/fuzz/tests/pathological.test.ts @@ -61,13 +61,7 @@ const NOISE_FLOOR_MS = 20; * removed from here. `scale` shrinks both sizes of a case too slow to run * at full size (its exponent does not depend on it). */ -const KNOWN_SLOW: Record = { - // formatGram rule 3 rescans to the end of the input from every unclosed `{`. - "nested opening braces": { - issue: "https://git.gram-lang.org/gram-lang/gram/issues/202", - scale: 1 / 4, - }, -}; +const KNOWN_SLOW: Record = {}; function exponents(generate: (n: number) => string, scale: number): string[] { const small = Math.round(SMALL * scale); diff --git a/packages/format/src/index.ts b/packages/format/src/index.ts index 8e9d0371..a34ef264 100644 --- a/packages/format/src/index.ts +++ b/packages/format/src/index.ts @@ -239,14 +239,20 @@ export function formatGram(source: string): { return lower; }); + // No rule below crosses a line break: a line break is syntax in gram (it + // ends a header, and a blank line ends a step), so `@flour` followed by + // `{200 g}` on the next line is an ingredient with no amount, then text. + // Joining them would give flour an amount; trimming a `{` whose `}` is + // lines below would pull a step into a header, or merge two steps. + // Rule 2: Remove space between @id and opening brace (@ing {10g} → @ing{10g}) - body = body.replace(/@([a-zA-Z][a-zA-Z0-9_-]*)\s+\{/g, (_, id) => { + body = body.replace(/@([a-zA-Z][a-zA-Z0-9_-]*)[ \t]+\{/g, (_, id) => { changes.spacesBeforeBrace++; return `@${id}{`; }); // Rule 3: Trim spaces inside braces ({ 10g } → {10g}) - body = body.replace(/\{([^}]*)\}/g, (_, inner) => { + body = body.replace(/\{([^{}\n]*)\}/g, (_, inner) => { const trimmed = inner.trim(); if (trimmed !== inner) changes.spacesInsideBraces++; return `{${trimmed}}`; @@ -256,7 +262,7 @@ export function formatGram(source: string): { // Runs before rule 4, whose patterns allow no space before the unit: in // the other order `{160.0 °C}` only became `{160.0°C}`, and a second run // was needed to drop the zero. - body = body.replace(/\{([^}]*)\}/g, (match, inner, offset) => { + body = body.replace(/\{([^{}\n]*)\}/g, (match, inner, offset) => { if (isCheckpoint(body, offset)) return match; const fixed = inner.replace(/(\d+)\s+°/g, (_m: string, n: string) => { changes.temperatureSpacing++; @@ -300,7 +306,7 @@ export function formatGram(source: string): { // Rule 7 (from language-server): normalize ->&name {} → ->&name{} // (handles single and multi-word names) - body = body.replace(/->&([^{\n]+?)\s+\{\}/g, (_, name) => { + body = body.replace(/->&([^{\n]+?)[ \t]+\{\}/g, (_, name) => { changes.arrowDeclarationSpacing++; return `->&${name}{}`; }); diff --git a/packages/format/tests/formatter.test.ts b/packages/format/tests/formatter.test.ts index 7ab0c846..5d1f76c6 100644 --- a/packages/format/tests/formatter.test.ts +++ b/packages/format/tests/formatter.test.ts @@ -519,3 +519,35 @@ describe("trailing zeros, the same rule for every quantity", () => { }); } }); + +describe("no rule crosses a line break (#197)", () => { + for (const source of [ + // An ingredient with no amount, then text: not `@flour{200 g}`. + "Add @flour\n{200 g}.\n", + // A `{` left open on its line: `@r{` would no longer parse. + "Mix @r\n{\n", + // The header ends at its line; the step below stays a step. + "## {\n}\n", + // Two steps stay two steps. + "~{5m}{\n\n~{2m}\n", + "Shape ->&dough\n{}\n", + ]) { + it(`leaves ${JSON.stringify(source)} as written`, () => { + const result = formatGram(source); + expect(result.content).toBe(source); + expect(hasChanges(result.changes)).toBe(false); + }); + } + + it("trims a space before the line break, not the line break", () => { + expect(formatGram("Add @flour \n{200 g}.\n").content).toBe( + "Add @flour\n{200 g}.\n", + ); + }); + + it("still formats the braces of each line", () => { + expect( + formatGram("Add @flour {200 g}\nand @salt{ 2.0 g }.\n").content, + ).toBe("Add @flour{200 g}\nand @salt{2 g}.\n"); + }); +}); -- 2.54.0