diff --git a/WEB_TOPS.md b/WEB_TOPS.md
index 0bd97c2..b18d191 100644
--- a/WEB_TOPS.md
+++ b/WEB_TOPS.md
@@ -829,7 +829,8 @@ fails the catalog, and so every `laws` command. Fields:
- `classes`, the launch classes that map the law (§5.2), and `conditional_classes`, those among them
that map it only when the project depends on Astro;
- `reader`: `kinds`, from `tl`, `an`, `at` and `tbd` (§8.1), and `text`, the Reader cell of §8.2;
-- `rules`: each tool rule, flag or check the law maps, as a `tool` and a `name`;
+- `rules`: each tool rule, flag or check the law maps, as a `tool` and a `name`, and an optional
+ `classes`, the classes among the law's that the rule applies to (absent: every class the law maps);
- `judged`: `v0.1` for a law ticked in §8.2, `m3` for a law M3 gives a reader, `no` for the rest;
- `rationale`, `fix` and an optional `note`.
diff --git a/fixtures/laws/W-22/bad/astro-semantics.astro b/fixtures/laws/W-22/bad/astro-semantics.astro
new file mode 100644
index 0000000..29d3daa
--- /dev/null
+++ b/fixtures/laws/W-22/bad/astro-semantics.astro
@@ -0,0 +1,65 @@
+---
+const handler = () => undefined;
+---
+
+
+
+
+
+go
+
+
+
+
+
+
+
+
+
+
+
+
+
+go
+
+
+
+
+
+
+
+
+
+go
+
+Name
+
+
+
+
+
+
+
+
+
+go
+
+go
+
+go
+
+go
+
+
+
+go
+
+go
+
+
+
+go
+
+
+
+
diff --git a/fixtures/laws/W-22/clean/astro-semantics.astro b/fixtures/laws/W-22/clean/astro-semantics.astro
new file mode 100644
index 0000000..56093c8
--- /dev/null
+++ b/fixtures/laws/W-22/clean/astro-semantics.astro
@@ -0,0 +1,16 @@
+---
+const handler = () => undefined;
+---
+
+go
+go
+Name
+Title
+
+
+go
+
+
+
+
+
diff --git a/laws/W-22.yaml b/laws/W-22.yaml
index 818eba7..59b1468 100644
--- a/laws/W-22.yaml
+++ b/laws/W-22.yaml
@@ -13,6 +13,53 @@ rules:
- { tool: eslint, name: "jsx-a11y-x/no-static-element-interactions" }
- { tool: eslint, name: "jsx-a11y-x/click-events-have-key-events" }
- { tool: eslint, name: "jsx-a11y-x/anchor-is-valid" }
+ - { tool: eslint, name: "astro/jsx-a11y/alt-text", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/anchor-has-content", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/anchor-is-valid", classes: [astro-site] }
+ - {
+ tool: eslint,
+ name: "astro/jsx-a11y/aria-activedescendant-has-tabindex",
+ classes: [astro-site],
+ }
+ - { tool: eslint, name: "astro/jsx-a11y/aria-props", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/aria-proptypes", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/aria-role", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/aria-unsupported-elements", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/autocomplete-valid", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/click-events-have-key-events", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/heading-has-content", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/html-has-lang", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/iframe-has-title", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/img-redundant-alt", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/interactive-supports-focus", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/label-has-associated-control", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/media-has-caption", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/mouse-events-have-key-events", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/no-access-key", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/no-autofocus", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/no-distracting-elements", classes: [astro-site] }
+ - {
+ tool: eslint,
+ name: "astro/jsx-a11y/no-interactive-element-to-noninteractive-role",
+ classes: [astro-site],
+ }
+ - {
+ tool: eslint,
+ name: "astro/jsx-a11y/no-noninteractive-element-interactions",
+ classes: [astro-site],
+ }
+ - {
+ tool: eslint,
+ name: "astro/jsx-a11y/no-noninteractive-element-to-interactive-role",
+ classes: [astro-site],
+ }
+ - { tool: eslint, name: "astro/jsx-a11y/no-noninteractive-tabindex", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/no-redundant-roles", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/no-static-element-interactions", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/role-has-required-aria-props", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/role-supports-aria-props", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/scope", classes: [astro-site] }
+ - { tool: eslint, name: "astro/jsx-a11y/tabindex-no-positive", classes: [astro-site] }
- { tool: html-validate, name: "a11y" }
- { tool: axe, name: "axe" }
judged: v0.1
diff --git a/schema/law.schema.json b/schema/law.schema.json
index 1a1bcdc..5d99f66 100644
--- a/schema/law.schema.json
+++ b/schema/law.schema.json
@@ -71,7 +71,14 @@
"additionalProperties": false,
"properties": {
"tool": { "type": "string", "minLength": 1 },
- "name": { "type": "string", "minLength": 1 }
+ "name": { "type": "string", "minLength": 1 },
+ "classes": {
+ "description": "The classes in the law's `classes` this rule applies to. Absent: every class the law maps.",
+ "type": "array",
+ "minItems": 1,
+ "uniqueItems": true,
+ "items": { "$ref": "#/$defs/class" }
+ }
}
}
},
diff --git a/src/laws/catalog/catalog.test.ts b/src/laws/catalog/catalog.test.ts
index b359f5c..15973eb 100644
--- a/src/laws/catalog/catalog.test.ts
+++ b/src/laws/catalog/catalog.test.ts
@@ -226,8 +226,9 @@ describe("the rules a law names, for the law probe", () => {
expect(rulesOf("W-26", "stylelint")).toHaveLength(8);
});
- it("names for W-22 the click and anchor rules beside alt, label and static-element rules", () => {
- expect(rulesOf("W-22", "eslint")).toEqual([
+ it("names for W-22 the click and anchor rules beside alt, label and static-element rules, and Astro's own beside them", () => {
+ const own = rulesOf("W-22", "eslint").filter((name) => !name.startsWith("astro/jsx-a11y/"));
+ expect(own).toEqual([
"jsx-a11y-x/alt-text",
"jsx-a11y-x/label-has-associated-control",
"jsx-a11y-x/no-static-element-interactions",
diff --git a/src/laws/catalog/rule-classes.test.ts b/src/laws/catalog/rule-classes.test.ts
new file mode 100644
index 0000000..0441a9b
--- /dev/null
+++ b/src/laws/catalog/rule-classes.test.ts
@@ -0,0 +1,51 @@
+import astro from "eslint-plugin-astro";
+import { join, resolve } from "node:path";
+import { describe, expect, it } from "vitest";
+import { loadCatalog } from "./load.ts";
+
+const ROOT = resolve(import.meta.dirname, "../../..");
+const PREFIX = "astro/jsx-a11y/";
+
+function w22Rules() {
+ const loaded = loadCatalog(join(ROOT, "laws"));
+ if (!loaded.ok) throw new Error(loaded.problems.map((found) => found.message).join("; "));
+ const found = loaded.laws.find((law) => law.id === "W-22");
+ if (found === undefined) throw new Error("no W-22");
+ return found.rules;
+}
+
+const levelOf = (setting: unknown): unknown => (Array.isArray(setting) ? setting[0] : setting);
+
+/** The astro a11y rules its strict config turns on: the plugin says which they are. */
+const ENABLED = astro.configs["flat/jsx-a11y-strict"]
+ .flatMap((entry) => Object.entries(entry.rules ?? {}))
+ .filter(
+ ([name, setting]) => name.startsWith(PREFIX) && ![0, "off"].includes(String(levelOf(setting))),
+ )
+ .map(([name]) => name)
+ .sort();
+
+describe("R106: W-22 maps the astro a11y rules to astro-site only", () => {
+ it("lists exactly the 31 rules the plugin's strict a11y config enables, each for astro-site", () => {
+ const mapped = w22Rules().filter((rule) => rule.name.startsWith(PREFIX));
+ expect(mapped.map((rule) => rule.name).sort()).toEqual(ENABLED);
+ expect(ENABLED).toHaveLength(31);
+ for (const rule of mapped) {
+ expect(rule, rule.name).toMatchObject({ tool: "eslint", classes: ["astro-site"] });
+ }
+ });
+
+ it("leaves the jsx-a11y-x rules, and the built-page checks, for every class the law maps", () => {
+ const others = w22Rules().filter((rule) => !rule.name.startsWith(PREFIX));
+ expect(others.map((rule) => rule.name)).toEqual([
+ "jsx-a11y-x/alt-text",
+ "jsx-a11y-x/label-has-associated-control",
+ "jsx-a11y-x/no-static-element-interactions",
+ "jsx-a11y-x/click-events-have-key-events",
+ "jsx-a11y-x/anchor-is-valid",
+ "a11y",
+ "axe",
+ ]);
+ for (const rule of others) expect(rule.classes, rule.name).toBeUndefined();
+ });
+});
diff --git a/src/laws/catalog/schema.test.ts b/src/laws/catalog/schema.test.ts
index 5eb3520..6584f89 100644
--- a/src/laws/catalog/schema.test.ts
+++ b/src/laws/catalog/schema.test.ts
@@ -86,6 +86,26 @@ describe("validateLaw (G1a: an invalid file fails)", () => {
expect(problemsOf(lawWith({ conditional_classes: [] }))).toHaveLength(1);
});
+ it("accepts a rule's classes among the law's, and fails one that is not, an empty list and a repeat", () => {
+ const withRule = (classes: unknown): unknown =>
+ lawWith({
+ classes: ["web-app", "astro-site"],
+ conditional_classes: undefined,
+ rules: [
+ { tool: "eslint", name: "a/b" },
+ { tool: "eslint", name: "c/d", classes },
+ ],
+ });
+ expect(problemsOf(withRule(["astro-site"]))).toEqual([]);
+ expect(problemsOf(withRule(["ui-components"]))).toEqual([
+ "/rules/1/classes ui-components is not in classes",
+ ]);
+ expect(problemsOf(withRule([]))).toHaveLength(1);
+ expect(problemsOf(withRule(["astro-site", "astro-site"]))).toHaveLength(1);
+ expect(problemsOf(withRule(["cli"]))).toHaveLength(1);
+ expect(problemsOf(withRule("astro-site"))).toHaveLength(1);
+ });
+
it("fails a severity, a judged value and an id the schema does not allow", () => {
expect(problemsOf(lawWith({ severity: "X" }))).toHaveLength(1);
expect(problemsOf(lawWith({ judged: "yes" }))).toHaveLength(1);
diff --git a/src/laws/catalog/schema.ts b/src/laws/catalog/schema.ts
index f9d683f..0bede3e 100644
--- a/src/laws/catalog/schema.ts
+++ b/src/laws/catalog/schema.ts
@@ -11,9 +11,20 @@ const strays = (law: Law): Problem[] =>
.filter((name) => !law.classes.includes(name))
.map((name) => ({ pointer: "/conditional_classes", message: `${name} is not in classes` }));
-/** Validates one parsed law file against `schema/law.schema.json`, and the one rule a schema cannot state. */
+/** A rule's classes must be among the law's: a schema cannot say that either. */
+const ruleStrays = (law: Law): Problem[] =>
+ law.rules.flatMap((rule, index) =>
+ (rule.classes ?? [])
+ .filter((name) => !law.classes.includes(name))
+ .map((name) => ({
+ pointer: `/rules/${String(index)}/classes`,
+ message: `${name} is not in classes`,
+ })),
+ );
+
+/** Validates one parsed law file against `schema/law.schema.json`, and the rules a schema cannot state. */
export function validateLaw(value: unknown): Validated {
if (!validate(value)) return { ok: false, problems: toProblems(validate.errors) };
- const problems = strays(value);
+ const problems = [...strays(value), ...ruleStrays(value)];
return problems.length === 0 ? { ok: true, value } : { ok: false, problems };
}
diff --git a/src/laws/catalog/types.ts b/src/laws/catalog/types.ts
index 8cd395a..6d33770 100644
--- a/src/laws/catalog/types.ts
+++ b/src/laws/catalog/types.ts
@@ -19,6 +19,8 @@ export type Judged = "v0.1" | "m3" | "no";
export interface LawRule {
readonly tool: string;
readonly name: string;
+ /** The classes the rule applies to, among the law's: absent, every class the law maps. */
+ readonly classes?: readonly LawClass[];
}
/** One law, as `laws/W-NN.yaml` holds it (WEB_TOPS.md section 8.3, `schema/law.schema.json`). */
diff --git a/src/laws/catalog/view.test.ts b/src/laws/catalog/view.test.ts
index f3660b8..64ec93c 100644
--- a/src/laws/catalog/view.test.ts
+++ b/src/laws/catalog/view.test.ts
@@ -57,6 +57,21 @@ describe("listText", () => {
});
describe("explainText", () => {
+ it("says which classes a rule applies to when it does not apply to all of the law's", () => {
+ const text = explainText(law("W-22"));
+ expect(text).toContain("\n eslint astro/jsx-a11y/alt-text (astro-site only)\n");
+ expect(text).toContain("\n eslint jsx-a11y-x/alt-text\n");
+ });
+
+ it("lists each class of a rule that names several, comma separated", () => {
+ const base = law("W-22");
+ const text = explainText({
+ ...base,
+ rules: [{ tool: "eslint", name: "some-rule", classes: ["astro-site", "web-app"] }],
+ });
+ expect(text).toContain("\n eslint some-rule (astro-site, web-app only)\n");
+ });
+
it("prints every field of a law, one line each", () => {
const text = explainText(law("W-01"));
const lines = text.split("\n");
diff --git a/src/laws/catalog/view.ts b/src/laws/catalog/view.ts
index 68dd1c8..40f4b0c 100644
--- a/src/laws/catalog/view.ts
+++ b/src/laws/catalog/view.ts
@@ -1,6 +1,6 @@
import { isConditional } from "./query.ts";
import type { Filter } from "./query.ts";
-import type { Judged, Law } from "./types.ts";
+import type { Judged, Law, LawRule } from "./types.ts";
const SEVERITY: Readonly> = {
R: "a break is refuse",
@@ -41,11 +41,15 @@ const classText = (law: Law): string =>
.map((name) => (isConditional(law, name) ? `${name} (${ASTRO_ONLY})` : name))
.join(", ");
+/** A rule that applies to some of the law's classes only says which. */
+const ruleClasses = (rule: LawRule): string =>
+ rule.classes === undefined ? "" : ` (${rule.classes.join(", ")} only)`;
+
/** The rules a law maps: one line each under `rules:`, or `none` after it. */
const rulesText = (law: Law): string =>
law.rules.length === 0
? " none"
- : law.rules.map((rule) => `\n ${rule.tool} ${rule.name}`).join("");
+ : law.rules.map((rule) => `\n ${rule.tool} ${rule.name}${ruleClasses(rule)}`).join("");
/** `laws explain`: every field of section 8.3, one line each, with the reader and rules spelled out. */
export function explainText(law: Law): string {
diff --git a/src/laws/probe/catalog.test.ts b/src/laws/probe/catalog.test.ts
index 19d6013..61573a1 100644
--- a/src/laws/probe/catalog.test.ts
+++ b/src/laws/probe/catalog.test.ts
@@ -57,6 +57,31 @@ describe("G3a the laws the probe proves are the catalog's ticked lint and compil
});
});
+describe("R106 a law's proofs under a class are the rules that apply to it", () => {
+ const law = {
+ id: "W-91",
+ classes: ["ui-components", "astro-site"],
+ proofs: ["jsx-a11y-x/alt-text", "astro/jsx-a11y/alt-text"],
+ scoped: { "astro/jsx-a11y/alt-text": ["astro-site"] },
+ };
+ const proofsOf = (className: string): string[] =>
+ lawsOfClass([law], className).flatMap((found) => found.proofs);
+
+ it("keeps a rule that names classes only for those, and every other rule for every class", () => {
+ expect(proofsOf("astro-site")).toEqual(["jsx-a11y-x/alt-text", "astro/jsx-a11y/alt-text"]);
+ expect(proofsOf("ui-components")).toEqual(["jsx-a11y-x/alt-text"]);
+ });
+
+ it("still maps no law for a class the law does not list", () => {
+ expect(proofsOf("web-app")).toEqual([]);
+ });
+
+ it("takes a law with no scopes as it is", () => {
+ const plain = { id: "W-92", proofs: ["a", "b"] };
+ expect(lawsOfClass([plain], "ui-components")).toEqual([plain]);
+ });
+});
+
const bad: Catalog = {
ok: false,
problems: [
diff --git a/src/laws/probe/catalog.ts b/src/laws/probe/catalog.ts
index a927fd3..9e8ea53 100644
--- a/src/laws/probe/catalog.ts
+++ b/src/laws/probe/catalog.ts
@@ -18,6 +18,18 @@ export function readCatalog(dir: string = CATALOG_DIR, load: typeof loadCatalog
/** The laws the probe proves: the ticked laws a lint rule or a compiler flag judges, read from the catalog. */
export const catalogLaws: LawSource = sourceOf(readCatalog);
-/** The laws a class maps (`classes` in each law's file); a law with no class list is mapped by every class. */
+const mapsClass = (law: ProbeLaw, className: string): boolean =>
+ law.classes === undefined || law.classes.includes(className);
+
+/** The proofs of a law that apply to a class: a rule whose entry names classes (R106) is proven only for those. */
+const proofsFor = (law: ProbeLaw, className: string): ProbeLaw => ({
+ ...law,
+ proofs: law.proofs.filter((proof) => law.scoped?.[proof]?.includes(className) ?? true),
+});
+
+/**
+ * The laws a class maps (`classes` in each law's file; a law with no class list is mapped by every class), each with
+ * the proofs that apply to the class.
+ */
export const lawsOfClass = (list: readonly ProbeLaw[], className: string): ProbeLaw[] =>
- list.filter((law) => law.classes === undefined || law.classes.includes(className));
+ list.filter((law) => mapsClass(law, className)).map((law) => proofsFor(law, className));
diff --git a/src/laws/probe/source.test.ts b/src/laws/probe/source.test.ts
index 4491dd2..f7c2c87 100644
--- a/src/laws/probe/source.test.ts
+++ b/src/laws/probe/source.test.ts
@@ -46,6 +46,7 @@ describe("G3a the laws the probe proves are read from the catalog's records", ()
id: "W-01",
classes: ["ts-library", "web-app"],
proofs: ["@typescript-eslint/no-explicit-any", "noImplicitAny"],
+ scoped: {},
});
expect(isProbed(W01)).toBe(true);
});
@@ -85,6 +86,24 @@ describe("G3a the laws the probe proves are read from the catalog's records", ()
});
});
+describe("R106 a rule whose entry names classes is scoped to them", () => {
+ const record: LawRecord = {
+ id: "W-91",
+ classes: ["ui-components", "astro-site"],
+ judged: "v0.1",
+ rules: [
+ { tool: "eslint", name: "jsx-a11y-x/alt-text" },
+ { tool: "eslint", name: "astro/jsx-a11y/alt-text", classes: ["astro-site"] },
+ { tool: "axe", name: "axe", classes: ["astro-site"] },
+ ],
+ };
+
+ it("lists the classes of each probed rule that has them, and leaves the other rules out", () => {
+ expect(toProbeLaw(record).scoped).toEqual({ "astro/jsx-a11y/alt-text": ["astro-site"] });
+ expect(toProbeLaw(W01).scoped).toEqual({});
+ });
+});
+
describe("G3a the tools whose rules a twin can fire", () => {
const law = (tool: string): LawRecord => ({
id: "W-90",
diff --git a/src/laws/probe/source.ts b/src/laws/probe/source.ts
index d5f97f6..e761c64 100644
--- a/src/laws/probe/source.ts
+++ b/src/laws/probe/source.ts
@@ -5,7 +5,11 @@ export interface LawRecord {
readonly id: string;
readonly classes: readonly string[];
readonly judged: string;
- readonly rules: readonly { readonly tool: string; readonly name: string }[];
+ readonly rules: readonly {
+ readonly tool: string;
+ readonly name: string;
+ readonly classes?: readonly string[];
+ }[];
}
/** The tools whose rules a twin can fire: a lint config, the compiler's flags, and Stylelint. */
@@ -33,11 +37,19 @@ export function isProbed(law: LawRecord): boolean {
return isJudged(law) && rules.length > 0 && !rules.some((rule) => isCustom(rule.name));
}
+/** The classes a rule applies to, by its name: only a rule whose entry has `classes` (R106) is listed. */
+function scopesOf(rules: LawRecord["rules"]): Record {
+ return Object.fromEntries(
+ rules.flatMap((rule) => (rule.classes === undefined ? [] : [[rule.name, rule.classes]])),
+ );
+}
+
/** A law as the probe takes it: what its tool rules and flags name are what its twins must prove. */
export const toProbeLaw = (law: LawRecord): ProbeLaw => ({
id: law.id,
classes: law.classes,
proofs: law.rules.filter(probed).map((rule) => rule.name),
+ scoped: scopesOf(law.rules.filter(probed)),
});
/** The probe's laws out of the catalog's records, in id order. */
diff --git a/src/laws/probe/types.ts b/src/laws/probe/types.ts
index 0f21b5c..afff1ab 100644
--- a/src/laws/probe/types.ts
+++ b/src/laws/probe/types.ts
@@ -13,6 +13,8 @@ export interface ProbeLaw {
readonly proofs: readonly string[];
/** The classes that map the law (WEB_TOPS.md section 5.2); absent for a law every class maps. */
readonly classes?: readonly string[];
+ /** The classes a rule applies to, by proof, where its entry names them (R106); any other proof is proven for every class. */
+ readonly scoped?: Readonly>;
}
/** Where the laws come from. `laws/W-NN.yaml` is the source once it lands; until then, the interim table. */