diff --git a/fuzz/regressions/lint-merged-header-bare-action.gram b/fuzz/regressions/lint-merged-header-bare-action.gram new file mode 100644 index 00000000..9ed12183 --- /dev/null +++ b/fuzz/regressions/lint-merged-header-bare-action.gram @@ -0,0 +1,3 @@ +. +## +[e] \ No newline at end of file diff --git a/fuzz/regressions/lint-reference-after-sigil.gram b/fuzz/regressions/lint-reference-after-sigil.gram new file mode 100644 index 00000000..1305d686 --- /dev/null +++ b/fuzz/regressions/lint-reference-after-sigil.gram @@ -0,0 +1 @@ +@flour ^&flour \ No newline at end of file diff --git a/packages/modules/src/lint/fixes.ts b/packages/modules/src/lint/fixes.ts index e981508a..12854101 100644 --- a/packages/modules/src/lint/fixes.ts +++ b/packages/modules/src/lint/fixes.ts @@ -114,8 +114,11 @@ function quantityFix( } if (w.code === "REFERENCE_TO_INGREDIENT") { - // The range starts at the `&`: an `@` before it makes `@&name`. + // The range starts at the `&`: an `@` before it makes `@&name`. Not + // when an element sigil is glued before it (`^&flour`): the parser + // reads `^@` as text, so no ingredient would come of it. if (!text.startsWith("&")) return null; + if (start > 0 && "@#~^&".includes(ctx.text[start - 1]!)) return null; return fix(`Write ${suggestion} (a raw ingredient)`, start, start, "@"); } diff --git a/packages/modules/src/lint/rules-writing.ts b/packages/modules/src/lint/rules-writing.ts index 25f900bc..36a91c8d 100644 --- a/packages/modules/src/lint/rules-writing.ts +++ b/packages/modules/src/lint/rules-writing.ts @@ -86,15 +86,17 @@ export function lostIngredients( /** `startAction` of the grammar: `[Action]` opening a line. */ const ACTION_LINE = /^[ \t]*\[[^\]\n]+\]/; /** - * Whether `line` can open a block on its own. When it cannot, the blank line - * would turn text the parser reads into a syntax error: a bare `[Action]` - * (nothing after the bracket, or a lone `\r` right after it, which ends a - * line for the parser too), a `## Title ~{5 min}.` with text after its - * anchor. Only run on a line the rule already picked. + * Whether `block` (the line the rule picked and the rest of its step, which + * the blank line turns into a block of its own) still parses. When it does + * not, the blank line would turn text the parser reads into a syntax error: + * a bare `[Action]` (nothing after the bracket, or a lone `\r` right after + * it, which ends a line for the parser too), a `## Title ~{5 min}.` with + * text after its anchor, a bare `##` whose next line is such an `[Action]` + * (the line under a header is read as its first step). */ -function opensBlock(line: string): boolean { +function opensBlock(block: string): boolean { try { - getAST(line); + getAST(block); return true; } catch { return false; @@ -110,9 +112,9 @@ const HEADER_LINE = /^[ \t]*##(?:[ \t]|\r?$)/; * (a section, which vanishes), or any line after one that ends with * `->&name` (the step's output: what follows is another step). A plain line * break inside a step stays legitimate (`A step,\nthe same step.`). The fix, - * preferred, inserts the blank line. A line that cannot open a block on its - * own (`opensBlock`) is left alone, whatever the line above: the blank line - * would only turn it into a parse error. + * preferred, inserts the blank line. A line that cannot open a block with + * the rest of its step (`opensBlock`) is left alone, whatever the line + * above: the blank line would only turn it into a parse error. */ export function mergedStepLines(ctx: RuleContext): RuleDiagnostic[] { const { text } = ctx; @@ -151,7 +153,7 @@ export function mergedStepLines(ctx: RuleContext): RuleDiagnostic[] { if (HEADER_LINE.test(line)) reason = "header"; else if (ACTION_LINE.test(line)) reason = "action"; else if (decl) reason = "intermediate"; - if (!reason || !opensBlock(line)) continue; + if (!reason || !opensBlock(text.slice(start, loc.end))) continue; const message = reason === "header" ? "This '##' line is read as text of the step above, not as a section: leave a blank line before it." diff --git a/packages/modules/tests/lint/lint-document.test.ts b/packages/modules/tests/lint/lint-document.test.ts index f2223aef..ce6b97fe 100644 --- a/packages/modules/tests/lint/lint-document.test.ts +++ b/packages/modules/tests/lint/lint-document.test.ts @@ -110,6 +110,20 @@ describe("lintDocument — what every diagnostic carries", () => { expect(apply(text, fix!)).toContain("~pasta{15 min}"); }); + it("offers no '@' fix for a reference glued to an element sigil", () => { + // `^@&flour` is read as text `^@` then `&flour`: the fix would not help. + const glued = lintSource(`${TITLE}Mix @flour{1}, ^&flour.\n`).find( + (x) => x.code === "REFERENCE_TO_INGREDIENT", + )!; + expect(glued.fixes ?? []).toEqual([]); + const plain = lintSource(`${TITLE}Mix @flour{1}, then &flour.\n`).find( + (x) => x.code === "REFERENCE_TO_INGREDIENT", + )!; + expect( + apply(`${TITLE}Mix @flour{1}, then &flour.\n`, plain.fixes![0]!), + ).toContain("then @&flour."); + }); + it("reports a COMPILE_ERROR at the top of the file", () => { const text = `${TITLE}Mix.\n`; const d = lintDocument({ diff --git a/packages/modules/tests/lint/merged-step-line.test.ts b/packages/modules/tests/lint/merged-step-line.test.ts index 6db672a4..03657624 100644 --- a/packages/modules/tests/lint/merged-step-line.test.ts +++ b/packages/modules/tests/lint/merged-step-line.test.ts @@ -70,6 +70,16 @@ describe("MERGED_STEP_LINE", () => { expect(fixOnce(text)).toBe("Mix @a{1}.\r\n\r\n[Bake] it.\r\n"); }); + it("does not flag a line that could not open a block with the rest of its step", () => { + // `##` alone parses, but its next line `[e]` becomes its first step, + // and a bare action is a parse error: the blank line would break it. + expect(merged(".\n##\n[e]")).toEqual([]); + // With an action that opens a step, both lines are still flagged. + expect( + merged(`${TITLE}Mix.\n## Bake\n[Bake] it.\n`).map((d) => d.data?.reason), + ).toEqual(["header", "action"]); + }); + it("does not flag a plain line break inside a step (the official example)", () => { expect(merged(`${TITLE}A step,\nthe same step.\n`)).toEqual([]); expect(