diff --git a/.claude/skills/address-bot-reviews/SKILL.md b/.claude/skills/address-bot-reviews/SKILL.md index b8f4bb71f..2a44dbdc7 100644 --- a/.claude/skills/address-bot-reviews/SKILL.md +++ b/.claude/skills/address-bot-reviews/SKILL.md @@ -91,10 +91,8 @@ undo a behaviour the maintainer asked for is a decline, with that intent as the reason. Repo rules are a valid source: Greptile enforces the comment discipline from `CLAUDE.md` and `review-polish`, and those findings stand. -For every valid finding, ask whether a static check could have caught it. If it -is pattern-shaped (a banned import, character, CSS value, or SFC shape), name -the stock or `frontend/eslint-plugin-romm/` rule as a follow-up in the reply. A -bot catching the same thing twice is a missing rule. +A valid finding that is pattern-shaped is a missing check: name one as a +follow-up in the reply (see `review-polish`, "Encode what you fixed twice"). Suggestion blocks are a starting point, not a patch to apply blindly. Check they are complete and not duplicating a call a neighbouring branch already makes. diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index d0ef46ae4..2f0fcaccb 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -92,6 +92,7 @@ lint: " - Replace the em dash with a comma, parentheses, or a separate sentence." }' ${target} success_codes: [0] + batch: true - name: eslint files: - javascript diff --git a/CLAUDE.md b/CLAUDE.md index 7fea2a81d..e86d34d62 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -66,8 +66,8 @@ These live in `.claude/skills/` and carry the detailed rules. Invoke the one tha **Tests travel with code.** New logic gets a test; new endpoints get endpoint tests; new v2 primitives get a Storybook story (+ `play()` if interactive). **Verify before handoff.** Don't say "done" on UI work without testing it in the browser in both themes and all input modalities. See `review-polish`. **English first.** Outside of language files, all code, comments, identifiers, `.md` files, and commit/PR messages are in English. -**No em-dashes.** Never use em-dashes (U+2014) when writing comments or text. Use commas, parentheses, or separate sentences instead. ESLint (`romm/no-emdash-in-comment`) checks comments under `frontend/src/v2`, and Trunk's `no-emdash` checks Markdown and Python comments. An em-dash string literal is code (an empty-value glyph) and stays allowed. -**Prefer a check to a model review.** A mechanical rule (an import, a character, a CSS pattern, an SFC shape) belongs in ESLint, `tsconfig`, or Trunk. When a review finding repeats, propose the stock or `frontend/eslint-plugin-romm/` rule as a named follow-up rather than growing the current PR. +**No em-dashes.** Never use em-dashes (U+2014) when writing comments or text. Use commas, parentheses, or separate sentences instead. An em-dash string literal is code (an empty-value glyph) and stays allowed. +**Prefer a check to a model review.** A mechanical rule (an import, a character, a CSS pattern, an SFC shape) belongs in ESLint, `tsconfig`, or Trunk. When a review finding repeats, propose a check as a named follow-up; see `review-polish`. **Keep comments short.** Comments should be concise, and focus on the "why" rather than the "what" (the code itself is the "what"). A comment is one or two lines; a docstring is one sentence plus `Args:`/`Returns:` when the signature needs it. Three or more lines of prose means you are explaining, not commenting. See `review-polish` for what to cut. **Don't restate the code.** The code is self-documenting, so skip comments that narrate what a reader can already see (which button sits where, that something is disabled when empty, obvious ordering). Comment only non-obvious "why" that the code can't convey on its own. **One home per value.** Before declaring a constant, type, limit, regex, or store getter, search for an existing one and import it. Validation limits live on the model and are imported by endpoints; a type lives in the component that owns it and is exported. Don't add a near-duplicate that differs only by sorting, and don't invent an identity scheme where a foreign key already exists. diff --git a/frontend/eslint-plugin-romm/rules/no-color-literal.js b/frontend/eslint-plugin-romm/rules/no-color-literal.js index 5a5e17aea..340e63ce9 100644 --- a/frontend/eslint-plugin-romm/rules/no-color-literal.js +++ b/frontend/eslint-plugin-romm/rules/no-color-literal.js @@ -1,9 +1,7 @@ // @ts-check -import { sfcStyleBlocks } from "../utils/sfcStyles.js"; +import { reportAt } from "../utils/report.js"; +import { rangeOf, sfcStyleRoots } from "../utils/sfcStyles.js"; -// A declaration follows `{`, `;` or a nested block's `}` and ends at `;` or -// `}`. A selector ends at `{`, so `a:not(#abc) {` is never read as a value. -const DECLARATION = /(?<=[{;}]\s*)(--[\w-]+|[a-z-]+)\s*:([^;{}]*)(?=[;}])/gi; const URL_OR_STRING = /url\([^)]*\)|"[^"]*"|'[^']*'/gi; const COLOR_LITERAL = /#[0-9a-f]{3,8}\b|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|device-cmyk)\(/gi; @@ -25,27 +23,26 @@ export default { create(context) { return { Program() { - const { sourceCode } = context; - for (const block of sfcStyleBlocks(context)) { - for (const decl of block.text.matchAll(DECLARATION)) { - const value = decl[2]; + for (const block of sfcStyleRoots(context)) { + block.root.walkDecls((decl) => { + const value = decl.raws.value?.raw ?? decl.value; const valueStart = - block.start + (decl.index ?? 0) + decl[0].length - value.length; + rangeOf(block, decl)[0] + + decl.prop.length + + (decl.raws.between ?? "").length; const scanned = value.replace(URL_OR_STRING, (u) => " ".repeat(u.length), ); for (const hit of scanned.matchAll(COLOR_LITERAL)) { - const index = valueStart + (hit.index ?? 0); - context.report({ - loc: { - start: sourceCode.getLocFromIndex(index), - end: sourceCode.getLocFromIndex(index + hit[0].length), - }, - messageId: "colorLiteral", - data: { literal: hit[0] }, - }); + reportAt( + context, + valueStart + (hit.index ?? 0), + hit[0].length, + "colorLiteral", + { literal: hit[0] }, + ); } - } + }); } }, }; diff --git a/frontend/eslint-plugin-romm/rules/no-color-literal.test.ts b/frontend/eslint-plugin-romm/rules/no-color-literal.test.ts index fe610119b..7450fab50 100644 --- a/frontend/eslint-plugin-romm/rules/no-color-literal.test.ts +++ b/frontend/eslint-plugin-romm/rules/no-color-literal.test.ts @@ -8,27 +8,24 @@ const error = (literal: string) => ({ ruleTester.run("no-color-literal", rule, { valid: [ - { code: sfc(".a { color: var(--r-color-fg); }"), filename: "A.vue" }, + sfc(".a { color: var(--r-color-fg); }"), { - code: sfc( + ...sfc( ".a { box-shadow: 0 1px 2px color-mix(in srgb, black 40%, transparent); }", ), - filename: "A.vue", }, { - code: sfc( + ...sfc( ".a { background: color-mix(in srgb, var(--r-color-bg) 60%, transparent); }", ), - filename: "A.vue", }, - { code: sfc(".a:not(#abc) { color: currentColor; }"), filename: "A.vue" }, - { code: sfc("#fade { fill: url(#fade); }"), filename: "A.vue" }, + sfc(".a:not(#abc) { color: currentColor; }"), + sfc("#fade { fill: url(#fade); }"), { - code: sfc(".a {\n color: red;\n li:not(#abc) { opacity: 1; }\n}"), - filename: "A.vue", + ...sfc(".a {\n color: red;\n li:not(#abc) { opacity: 1; }\n}"), }, - { code: sfc('.a::before { content: "#123"; }'), filename: "A.vue" }, - { code: sfc("/* was #fff */\n.a { color: white; }"), filename: "A.vue" }, + sfc('.a::before { content: "#123"; }'), + sfc("/* was #fff */\n.a { color: white; }"), { code: 'const fill = "#ffffff";' }, { code: '\n\n', @@ -37,50 +34,39 @@ ruleTester.run("no-color-literal", rule, { ], invalid: [ { - code: sfc(".a { color: #fff; }"), - filename: "A.vue", + ...sfc(".a { color: #fff; }"), errors: [error("#fff")], }, { - code: sfc(".a { --r-color-fg: #ffffff; }"), - filename: "A.vue", + ...sfc(".a { --r-color-fg: #ffffff; }"), errors: [error("#ffffff")], }, { - code: sfc(".a { text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7); }"), - filename: "A.vue", + ...sfc(".a { text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7); }"), errors: [error("rgba(")], }, { - code: sfc( - ".a { color: hsl(0 0% 100%); border-color: oklch(70% 0.1 200); }", - ), - filename: "A.vue", + ...sfc(".a { color: hsl(0 0% 100%); border-color: oklch(70% 0.1 200); }"), errors: [error("hsl("), error("oklch(")], }, { - code: sfc(".a { color: color(display-p3 1 0 0); }"), - filename: "A.vue", + ...sfc(".a { color: color(display-p3 1 0 0); }"), errors: [error("color(")], }, { - code: sfc(".a { color: device-cmyk(0 81% 81% 30%); }"), - filename: "A.vue", + ...sfc(".a { color: device-cmyk(0 81% 81% 30%); }"), errors: [error("device-cmyk(")], }, { - code: sfc(".a { background: linear-gradient(#000, #111); }"), - filename: "A.vue", + ...sfc(".a { background: linear-gradient(#000, #111); }"), errors: [error("#000"), error("#111")], }, { - code: sfc(".a {\n color: #abc;\n}"), - filename: "A.vue", + ...sfc(".a {\n color: #abc;\n}"), errors: [{ ...error("#abc"), line: 5, column: 10 }], }, { - code: sfc(".a {\n @media print { display: none; }\n color: #fff;\n}"), - filename: "A.vue", + ...sfc(".a {\n @media print { display: none; }\n color: #fff;\n}"), errors: [{ ...error("#fff"), line: 6 }], }, { diff --git a/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.js b/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.js index fa02bde3c..ee2199366 100644 --- a/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.js +++ b/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.js @@ -1,5 +1,6 @@ // @ts-check -import { sfcFragment, sfcStyleCommentRanges } from "../utils/sfcStyles.js"; +import { reportAt } from "../utils/report.js"; +import { rangeOf, sfcFragment, sfcStyleRoots } from "../utils/sfcStyles.js"; const EM_DASH = "—"; @@ -15,7 +16,11 @@ function commentRanges(context) { if (c.range) ranges.push(c.range); } for (const c of sfcFragment(context)?.comments ?? []) ranges.push(c.range); - ranges.push(...sfcStyleCommentRanges(context)); + for (const block of sfcStyleRoots(context)) { + block.root.walkComments((c) => { + ranges.push(rangeOf(block, c)); + }); + } return ranges; } @@ -36,21 +41,13 @@ export default { create(context) { return { Program() { - const { sourceCode } = context; - const { text } = sourceCode; + const { text } = context.sourceCode; if (!text.includes(EM_DASH)) return; for (const [start, end] of commentRanges(context)) { const comment = text.slice(start, end); let offset = comment.indexOf(EM_DASH); while (offset !== -1) { - const index = start + offset; - context.report({ - loc: { - start: sourceCode.getLocFromIndex(index), - end: sourceCode.getLocFromIndex(index + 1), - }, - messageId: "emDash", - }); + reportAt(context, start + offset, 1, "emDash"); offset = comment.indexOf(EM_DASH, offset + 1); } } diff --git a/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.test.ts b/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.test.ts index 29c4d5bf2..8324e7768 100644 --- a/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.test.ts +++ b/frontend/eslint-plugin-romm/rules/no-emdash-in-comment.test.ts @@ -28,7 +28,7 @@ ruleTester.run("no-emdash-in-comment", rule, { code: '\n\n', filename: "A.vue", }, - { code: sfc('.a::after { content: "—"; }'), filename: "A.vue" }, + sfc('.a::after { content: "—"; }'), ], invalid: [ { @@ -62,8 +62,7 @@ ruleTester.run("no-emdash-in-comment", rule, { errors: [{ ...error, line: 2 }], }, { - code: sfc('/* Glass — dark */\n.a { content: "—"; }'), - filename: "A.vue", + ...sfc('/* Glass — dark */\n.a { content: "—"; }'), errors: [{ ...error, line: 4 }], }, ], diff --git a/frontend/eslint-plugin-romm/rules/no-layout-media-query.js b/frontend/eslint-plugin-romm/rules/no-layout-media-query.js index 4fc7b55ca..f85a3dc97 100644 --- a/frontend/eslint-plugin-romm/rules/no-layout-media-query.js +++ b/frontend/eslint-plugin-romm/rules/no-layout-media-query.js @@ -1,7 +1,7 @@ // @ts-check -import { sfcStyleBlocks } from "../utils/sfcStyles.js"; +import { reportAt } from "../utils/report.js"; +import { rangeOf, sfcStyleRoots } from "../utils/sfcStyles.js"; -const MEDIA_RULE = /@media\b([^{;]*)/gi; // Whatever is left after removing these is a condition the rule rejects. const ALLOWED_PARTS = /\(\s*prefers-reduced-motion\b[^)]*\)|\b(?:print|screen|all|only|not|and|or)\b|[\s,]/gi; @@ -23,21 +23,17 @@ export default { create(context) { return { Program() { - const { sourceCode } = context; - for (const block of sfcStyleBlocks(context)) { - for (const rule of block.text.matchAll(MEDIA_RULE)) { - if (rule[1].replace(ALLOWED_PARTS, "") === "") continue; - const index = block.start + (rule.index ?? 0); - context.report({ - loc: { - start: sourceCode.getLocFromIndex(index), - end: sourceCode.getLocFromIndex( - index + rule[0].trimEnd().length, - ), - }, - messageId: "layoutMedia", - }); - } + for (const block of sfcStyleRoots(context)) { + block.root.walkAtRules("media", (rule) => { + if (rule.params.replace(ALLOWED_PARTS, "") === "") return; + const prelude = `@${rule.name}${rule.raws.afterName ?? ""}${rule.params}`; + reportAt( + context, + rangeOf(block, rule)[0], + prelude.length, + "layoutMedia", + ); + }); } }, }; diff --git a/frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts b/frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts index bd59aa246..a24a845d7 100644 --- a/frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts +++ b/frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts @@ -6,60 +6,50 @@ const error = { messageId: "layoutMedia" }; ruleTester.run("no-layout-media-query", rule, { valid: [ { - code: sfc( + ...sfc( "@media (prefers-reduced-motion: reduce) { .a { transition: none; } }", ), - filename: "A.vue", }, - { code: sfc("@media print { .a { display: none; } }"), filename: "A.vue" }, + sfc("@media print { .a { display: none; } }"), { - code: sfc( + ...sfc( "@media not print and (prefers-reduced-motion: no-preference) { .a { opacity: 1; } }", ), - filename: "A.vue", }, { - code: sfc( + ...sfc( "@media (prefers-reduced-motion: reduce) or print { .a { gap: 0; } }", ), - filename: "A.vue", }, { - code: sfc('html[data-bp~="xs"] .a { display: none; }'), - filename: "A.vue", + ...sfc('html[data-bp~="xs"] .a { display: none; }'), }, { - code: sfc("/* @media (max-width: 600px) */\n.a { color: red; }"), - filename: "A.vue", + ...sfc("/* @media (max-width: 600px) */\n.a { color: red; }"), }, { code: 'const q = "@media (max-width: 600px)";' }, ], invalid: [ { - code: sfc("@media (max-width: 600px) { .a { display: none; } }"), - filename: "A.vue", + ...sfc("@media (max-width: 600px) { .a { display: none; } }"), errors: [{ ...error, line: 4, column: 1 }], }, { - code: sfc("@media screen and (min-width: 960px) { .a { gap: 0; } }"), - filename: "A.vue", + ...sfc("@media screen and (min-width: 960px) { .a { gap: 0; } }"), errors: [error], }, { - code: sfc( + ...sfc( "@media (prefers-reduced-motion: reduce), (width < 600px) { .a { gap: 0; } }", ), - filename: "A.vue", errors: [error], }, { - code: sfc("@media (hover: hover) { .a:hover { opacity: 1; } }"), - filename: "A.vue", + ...sfc("@media (hover: hover) { .a:hover { opacity: 1; } }"), errors: [error], }, { - code: sfc(".a { @media (max-width: 600px) { gap: 0; } }"), - filename: "A.vue", + ...sfc(".a { @media (max-width: 600px) { gap: 0; } }"), errors: [error], }, ], diff --git a/frontend/eslint-plugin-romm/rules/testing.ts b/frontend/eslint-plugin-romm/rules/testing.ts index 11a41ec28..f7f3d55d1 100644 --- a/frontend/eslint-plugin-romm/rules/testing.ts +++ b/frontend/eslint-plugin-romm/rules/testing.ts @@ -17,6 +17,9 @@ export const ruleTester = new RuleTester({ }, }); -export function sfc(style: string): string { - return `\n\n\n`; +export function sfc(style: string): { code: string; filename: string } { + return { + code: `\n\n\n`, + filename: "A.vue", + }; } diff --git a/frontend/eslint-plugin-romm/utils/report.js b/frontend/eslint-plugin-romm/utils/report.js new file mode 100644 index 000000000..f75b41ed0 --- /dev/null +++ b/frontend/eslint-plugin-romm/utils/report.js @@ -0,0 +1,21 @@ +// @ts-check + +/** + * Report a problem over `length` characters starting at a source offset. + * @param {import("eslint").Rule.RuleContext} context + * @param {number} index + * @param {number} length + * @param {string} messageId + * @param {Record} [data] + */ +export function reportAt(context, index, length, messageId, data) { + const { sourceCode } = context; + context.report({ + loc: { + start: sourceCode.getLocFromIndex(index), + end: sourceCode.getLocFromIndex(index + length), + }, + messageId, + data, + }); +} diff --git a/frontend/eslint-plugin-romm/utils/sfcStyles.js b/frontend/eslint-plugin-romm/utils/sfcStyles.js index 59882c2f5..3e14f9f2b 100644 --- a/frontend/eslint-plugin-romm/utils/sfcStyles.js +++ b/frontend/eslint-plugin-romm/utils/sfcStyles.js @@ -1,11 +1,13 @@ // @ts-check +import postcss from "postcss"; /** - * @typedef {{ text: string, start: number }} StyleBlock + * @typedef {{ root: import("postcss").Root, start: number }} StyleRoot * @typedef {{ type: string, name?: string, range: [number, number], children?: SfcNode[], comments?: SfcNode[] }} SfcNode */ -const CSS_COMMENT = /\/\*[\s\S]*?\*\//g; +/** @type {WeakMap} */ +const parsed = new WeakMap(); /** * The SFC document fragment, or null for files that are not SFCs. @@ -21,52 +23,44 @@ export function sfcFragment(context) { } /** - * The raw text and offset of each `