fix(modules): two lint preferred fixes that did not hold (fuzz fix-idempotent) #311

Merged
abiwab merged 1 commit from fix/lint-preferred-fixes into dev/1.6 2026-11-29 11:15:50 +01:00
6 changed files with 45 additions and 12 deletions

View file

@ -0,0 +1,3 @@
.
##
[e]

View file

@ -0,0 +1 @@
@flour ^&flour

View file

@ -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, "@");
}

View file

@ -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."

View file

@ -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({

View file

@ -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(