chore: merge main into dev/1.6 #271
5 changed files with 66 additions and 4 deletions
5
.changeset/format-composite-unicode-blanks.md
Normal file
5
.changeset/format-composite-unicode-blanks.md
Normal file
|
|
@ -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.
|
||||
|
|
@ -0,0 +1 @@
|
|||
Mix @Juice{1} < @Lemon{2}.
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 "<@";
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue