Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion WEB_TOPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
65 changes: 65 additions & 0 deletions fixtures/laws/W-22/bad/astro-semantics.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
const handler = () => undefined;
---
<!-- expect: astro/jsx-a11y/alt-text -->
<img src="a.png" />
<!-- expect: astro/jsx-a11y/anchor-has-content -->
<a href="/x"></a>
<!-- expect: astro/jsx-a11y/anchor-is-valid -->
<a href="#">go</a>
<!-- expect: astro/jsx-a11y/aria-activedescendant-has-tabindex -->
<div aria-activedescendant="item"></div>
<!-- expect: astro/jsx-a11y/aria-props -->
<div aria-foo="x"></div>
<!-- expect: astro/jsx-a11y/aria-proptypes -->
<div aria-hidden="yes"></div>
<!-- expect: astro/jsx-a11y/aria-role -->
<div role="datepicker"></div>
<!-- expect: astro/jsx-a11y/aria-unsupported-elements -->
<meta charset="UTF-8" aria-hidden="true" />
<!-- expect: astro/jsx-a11y/autocomplete-valid -->
<input type="text" autocomplete="foo" />
<!-- expect: astro/jsx-a11y/click-events-have-key-events, astro/jsx-a11y/no-static-element-interactions -->
<div onClick={handler}>go</div>
<!-- expect: astro/jsx-a11y/heading-has-content -->
<h1></h1>
<!-- expect: astro/jsx-a11y/html-has-lang -->
<html></html>
<!-- expect: astro/jsx-a11y/iframe-has-title -->
<iframe src="a"></iframe>
<!-- expect: astro/jsx-a11y/img-redundant-alt -->
<img src="a.png" alt="image of a dog" />
<!-- expect: astro/jsx-a11y/click-events-have-key-events, astro/jsx-a11y/interactive-supports-focus -->
<div role="button" onClick={handler}>go</div>
<!-- expect: astro/jsx-a11y/label-has-associated-control -->
<label>Name</label>
<!-- expect: astro/jsx-a11y/media-has-caption -->
<video src="a.mp4"></video>
<!-- expect: astro/jsx-a11y/mouse-events-have-key-events, astro/jsx-a11y/no-static-element-interactions -->
<div onMouseOver={handler}></div>
<!-- expect: astro/jsx-a11y/no-access-key -->
<div accesskey="a"></div>
<!-- expect: astro/jsx-a11y/no-autofocus -->
<input autofocus />
<!-- expect: astro/jsx-a11y/no-distracting-elements -->
<marquee>go</marquee>
<!-- expect: astro/jsx-a11y/no-interactive-element-to-noninteractive-role -->
<button role="img">go</button>
<!-- expect: astro/jsx-a11y/click-events-have-key-events, astro/jsx-a11y/no-noninteractive-element-interactions -->
<h2 onClick={handler}>go</h2>
<!-- expect: astro/jsx-a11y/no-noninteractive-element-to-interactive-role -->
<h3 role="button">go</h3>
<!-- expect: astro/jsx-a11y/no-noninteractive-tabindex -->
<article tabindex="0"></article>
<!-- expect: astro/jsx-a11y/no-redundant-roles -->
<button role="button">go</button>
<!-- expect: astro/jsx-a11y/no-static-element-interactions -->
<span onKeyDown={handler}>go</span>
<!-- expect: astro/jsx-a11y/role-has-required-aria-props -->
<div role="checkbox"></div>
<!-- expect: astro/jsx-a11y/role-supports-aria-props -->
<div role="button" aria-checked="true">go</div>
<!-- expect: astro/jsx-a11y/scope -->
<div scope="col"></div>
<!-- expect: astro/jsx-a11y/no-noninteractive-tabindex, astro/jsx-a11y/tabindex-no-positive -->
<div tabindex="1"></div>
16 changes: 16 additions & 0 deletions fixtures/laws/W-22/clean/astro-semantics.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
const handler = () => undefined;
---
<img src="a.png" alt="A mountain at dusk" />
<a href="/home">go</a>
<button type="button" onClick={handler}>go</button>
<label>Name <input type="text" /></label>
<h1>Title</h1>
<iframe src="a" title="A map"></iframe>
<video src="a.mp4"><track kind="captions" src="a.vtt" /></video>
<div role="button" tabindex="0" onClick={handler} onKeyDown={handler}>go</div>
<div role="checkbox" aria-checked="false" tabindex="0"></div>
<input type="text" autocomplete="email" />
<div aria-hidden="true"></div>
<table><tr><th scope="col">A</th></tr></table>
<html lang="en"></html>
47 changes: 47 additions & 0 deletions laws/W-22.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion schema/law.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
}
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/laws/catalog/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 51 additions & 0 deletions src/laws/catalog/rule-classes.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
20 changes: 20 additions & 0 deletions src/laws/catalog/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions src/laws/catalog/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Law> {
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 };
}
2 changes: 2 additions & 0 deletions src/laws/catalog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`). */
Expand Down
15 changes: 15 additions & 0 deletions src/laws/catalog/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions src/laws/catalog/view.ts
Original file line number Diff line number Diff line change
@@ -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<Record<Law["severity"], string>> = {
R: "a break is refuse",
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions src/laws/probe/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
16 changes: 14 additions & 2 deletions src/laws/probe/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Loading
Loading