From 9d391bfa62ae07e73cfd15601a7a9d9e352ce607 Mon Sep 17 00:00:00 2001 From: Stuart Anderson Date: Wed, 23 Sep 2026 12:55:46 -0500 Subject: [PATCH] chore: delegate llm tasks to static analysis tools. Remind robots they have a role to play too --- .claude/skills/address-bot-reviews/SKILL.md | 6 ++ .../skills/frontend-v2-components/SKILL.md | 3 +- .claude/skills/frontend-v2-input/SKILL.md | 2 +- .claude/skills/frontend-v2-theming/SKILL.md | 2 + .claude/skills/review-polish/SKILL.md | 6 ++ CLAUDE.md | 3 +- frontend/eslint-plugin-romm/index.js | 14 ++++ .../rules/no-color-literal.js | 53 ++++++++++++ .../rules/no-color-literal.test.ts | 72 +++++++++++++++++ .../eslint-plugin-romm/rules/no-em-dash.js | 34 ++++++++ .../rules/no-em-dash.test.ts | 30 +++++++ .../rules/no-layout-media-query.js | 45 +++++++++++ .../rules/no-layout-media-query.test.ts | 60 ++++++++++++++ frontend/eslint-plugin-romm/rules/testing.ts | 22 +++++ .../eslint-plugin-romm/utils/sfcStyles.js | 44 ++++++++++ frontend/eslint.config.js | 80 +++++++++++++++++++ frontend/tsconfig.node.json | 8 +- frontend/vitest.config.ts | 6 +- 18 files changed, 485 insertions(+), 5 deletions(-) create mode 100644 frontend/eslint-plugin-romm/index.js create mode 100644 frontend/eslint-plugin-romm/rules/no-color-literal.js create mode 100644 frontend/eslint-plugin-romm/rules/no-color-literal.test.ts create mode 100644 frontend/eslint-plugin-romm/rules/no-em-dash.js create mode 100644 frontend/eslint-plugin-romm/rules/no-em-dash.test.ts create mode 100644 frontend/eslint-plugin-romm/rules/no-layout-media-query.js create mode 100644 frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts create mode 100644 frontend/eslint-plugin-romm/rules/testing.ts create mode 100644 frontend/eslint-plugin-romm/utils/sfcStyles.js diff --git a/.claude/skills/address-bot-reviews/SKILL.md b/.claude/skills/address-bot-reviews/SKILL.md index 5f7cdc91a..41f485d9b 100644 --- a/.claude/skills/address-bot-reviews/SKILL.md +++ b/.claude/skills/address-bot-reviews/SKILL.md @@ -91,6 +91,12 @@ 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), add or +enable the ESLint rule in the fix commit (`frontend/eslint-plugin-romm/` for +custom rules, with a `RuleTester` test), or name the rule as a follow-up in the +reply. A bot catching the same thing twice is a missing rule. + 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/.claude/skills/frontend-v2-components/SKILL.md b/.claude/skills/frontend-v2-components/SKILL.md index 6a971e710..9cc4a4268 100644 --- a/.claude/skills/frontend-v2-components/SKILL.md +++ b/.claude/skills/frontend-v2-components/SKILL.md @@ -45,7 +45,7 @@ If any fails: **shared composite** if generic across features, **feature composi ### Primitive boundaries - **Can use**: tokens, other primitives, Vue, generic composables (`useInput*`, `useFocus*`). -- **Cannot use**: Pinia stores, API services, `emitter`, `router` (a `RouterLink` may be accepted as a prop), `i18n` directly. **No `$t()` in primitives** — text comes via props or slots. +- **Cannot use**: Pinia stores, API services, `emitter`, `router` (a `RouterLink` may be accepted as a prop), `i18n` directly. **No `$t()` in primitives** — text comes via props or slots. ESLint's `@typescript-eslint/no-restricted-imports` block for `src/v2/lib` enforces the import side. Domain knowledge that is not an import (a hardcoded `/assets/...` path, domain-named props) still needs review. - **Chrome labels** are the exception to "via props": the accessible name of a control the primitive renders for itself (a dialog's close button, a chip's remove X, a date field's steppers, a stepper's "Step 2 of 5") is @@ -72,6 +72,7 @@ If any fails: **shared composite** if generic across features, **feature composi - `defineOptions({ inheritAttrs: false })` on every wrapper, paired with `v-bind="$attrs"` and slot passthrough (without the bind, attrs vanish silently). - Props via `defineProps()` (interface), never runtime declarations. Emits via `defineEmits<{...}>()`. Slots with payload via `defineSlots<{}>()`. - Order: `\n\n', + filename: "A.vue", + }, + ], + invalid: [ + { + code: sfc(".a { color: #fff; }"), + filename: "A.vue", + errors: [error("#fff")], + }, + { + code: sfc(".a { --r-color-fg: #ffffff; }"), + filename: "A.vue", + errors: [error("#ffffff")], + }, + { + code: sfc(".a { text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7); }"), + filename: "A.vue", + errors: [error("rgba(")], + }, + { + code: sfc( + ".a { color: hsl(0 0% 100%); border-color: oklch(70% 0.1 200); }", + ), + filename: "A.vue", + errors: [error("hsl("), error("oklch(")], + }, + { + code: sfc(".a { background: linear-gradient(#000, #111); }"), + filename: "A.vue", + errors: [error("#000"), error("#111")], + }, + { + code: sfc(".a {\n color: #abc;\n}"), + filename: "A.vue", + errors: [{ ...error("#abc"), line: 5, column: 10 }], + }, + { + code: `${sfc(".a { color: var(--r-color-fg); }")}\n`, + filename: "A.vue", + errors: [error("#123456")], + }, + ], +}); diff --git a/frontend/eslint-plugin-romm/rules/no-em-dash.js b/frontend/eslint-plugin-romm/rules/no-em-dash.js new file mode 100644 index 000000000..d9891bd3d --- /dev/null +++ b/frontend/eslint-plugin-romm/rules/no-em-dash.js @@ -0,0 +1,34 @@ +// @ts-check + +/** @type {import("eslint").Rule.RuleModule} */ +export default { + meta: { + type: "problem", + docs: { + description: + "Disallow the em dash (U+2014) in source, comments, and templates.", + }, + schema: [], + messages: { + emDash: + "Replace the em dash with a comma, parentheses, or a separate sentence.", + }, + }, + create(context) { + return { + Program() { + const { sourceCode } = context; + for (const match of sourceCode.text.matchAll(/\u2014/g)) { + const index = match.index ?? 0; + context.report({ + loc: { + start: sourceCode.getLocFromIndex(index), + end: sourceCode.getLocFromIndex(index + 1), + }, + messageId: "emDash", + }); + } + }, + }; + }, +}; diff --git a/frontend/eslint-plugin-romm/rules/no-em-dash.test.ts b/frontend/eslint-plugin-romm/rules/no-em-dash.test.ts new file mode 100644 index 000000000..efbe78932 --- /dev/null +++ b/frontend/eslint-plugin-romm/rules/no-em-dash.test.ts @@ -0,0 +1,30 @@ +import rule from "./no-em-dash.js"; +import { ruleTester, sfc } from "./testing"; + +const error = { messageId: "emDash" }; + +ruleTester.run("no-em-dash", rule, { + valid: [ + { code: "// A comma, then (parentheses) - and a hyphen.\nconst a = 1;" }, + { code: 'const range = "1\u20134";' }, + { code: sfc(".a { color: var(--r-color-fg); }"), filename: "A.vue" }, + ], + invalid: [ + { code: "// Loads icons \u2014 slowly.\nconst a = 1;", errors: [error] }, + { code: 'const empty = "\u2014";', errors: [error] }, + { + code: "/** One \u2014 two \u2014 three. */\nexport const x = 1;", + errors: [error, error], + }, + { + code: '\n\n', + filename: "A.vue", + errors: [{ ...error, line: 2 }], + }, + { + code: sfc("/* Glass \u2014 dark */\n.a { color: var(--r-color-fg); }"), + filename: "A.vue", + 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 new file mode 100644 index 000000000..e00b42ad9 --- /dev/null +++ b/frontend/eslint-plugin-romm/rules/no-layout-media-query.js @@ -0,0 +1,45 @@ +// @ts-check +import { sfcStyleBlocks } 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)\b|[\s,]/gi; + +/** @type {import("eslint").Rule.RuleModule} */ +export default { + meta: { + type: "problem", + docs: { + description: + "Disallow @media in SFC styles except prefers-reduced-motion and print; layout switches use html[data-bp~=...].", + }, + schema: [], + messages: { + layoutMedia: + 'Use `html[data-bp~="..."]` selectors (useBreakpoint) for layout. Only `prefers-reduced-motion` and `print` media queries are allowed.', + }, + }, + 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", + }); + } + } + }, + }; + }, +}; 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 new file mode 100644 index 000000000..0b3edb45a --- /dev/null +++ b/frontend/eslint-plugin-romm/rules/no-layout-media-query.test.ts @@ -0,0 +1,60 @@ +import rule from "./no-layout-media-query.js"; +import { ruleTester, sfc } from "./testing"; + +const error = { messageId: "layoutMedia" }; + +ruleTester.run("no-layout-media-query", rule, { + valid: [ + { + code: sfc( + "@media (prefers-reduced-motion: reduce) { .a { transition: none; } }", + ), + filename: "A.vue", + }, + { code: sfc("@media print { .a { display: none; } }"), filename: "A.vue" }, + { + code: sfc( + "@media not print and (prefers-reduced-motion: no-preference) { .a { opacity: 1; } }", + ), + filename: "A.vue", + }, + { + code: sfc('html[data-bp~="xs"] .a { display: none; }'), + filename: "A.vue", + }, + { + code: sfc("/* @media (max-width: 600px) */\n.a { color: red; }"), + filename: "A.vue", + }, + { code: 'const q = "@media (max-width: 600px)";' }, + ], + invalid: [ + { + code: sfc("@media (max-width: 600px) { .a { display: none; } }"), + filename: "A.vue", + errors: [{ ...error, line: 4, column: 1 }], + }, + { + code: sfc("@media screen and (min-width: 960px) { .a { gap: 0; } }"), + filename: "A.vue", + errors: [error], + }, + { + code: 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", + errors: [error], + }, + { + code: sfc(".a { @media (max-width: 600px) { gap: 0; } }"), + filename: "A.vue", + errors: [error], + }, + ], +}); diff --git a/frontend/eslint-plugin-romm/rules/testing.ts b/frontend/eslint-plugin-romm/rules/testing.ts new file mode 100644 index 000000000..00c82f33d --- /dev/null +++ b/frontend/eslint-plugin-romm/rules/testing.ts @@ -0,0 +1,22 @@ +import type { Linter } from "eslint"; +import { RuleTester } from "eslint"; +import vue from "eslint-plugin-vue"; + +const vueParser = vue.configs["flat/base"] + .map((config: Linter.Config) => config.languageOptions?.parser) + .find(Boolean); + +if (!vueParser) + throw new Error("eslint-plugin-vue no longer exposes its parser"); + +export const ruleTester = new RuleTester({ + languageOptions: { + parser: vueParser, + ecmaVersion: 2022, + sourceType: "module", + }, +}); + +export function sfc(style: string, lang = ""): string { + return `\n\n\n`; +} diff --git a/frontend/eslint-plugin-romm/utils/sfcStyles.js b/frontend/eslint-plugin-romm/utils/sfcStyles.js new file mode 100644 index 000000000..1c7d938ef --- /dev/null +++ b/frontend/eslint-plugin-romm/utils/sfcStyles.js @@ -0,0 +1,44 @@ +// @ts-check + +/** + * @typedef {{ text: string, start: number }} StyleBlock + * @typedef {{ type: string, name?: string, range: [number, number], children?: SfcNode[] }} SfcNode + */ + +/** + * The `