From 457f1c8ca387b78a0c03a8e7d454380fd032bb73 Mon Sep 17 00:00:00 2001 From: abiwab Date: Thu, 26 Nov 2026 13:13:21 +0100 Subject: [PATCH] fix(format): glue a composite separator surrounded by Unicode blanks --- .changeset/format-composite-unicode-blanks.md | 5 +++ .../269-nbsp-around-composite-separator.gram | 1 + fuzz/src/normalize.ts | 20 ++++++++++++ packages/format/src/index.ts | 13 +++++--- packages/format/tests/formatter.test.ts | 31 +++++++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 .changeset/format-composite-unicode-blanks.md create mode 100644 fuzz/regressions/269-nbsp-around-composite-separator.gram diff --git a/.changeset/format-composite-unicode-blanks.md b/.changeset/format-composite-unicode-blanks.md new file mode 100644 index 00000000..42829c7f --- /dev/null +++ b/.changeset/format-composite-unicode-blanks.md @@ -0,0 +1,5 @@ +--- +"@gram-lang/format": patch +--- + +**Format**: The formatter now glues a composite whose `<` is surrounded by a no-break space or another Unicode blank (`@lemon zest{1} < @lemon{2}`), the same spaced composite that the `SPACED_COMPOSITE` warning flags; it still never joins two lines. diff --git a/fuzz/regressions/269-nbsp-around-composite-separator.gram b/fuzz/regressions/269-nbsp-around-composite-separator.gram new file mode 100644 index 00000000..1c4cf53d --- /dev/null +++ b/fuzz/regressions/269-nbsp-around-composite-separator.gram @@ -0,0 +1 @@ +Mix @Juice{1} < @Lemon{2}. diff --git a/fuzz/src/normalize.ts b/fuzz/src/normalize.ts index c51776f7..05c1d206 100644 --- a/fuzz/src/normalize.ts +++ b/fuzz/src/normalize.ts @@ -76,6 +76,26 @@ export function normalize(value: unknown): unknown { out[key] = v.toLowerCase(); continue; } + // Rule 1 again: the glued parent a SPACED_COMPOSITE suggests + // (`<@Lemon`) quotes it as written, like `parent`. Today rule 6 + // glues every `<` kitchen flags, so I6 skips those inputs; this + // keeps a blank kitchen would flag but rule 6 misses (a no-break + // space before the fix) from reporting a case change as a change + // of meaning. + if ( + key === "data" && + record.code === "SPACED_COMPOSITE" && + v && + typeof v === "object" && + typeof (v as { suggestion?: unknown }).suggestion === "string" + ) { + const data = v as { suggestion: string }; + out[key] = normalize({ + ...data, + suggestion: data.suggestion.toLowerCase(), + }); + continue; + } out[key] = normalize(v); } return out; diff --git a/packages/format/src/index.ts b/packages/format/src/index.ts index 32d35648..d1fb7a7e 100644 --- a/packages/format/src/index.ts +++ b/packages/format/src/index.ts @@ -584,10 +584,15 @@ export function formatGram(source: string): { let body = originalBody; // Rule 6 (from language-server): normalize composite separator spacing - // (@ingA{} < @ingB{} → @ingA{}<@ingB{}). Tabs included, as in the - // grammar's `invalidComposite`: left to rule 9, `<\t@` became `< @` - // and needed a second pass (#204). - body = body.replace(/[ \t]*<[ \t]*@/g, (match) => { + // (@ingA{} < @ingB{} → @ingA{}<@ingB{}). Tabs included: left to rule 9, + // `<\t@` became `< @` and needed a second pass (#204). Unlike + // `trimBlanks` (#200), which strips only the grammar's blanks so as not to + // turn a line of text into a blank line, this repair takes every Unicode + // horizontal blank (a no-break space, U+3000…): kitchen flags + // SPACED_COMPOSITE whatever blank surrounds `<`, so the repair must glue + // what it flags. Never `\s`: the rule runs before the parser finds the + // steps, and a line break taken here would merge two steps or a comment. + body = body.replace(/[^\S\r\n]*<[^\S\r\n]*@/g, (match) => { if (match !== "<@") changes.compositeSeparatorSpacing++; return "<@"; }); diff --git a/packages/format/tests/formatter.test.ts b/packages/format/tests/formatter.test.ts index 1475f329..f6182a1d 100644 --- a/packages/format/tests/formatter.test.ts +++ b/packages/format/tests/formatter.test.ts @@ -107,6 +107,37 @@ describe("formatGram", () => { } }); + // Kitchen flags `<` with any Unicode blank around it (SPACED_COMPOSITE + // trims the text between the two ingredients): the repair glues those + // too, a no-break space or an ideographic space as well as a space. + it("normalizes Unicode horizontal blanks around the composite separator", () => { + for (const src of [ + "Add @Yolks{3} < @Eggs{3}.\n", + "Add @yolks{3} < @eggs{3}.\n", + "Add @yolks{3} < @eggs{3}.\n", + ]) { + const { content, changes } = formatGram(src); + expect(content).toBe("Add @yolks{3}<@eggs{3}.\n"); + expect(changes.compositeSeparatorSpacing).toBe(1); + expect(formatGram(content).content).toBe(content); + } + }); + + // The repair runs before the parser finds the steps: a line break is + // never a blank it takes, or it would merge two steps (or a comment). + it("never glues a composite separator across a line break", () => { + for (const [src, expected] of [ + ["Add @yolks{3}\n< @eggs{3}.\n", "Add @yolks{3}\n<@eggs{3}.\n"], + ["Add @yolks{3} <\n@eggs{3}.\n", "Add @yolks{3} <\n@eggs{3}.\n"], + [ + "Add @yolks{3}.\n\n<\u00a0@eggs{3}.\n", + "Add @yolks{3}.\n\n<@eggs{3}.\n", + ], + ]) { + expect(formatGram(src as string).content).toBe(expected as string); + } + }); + it("does not touch an already-normalized composite separator", () => { const { content, changes } = formatGram("Add @sugar<@eggs{3}.\n"); expect(content).toBe("Add @sugar<@eggs{3}.\n"); -- 2.54.0