From 268c8ea7c8b2818f5500ff8f514349b7d64434d2 Mon Sep 17 00:00:00 2001 From: abiwab Date: Mon, 16 Nov 2026 23:18:33 +0100 Subject: [PATCH] fix(format): drop a temperature's space before its trailing zeros --- .changeset/format-temperature-zeros.md | 5 +++++ packages/format/src/index.ts | 21 ++++++++++++--------- packages/format/tests/formatter.test.ts | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 .changeset/format-temperature-zeros.md diff --git a/.changeset/format-temperature-zeros.md b/.changeset/format-temperature-zeros.md new file mode 100644 index 00000000..7b4d0940 --- /dev/null +++ b/.changeset/format-temperature-zeros.md @@ -0,0 +1,5 @@ +--- +"@gram-lang/format": patch +--- + +**Format**: Fixed a temperature written with a space and trailing zeros, such as `^{160.0 °C}`, that took two runs of the formatter to become `^{160°C}`: it is now fixed in one. diff --git a/packages/format/src/index.ts b/packages/format/src/index.ts index 7d936e74..2b634a8f 100644 --- a/packages/format/src/index.ts +++ b/packages/format/src/index.ts @@ -161,6 +161,18 @@ export function formatGram(source: string): { return `{${trimmed}}`; }); + // Rule 5: Remove space before ° in temperature quantities (handles multiple temps per brace). + // 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, (_, inner) => { + const fixed = inner.replace(/(\d+)\s+°/g, (_m: string, n: string) => { + changes.temperatureSpacing++; + return `${n}°`; + }); + return `{${fixed}}`; + }); + // Rule 4: Remove trailing decimal zeros in quantities ({500.0g} → {500g}, {1.50g} → {1.5g}) body = body.replace(/\{(\d+)\.0+([a-zA-Z°%]*)\}/g, (_, n, u) => { changes.trailingZeros++; @@ -171,15 +183,6 @@ export function formatGram(source: string): { return `{${n}${u}}`; }); - // Rule 5: Remove space before ° in temperature quantities (handles multiple temps per brace) - body = body.replace(/\{([^}]*)\}/g, (_, inner) => { - const fixed = inner.replace(/(\d+)\s+°/g, (_m: string, n: string) => { - changes.temperatureSpacing++; - return `${n}°`; - }); - return `{${fixed}}`; - }); - // Rule 6 (from language-server): normalize composite separator spacing // (@ingA{} < @ingB{} → @ingA{}<@ingB{}) body = body.replace(/ *< *@/g, (match) => { diff --git a/packages/format/tests/formatter.test.ts b/packages/format/tests/formatter.test.ts index 18b21c44..6dc690b3 100644 --- a/packages/format/tests/formatter.test.ts +++ b/packages/format/tests/formatter.test.ts @@ -277,3 +277,19 @@ describe("summarizeChanges", () => { expect(summary).toContain("3 tabs converted to spaces"); }); }); + +describe("a temperature with a space and trailing zeros", () => { + it("is fixed in a single run", () => { + const { content, changes } = formatGram("## T\n\nBake at ^{160.0 °C}.\n"); + expect(content).toContain("^{160°C}"); + expect(changes.temperatureSpacing).toBe(1); + expect(changes.trailingZeros).toBe(1); + expect(formatGram(content).content).toBe(content); + }); + + it("keeps the zeros that matter", () => { + expect(formatGram("## T\n\nHeat to ^{37.50 °C}.\n").content).toContain( + "^{37.5°C}", + ); + }); +}); -- 2.54.0