@nifrajs/cli 5.17.0 → 5.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan-sql.d.ts +95 -0
- package/dist/check-scan-sql.d.ts.map +1 -0
- package/dist/check-scan-sql.js +1190 -0
- package/dist/check-scan-sql.js.map +1 -0
- package/dist/check-scan.d.ts +5 -93
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +20 -1207
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +9 -0
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -6
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +249 -80
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +18 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/frontend-guidance.js +1 -1
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/a11y-element.d.ts +33 -0
- package/dist/internal/a11y-element.d.ts.map +1 -0
- package/dist/internal/a11y-element.js +124 -0
- package/dist/internal/a11y-element.js.map +1 -0
- package/dist/internal/a11y-jsx.d.ts +12 -0
- package/dist/internal/a11y-jsx.d.ts.map +1 -0
- package/dist/internal/a11y-jsx.js +42 -0
- package/dist/internal/a11y-jsx.js.map +1 -0
- package/dist/internal/a11y-template.d.ts +11 -0
- package/dist/internal/a11y-template.d.ts.map +1 -0
- package/dist/internal/a11y-template.js +56 -0
- package/dist/internal/a11y-template.js.map +1 -0
- package/dist/internal/aria-data.d.ts +23 -0
- package/dist/internal/aria-data.d.ts.map +1 -0
- package/dist/internal/aria-data.js +220 -0
- package/dist/internal/aria-data.js.map +1 -0
- package/dist/internal/edit-distance.d.ts +3 -0
- package/dist/internal/edit-distance.d.ts.map +1 -0
- package/dist/internal/edit-distance.js +12 -0
- package/dist/internal/edit-distance.js.map +1 -0
- package/dist/internal/imported-as.d.ts +10 -0
- package/dist/internal/imported-as.d.ts.map +1 -0
- package/dist/internal/imported-as.js +23 -0
- package/dist/internal/imported-as.js.map +1 -0
- package/dist/internal/source-facts.d.ts +3 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +2 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/template-markup.d.ts +58 -0
- package/dist/internal/template-markup.d.ts.map +1 -0
- package/dist/internal/template-markup.js +221 -0
- package/dist/internal/template-markup.js.map +1 -0
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +11 -0
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +37 -23
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +5 -3
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +3 -2
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout-config.d.ts +63 -0
- package/dist/migrate-layout-config.d.ts.map +1 -0
- package/dist/migrate-layout-config.js +507 -0
- package/dist/migrate-layout-config.js.map +1 -0
- package/dist/migrate-layout-split.d.ts +46 -0
- package/dist/migrate-layout-split.d.ts.map +1 -0
- package/dist/migrate-layout-split.js +687 -0
- package/dist/migrate-layout-split.js.map +1 -0
- package/dist/migrate-layout.d.ts +4 -24
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +8 -1183
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +78 -10
- package/dist/output-schemas.js.map +1 -1
- package/dist/recipes/5.18.0.d.ts +9 -0
- package/dist/recipes/5.18.0.d.ts.map +1 -0
- package/dist/recipes/5.18.0.js +56 -0
- package/dist/recipes/5.18.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +6 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/a11y.d.ts +5 -0
- package/dist/rules/a11y.d.ts.map +1 -0
- package/dist/rules/a11y.js +78 -0
- package/dist/rules/a11y.js.map +1 -0
- package/dist/rules/codes.d.ts +6 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +6 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +1 -1
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +30 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/host-exposure.d.ts +23 -0
- package/dist/rules/host-exposure.d.ts.map +1 -0
- package/dist/rules/host-exposure.js +380 -0
- package/dist/rules/host-exposure.js.map +1 -0
- package/dist/rules/internal-links.d.ts.map +1 -1
- package/dist/rules/internal-links.js +16 -141
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.d.ts +54 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +577 -80
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/security.d.ts +1 -0
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +34 -7
- package/dist/rules/security.js.map +1 -1
- package/dist/runtime-differences.js +3 -3
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold.d.ts +1 -1
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +2 -2
- package/dist/scaffold.js.map +1 -1
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +2 -19
- package/dist/source-rewrites.js.map +1 -1
- package/docs/examples.json +4 -4
- package/docs/llms-full.txt +422 -42
- package/docs/types.json +466 -56
- package/package.json +12 -12
- package/src/check-diagnostics.ts +11 -0
- package/src/check-scan-sql.ts +1746 -0
- package/src/check-scan.ts +34 -1754
- package/src/check.ts +10 -1
- package/src/cli.ts +258 -98
- package/src/command-catalog.ts +21 -4
- package/src/frontend-guidance.ts +1 -1
- package/src/internal/a11y-element.ts +227 -0
- package/src/internal/a11y-jsx.ts +54 -0
- package/src/internal/a11y-template.ts +74 -0
- package/src/internal/aria-data.ts +242 -0
- package/src/internal/edit-distance.ts +16 -0
- package/src/internal/imported-as.ts +30 -0
- package/src/internal/source-facts.ts +4 -0
- package/src/internal/template-markup.ts +270 -0
- package/src/internal/typescript-import.ts +15 -0
- package/src/mcp-exec.ts +43 -22
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +5 -3
- package/src/mcp-root.ts +1 -1
- package/src/mcp-ws.ts +3 -2
- package/src/migrate-layout-config.ts +615 -0
- package/src/migrate-layout-split.ts +870 -0
- package/src/migrate-layout.ts +37 -1468
- package/src/output-schemas.ts +85 -17
- package/src/recipes/5.18.0.ts +63 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +6 -0
- package/src/rules/a11y.ts +119 -0
- package/src/rules/codes.ts +6 -0
- package/src/rules/comment-markers.ts +2 -2
- package/src/rules/explanations.ts +30 -0
- package/src/rules/host-exposure.ts +531 -0
- package/src/rules/internal-links.ts +21 -154
- package/src/rules/islands.ts +698 -86
- package/src/rules/security.ts +48 -7
- package/src/runtime-differences.ts +3 -3
- package/src/scaffold.ts +3 -3
- package/src/source-rewrites.ts +2 -15
package/src/command-catalog.ts
CHANGED
|
@@ -340,12 +340,14 @@ interface RoutesInput {
|
|
|
340
340
|
}
|
|
341
341
|
|
|
342
342
|
interface ContextInput {
|
|
343
|
+
readonly json?: boolean | undefined
|
|
343
344
|
readonly path?: string | undefined
|
|
344
345
|
readonly kind?: "api" | "pages" | undefined
|
|
345
346
|
readonly dir?: string | undefined
|
|
346
347
|
}
|
|
347
348
|
|
|
348
349
|
interface OpenApiInput {
|
|
350
|
+
readonly json?: boolean | undefined
|
|
349
351
|
readonly format?: "json" | "yaml" | undefined
|
|
350
352
|
readonly path?: string | undefined
|
|
351
353
|
readonly dir?: string | undefined
|
|
@@ -482,6 +484,7 @@ interface ProveInput {
|
|
|
482
484
|
}
|
|
483
485
|
|
|
484
486
|
interface ReplayInput {
|
|
487
|
+
readonly json?: boolean | undefined
|
|
485
488
|
readonly file: string
|
|
486
489
|
readonly dir?: string | undefined
|
|
487
490
|
}
|
|
@@ -734,6 +737,7 @@ const ROUTES_SCHEMA = input<RoutesInput>(
|
|
|
734
737
|
|
|
735
738
|
const CONTEXT_SCHEMA = input<ContextInput>(
|
|
736
739
|
objectSchema({
|
|
740
|
+
json: { type: "boolean" },
|
|
737
741
|
path: { type: "string" },
|
|
738
742
|
kind: { type: "string", enum: ["api", "pages"] },
|
|
739
743
|
dir: { type: "string" },
|
|
@@ -743,6 +747,7 @@ const CONTEXT_SCHEMA = input<ContextInput>(
|
|
|
743
747
|
if (raw.kind !== undefined && raw.kind !== "api" && raw.kind !== "pages")
|
|
744
748
|
throw new TypeError("kind must be api or pages")
|
|
745
749
|
return {
|
|
750
|
+
...parseBooleanFlags(raw, ["json"]),
|
|
746
751
|
path: optionalString(raw.path, "path"),
|
|
747
752
|
kind: raw.kind as "api" | "pages" | undefined,
|
|
748
753
|
...(raw.dir === undefined ? {} : { dir: raw.dir }),
|
|
@@ -752,6 +757,7 @@ const CONTEXT_SCHEMA = input<ContextInput>(
|
|
|
752
757
|
|
|
753
758
|
const OPENAPI_SCHEMA = input<OpenApiInput>(
|
|
754
759
|
objectSchema({
|
|
760
|
+
json: { type: "boolean" },
|
|
755
761
|
format: { type: "string", enum: ["json", "yaml"] },
|
|
756
762
|
path: { type: "string" },
|
|
757
763
|
dir: { type: "string" },
|
|
@@ -761,6 +767,7 @@ const OPENAPI_SCHEMA = input<OpenApiInput>(
|
|
|
761
767
|
if (raw.format !== undefined && raw.format !== "json" && raw.format !== "yaml")
|
|
762
768
|
throw new TypeError("format must be json or yaml")
|
|
763
769
|
return {
|
|
770
|
+
...parseBooleanFlags(raw, ["json"]),
|
|
764
771
|
format: (raw.format ?? "json") as "json" | "yaml",
|
|
765
772
|
path: optionalString(raw.path, "path"),
|
|
766
773
|
...(raw.dir === undefined ? {} : { dir: raw.dir }),
|
|
@@ -1038,12 +1045,18 @@ const PROVE_SCHEMA = input<ProveInput>(
|
|
|
1038
1045
|
)
|
|
1039
1046
|
|
|
1040
1047
|
const REPLAY_SCHEMA = input<ReplayInput>(
|
|
1041
|
-
objectSchema({ file: { type: "string" }, dir: { type: "string" } }, [
|
|
1048
|
+
objectSchema({ file: { type: "string" }, dir: { type: "string" }, json: { type: "boolean" } }, [
|
|
1049
|
+
"file",
|
|
1050
|
+
]),
|
|
1042
1051
|
(value) => {
|
|
1043
1052
|
const raw = withDir(record(value))
|
|
1044
1053
|
const file = optionalString(raw.file, "file")
|
|
1045
1054
|
if (file === undefined || file.trim() === "") throw new TypeError("file is required")
|
|
1046
|
-
return {
|
|
1055
|
+
return {
|
|
1056
|
+
file,
|
|
1057
|
+
...parseBooleanFlags(raw, ["json"]),
|
|
1058
|
+
...(raw.dir === undefined ? {} : { dir: raw.dir }),
|
|
1059
|
+
}
|
|
1047
1060
|
},
|
|
1048
1061
|
)
|
|
1049
1062
|
|
|
@@ -1461,10 +1474,12 @@ const contextSpec: CommandSpec<ContextInput, ContextCommandOutput> = {
|
|
|
1461
1474
|
}),
|
|
1462
1475
|
transports: ["cli", "mcp"],
|
|
1463
1476
|
stability: "stable",
|
|
1477
|
+
cliOnlyFields: ["json"],
|
|
1464
1478
|
argv: {
|
|
1465
1479
|
flags: [
|
|
1466
1480
|
{ name: "path", field: "path", type: "string" },
|
|
1467
1481
|
{ name: "kind", field: "kind", type: "string" },
|
|
1482
|
+
{ name: "json", field: "json", type: "boolean" },
|
|
1468
1483
|
],
|
|
1469
1484
|
},
|
|
1470
1485
|
async run(value, ctx) {
|
|
@@ -1495,12 +1510,13 @@ const openApiSpec: CommandSpec<OpenApiInput, OpenApiCommandOutput> = {
|
|
|
1495
1510
|
flags: [
|
|
1496
1511
|
{ name: "format", field: "format", type: "string" },
|
|
1497
1512
|
{ name: "path", field: "path", type: "string" },
|
|
1513
|
+
{ name: "json", field: "json", type: "boolean" },
|
|
1498
1514
|
],
|
|
1499
1515
|
},
|
|
1500
1516
|
async run(value, ctx) {
|
|
1501
1517
|
const app = await backendAppFor(ctx)
|
|
1502
1518
|
const { renderOpenApiWithTypes } = await import("./openapi-tool.ts")
|
|
1503
|
-
const format = value.format ?? "json"
|
|
1519
|
+
const format = value.json === true ? "json" : (value.format ?? "json")
|
|
1504
1520
|
return {
|
|
1505
1521
|
ok: true,
|
|
1506
1522
|
format,
|
|
@@ -2219,7 +2235,8 @@ const replaySpec: CommandSpec<ReplayInput, ReplayResult> = {
|
|
|
2219
2235
|
output: output({ type: "object" }),
|
|
2220
2236
|
transports: ["cli", "mcp"],
|
|
2221
2237
|
stability: "stable",
|
|
2222
|
-
|
|
2238
|
+
cliOnlyFields: ["json"],
|
|
2239
|
+
argv: { positionals: ["file"], flags: [{ name: "json", field: "json", type: "boolean" }] },
|
|
2223
2240
|
async run(value, ctx) {
|
|
2224
2241
|
const { runReplay } = await import("./replay.ts")
|
|
2225
2242
|
return runReplay(ctx.cwd, value.file)
|
package/src/frontend-guidance.ts
CHANGED
|
@@ -421,7 +421,7 @@ export const FRONTEND_GUIDANCE: readonly GuidanceEntry[] = [
|
|
|
421
421
|
"An imperative defineIsland enhancer has no state primitive, so each update is hand-wired and a list means rebuilding the container.",
|
|
422
422
|
fix: "Mount the island with `bindIsland`: `state(key, fallback)` for state, `data-bind-*` attributes for the markup, `bindList` for a keyed list, `resource` for async. Everything the setup creates stops when the island unmounts.",
|
|
423
423
|
verify:
|
|
424
|
-
"nifra_check runs NF-C047 (a binding names no state or handler), NF-C048 (a binding outside a bindIsland host)
|
|
424
|
+
"nifra_check runs NF-C047 (a binding names no state or handler), NF-C048 (a binding outside a bindIsland host), NF-C022 (a positional list key), NF-C064 (a host no mountIslands map lists), NF-C065 (props that differ from the enhancer's props type) and NF-C066 (a data-bind-attr the runtime refuses) over the island code.",
|
|
425
425
|
seeAlso: "bindIsland state data-bind bindList resource islands",
|
|
426
426
|
category: "seam",
|
|
427
427
|
},
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ABSTRACT_ROLES,
|
|
3
|
+
ARIA_ATTRIBUTES,
|
|
4
|
+
ARIA_VALUES,
|
|
5
|
+
CONCRETE_ROLES,
|
|
6
|
+
IMPLICIT_ROLES,
|
|
7
|
+
MODULE_ROLE_PREFIX,
|
|
8
|
+
NATIVE_FOR_ROLE,
|
|
9
|
+
ROLE_REQUIRED,
|
|
10
|
+
} from "./aria-data.ts"
|
|
11
|
+
import { distance } from "./edit-distance.ts"
|
|
12
|
+
|
|
13
|
+
export type A11yKind =
|
|
14
|
+
| "invalid-role"
|
|
15
|
+
| "abstract-role"
|
|
16
|
+
| "unknown-aria"
|
|
17
|
+
| "invalid-aria-value"
|
|
18
|
+
| "missing-required"
|
|
19
|
+
| "img-alt"
|
|
20
|
+
| "tabindex"
|
|
21
|
+
| "redundant-role"
|
|
22
|
+
| "native-element"
|
|
23
|
+
| "html-lang"
|
|
24
|
+
|
|
25
|
+
/** The two codes the kinds split into: markup assistive technology cannot read, and markup it reads worse. */
|
|
26
|
+
export type A11yCode = "NF-C062" | "NF-C063"
|
|
27
|
+
|
|
28
|
+
export const A11Y_KIND_CODE: Readonly<Record<A11yKind, A11yCode>> = Object.freeze({
|
|
29
|
+
"invalid-role": "NF-C062",
|
|
30
|
+
"abstract-role": "NF-C062",
|
|
31
|
+
"unknown-aria": "NF-C062",
|
|
32
|
+
"invalid-aria-value": "NF-C062",
|
|
33
|
+
"missing-required": "NF-C062",
|
|
34
|
+
"img-alt": "NF-C063",
|
|
35
|
+
tabindex: "NF-C063",
|
|
36
|
+
"redundant-role": "NF-C063",
|
|
37
|
+
"native-element": "NF-C063",
|
|
38
|
+
"html-lang": "NF-C063",
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
/** One accessibility finding in a file, from {@link a11yFindings}. */
|
|
42
|
+
export interface A11yFinding {
|
|
43
|
+
readonly code: A11yCode
|
|
44
|
+
readonly kind: A11yKind
|
|
45
|
+
/** The start of the attribute at fault, or of the tag when the finding is an attribute that is missing. */
|
|
46
|
+
readonly position: number
|
|
47
|
+
/** The start of the element, where a `nifra-expect` comment is written above. */
|
|
48
|
+
readonly expectAt: number
|
|
49
|
+
readonly message: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type AttributeValue =
|
|
53
|
+
| { readonly kind: "text"; readonly text: string }
|
|
54
|
+
| { readonly kind: "other" }
|
|
55
|
+
|
|
56
|
+
/** One attribute an element spells out, by its lowercased name. */
|
|
57
|
+
export interface ElementAttribute {
|
|
58
|
+
/** Where the attribute starts in the file. */
|
|
59
|
+
readonly at: number
|
|
60
|
+
readonly value: AttributeValue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Whether the element's own state stands in for an `aria-*` attribute its role would otherwise need. */
|
|
64
|
+
function suppliesState(
|
|
65
|
+
tag: string,
|
|
66
|
+
attributes: ReadonlyMap<string, ElementAttribute>,
|
|
67
|
+
name: string,
|
|
68
|
+
): boolean {
|
|
69
|
+
if (tag === "input") {
|
|
70
|
+
const type = attributes.get("type")?.value
|
|
71
|
+
// An input whose type is an expression may be any of them.
|
|
72
|
+
if (type?.kind === "other") return true
|
|
73
|
+
const kind = type?.text.trim().toLowerCase()
|
|
74
|
+
if (name === "aria-checked") return kind === "checkbox" || kind === "radio"
|
|
75
|
+
if (name === "aria-valuenow") return kind === "range"
|
|
76
|
+
return name === "aria-expanded" && attributes.has("list")
|
|
77
|
+
}
|
|
78
|
+
if (name === "aria-expanded") return tag === "select"
|
|
79
|
+
if (name === "aria-selected") return tag === "option"
|
|
80
|
+
if (name === "aria-valuenow") return tag === "meter" || tag === "progress"
|
|
81
|
+
return name === "aria-level" && /^h[1-6]$/.test(tag)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const closest = (word: string, among: Iterable<string>): string | undefined => {
|
|
85
|
+
let best: string | undefined
|
|
86
|
+
let bestDistance = 3
|
|
87
|
+
for (const candidate of among) {
|
|
88
|
+
const apart = distance(word, candidate)
|
|
89
|
+
if (apart < bestDistance) {
|
|
90
|
+
best = candidate
|
|
91
|
+
bestDistance = apart
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return best
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const didYouMean = (word: string, among: Iterable<string>): string => {
|
|
98
|
+
const near = closest(word, among)
|
|
99
|
+
return near === undefined ? "" : ` Did you mean "${near}"?`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The findings for one element, from the attributes it spells out. The caller has already left out an
|
|
104
|
+
* element it cannot read in full (a spread may supply or replace any attribute) and any tag that is not
|
|
105
|
+
* a lowercase intrinsic element.
|
|
106
|
+
*/
|
|
107
|
+
export function judgeElement(
|
|
108
|
+
tag: string,
|
|
109
|
+
attributes: ReadonlyMap<string, ElementAttribute>,
|
|
110
|
+
elementStart: number,
|
|
111
|
+
): readonly A11yFinding[] {
|
|
112
|
+
const found: A11yFinding[] = []
|
|
113
|
+
const report = (kind: A11yKind, position: number, message: string): void => {
|
|
114
|
+
found.push({ code: A11Y_KIND_CODE[kind], kind, position, expectAt: elementStart, message })
|
|
115
|
+
}
|
|
116
|
+
const textOf = (name: string): string | undefined => {
|
|
117
|
+
const value = attributes.get(name)?.value
|
|
118
|
+
return value?.kind === "text" ? value.text : undefined
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let role: string | undefined
|
|
122
|
+
const written = attributes.get("role")
|
|
123
|
+
const roleText = textOf("role")
|
|
124
|
+
if (written !== undefined && roleText !== undefined) {
|
|
125
|
+
const tokens = roleText.split(/\s+/).filter((token) => token !== "")
|
|
126
|
+
const usable = tokens.find((token) => CONCRETE_ROLES.has(token))
|
|
127
|
+
if (usable !== undefined) role = usable
|
|
128
|
+
else if (tokens.length > 0 && !tokens.some((token) => MODULE_ROLE_PREFIX.test(token))) {
|
|
129
|
+
const abstract = tokens.find((token) => ABSTRACT_ROLES.has(token))
|
|
130
|
+
if (abstract !== undefined) {
|
|
131
|
+
report(
|
|
132
|
+
"abstract-role",
|
|
133
|
+
written.at,
|
|
134
|
+
`role="${abstract}" is an abstract role: it names a category, and a page may not use it. Use a concrete role such as "button", "checkbox" or "slider".`,
|
|
135
|
+
)
|
|
136
|
+
} else {
|
|
137
|
+
const first = tokens[0] ?? ""
|
|
138
|
+
report(
|
|
139
|
+
"invalid-role",
|
|
140
|
+
written.at,
|
|
141
|
+
`role="${roleText}" names no ARIA role, so assistive technology ignores it.${didYouMean(first, CONCRETE_ROLES)}`,
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const [name, { at, value }] of attributes) {
|
|
148
|
+
if (!name.startsWith("aria-")) continue
|
|
149
|
+
if (!ARIA_ATTRIBUTES.has(name)) {
|
|
150
|
+
report(
|
|
151
|
+
"unknown-aria",
|
|
152
|
+
at,
|
|
153
|
+
`${name} is not an ARIA attribute, so assistive technology ignores it.${didYouMean(name.toLowerCase(), ARIA_ATTRIBUTES)}`,
|
|
154
|
+
)
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
const allowed = ARIA_VALUES.get(name)
|
|
158
|
+
if (allowed === undefined || value.kind !== "text") continue
|
|
159
|
+
if (!allowed.includes(value.text.trim().toLowerCase()))
|
|
160
|
+
report(
|
|
161
|
+
"invalid-aria-value",
|
|
162
|
+
at,
|
|
163
|
+
`"${value.text}" is not a value of ${name}; it takes ${allowed.map((word) => `"${word}"`).join(", ")}.`,
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (role !== undefined && written !== undefined) {
|
|
168
|
+
const required = ROLE_REQUIRED.get(role) ?? []
|
|
169
|
+
for (const name of required)
|
|
170
|
+
if (!attributes.has(name) && !suppliesState(tag, attributes, name))
|
|
171
|
+
report("missing-required", written.at, `role="${role}" requires ${name}.`)
|
|
172
|
+
if (role === "separator" && attributes.has("tabindex") && !attributes.has("aria-valuenow"))
|
|
173
|
+
report("missing-required", written.at, 'a focusable role="separator" requires aria-valuenow.')
|
|
174
|
+
const implicit = IMPLICIT_ROLES.get(tag)
|
|
175
|
+
if (implicit === role && roleText?.trim() === role)
|
|
176
|
+
report(
|
|
177
|
+
"redundant-role",
|
|
178
|
+
written.at,
|
|
179
|
+
`<${tag}> already has the role "${role}"; the role attribute adds nothing.`,
|
|
180
|
+
)
|
|
181
|
+
else if (tag === "a" && attributes.has("href") && roleText?.trim() === "link")
|
|
182
|
+
report(
|
|
183
|
+
"redundant-role",
|
|
184
|
+
written.at,
|
|
185
|
+
'<a href> already has the role "link"; the role attribute adds nothing.',
|
|
186
|
+
)
|
|
187
|
+
const native = NATIVE_FOR_ROLE.get(role)
|
|
188
|
+
if ((tag === "div" || tag === "span") && native !== undefined)
|
|
189
|
+
report(
|
|
190
|
+
"native-element",
|
|
191
|
+
written.at,
|
|
192
|
+
`<${tag} role="${role}"> has to rebuild what ${native} gives for free: keyboard operation, focus and the role itself.`,
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (!attributes.has("alt")) {
|
|
197
|
+
const type = textOf("type")
|
|
198
|
+
const presentational = role === "none" || role === "presentation"
|
|
199
|
+
if (
|
|
200
|
+
(tag === "img" && !presentational) ||
|
|
201
|
+
(tag === "input" && type === "image") ||
|
|
202
|
+
(tag === "area" && attributes.has("href"))
|
|
203
|
+
)
|
|
204
|
+
report(
|
|
205
|
+
"img-alt",
|
|
206
|
+
elementStart,
|
|
207
|
+
`<${tag}> has no alt attribute, so a screen reader reads out the file name. Describe the image, or write alt="" when it is decoration.`,
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const tabindex = textOf("tabindex")
|
|
212
|
+
if (tabindex !== undefined && /^\s*\+?\d+\s*$/.test(tabindex) && Number(tabindex) > 0) {
|
|
213
|
+
report(
|
|
214
|
+
"tabindex",
|
|
215
|
+
attributes.get("tabindex")?.at ?? elementStart,
|
|
216
|
+
`tabindex="${tabindex.trim()}" pulls the element ahead of the page's own tab order. Use 0 to join the order or -1 to focus it from script.`,
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (tag === "html" && !attributes.has("lang"))
|
|
221
|
+
report(
|
|
222
|
+
"html-lang",
|
|
223
|
+
elementStart,
|
|
224
|
+
"<html> has no lang attribute, so a screen reader picks its pronunciation from the reader's settings. Write lang=\"en\" or the page's language.",
|
|
225
|
+
)
|
|
226
|
+
return found
|
|
227
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type * as TSApi from "typescript"
|
|
2
|
+
import {
|
|
3
|
+
type A11yFinding,
|
|
4
|
+
type AttributeValue,
|
|
5
|
+
type ElementAttribute,
|
|
6
|
+
judgeElement,
|
|
7
|
+
} from "./a11y-element.ts"
|
|
8
|
+
import type { TypeScriptApi } from "./typescript-import.ts"
|
|
9
|
+
|
|
10
|
+
export type { A11yCode, A11yFinding, A11yKind } from "./a11y-element.ts"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The accessibility findings in the JSX of `source`: React, Preact and Solid all write the same
|
|
14
|
+
* intrinsic elements. Only a lowercase intrinsic element is judged, and only from what its attributes
|
|
15
|
+
* spell out: an element with a spread is left alone, since the spread may supply or replace any of
|
|
16
|
+
* them, and a value written as an expression is never read.
|
|
17
|
+
*/
|
|
18
|
+
export function a11yFindings(ts: TypeScriptApi, source: TSApi.SourceFile): readonly A11yFinding[] {
|
|
19
|
+
const found: A11yFinding[] = []
|
|
20
|
+
|
|
21
|
+
const attributeValue = (attribute: TSApi.JsxAttribute): AttributeValue => {
|
|
22
|
+
const init = attribute.initializer
|
|
23
|
+
if (init === undefined) return { kind: "other" }
|
|
24
|
+
if (ts.isStringLiteral(init)) return { kind: "text", text: init.text }
|
|
25
|
+
if (ts.isJsxExpression(init) && init.expression !== undefined) {
|
|
26
|
+
const expression = init.expression
|
|
27
|
+
if (ts.isStringLiteralLike(expression) || ts.isNumericLiteral(expression))
|
|
28
|
+
return { kind: "text", text: expression.text }
|
|
29
|
+
}
|
|
30
|
+
return { kind: "other" }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const inspect = (node: TSApi.JsxOpeningElement | TSApi.JsxSelfClosingElement): void => {
|
|
34
|
+
const tagName = node.tagName
|
|
35
|
+
if (!ts.isIdentifier(tagName) || !/^[a-z][a-z0-9]*$/.test(tagName.text)) return
|
|
36
|
+
const attributes = new Map<string, ElementAttribute>()
|
|
37
|
+
for (const property of node.attributes.properties) {
|
|
38
|
+
if (ts.isJsxSpreadAttribute(property)) return
|
|
39
|
+
if (!ts.isIdentifier(property.name)) continue
|
|
40
|
+
attributes.set(property.name.text.toLowerCase(), {
|
|
41
|
+
at: property.getStart(source),
|
|
42
|
+
value: attributeValue(property),
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
found.push(...judgeElement(tagName.text, attributes, node.getStart(source)))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const visit = (node: TSApi.Node): void => {
|
|
49
|
+
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) inspect(node)
|
|
50
|
+
ts.forEachChild(node, visit)
|
|
51
|
+
}
|
|
52
|
+
visit(source)
|
|
53
|
+
return found.sort((a, b) => a.position - b.position)
|
|
54
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type A11yFinding,
|
|
3
|
+
type AttributeValue,
|
|
4
|
+
type ElementAttribute,
|
|
5
|
+
judgeElement,
|
|
6
|
+
} from "./a11y-element.ts"
|
|
7
|
+
import { readAttributes, scanTemplate, type TemplateAttribute } from "./template-markup.ts"
|
|
8
|
+
|
|
9
|
+
const INTRINSIC = /^[a-z][a-z0-9]*$/
|
|
10
|
+
const OTHER: AttributeValue = { kind: "other" }
|
|
11
|
+
|
|
12
|
+
/** An attribute that may bring in any other: a Svelte `{...props}` or a Vue `v-bind="props"` / `:[name]`. */
|
|
13
|
+
const spreads = (name: string): boolean =>
|
|
14
|
+
/^\{\s*\.\.\./.test(name) ||
|
|
15
|
+
name === "v-bind" ||
|
|
16
|
+
name.startsWith(":[") ||
|
|
17
|
+
name.startsWith("v-bind:[")
|
|
18
|
+
|
|
19
|
+
/** The attribute an element carries under this written name: undefined for a directive (`on:click`,
|
|
20
|
+
* `@click`, `v-if`, `use:action`), which is not one. A bound value (`:role`, `v-bind:role`, Svelte's
|
|
21
|
+
* `{role}`) is named but its value is not read. */
|
|
22
|
+
function attributeName(
|
|
23
|
+
name: string,
|
|
24
|
+
): { readonly name: string; readonly bound: boolean } | undefined {
|
|
25
|
+
const shorthand = /^\{\s*([\w-]+)\s*\}$/.exec(name)
|
|
26
|
+
if (shorthand?.[1] !== undefined) return { name: shorthand[1].toLowerCase(), bound: true }
|
|
27
|
+
const bound = name.startsWith(":") || name.startsWith("v-bind:")
|
|
28
|
+
const plain = name.startsWith(":")
|
|
29
|
+
? name.slice(1)
|
|
30
|
+
: name.startsWith("v-bind:")
|
|
31
|
+
? name.slice(7)
|
|
32
|
+
: name
|
|
33
|
+
if (/[:@#]/.test(plain) || plain.startsWith("v-")) return undefined
|
|
34
|
+
return { name: (plain.split(".")[0] ?? plain).toLowerCase(), bound }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const attributeValue = (attribute: TemplateAttribute, bound: boolean): AttributeValue => {
|
|
38
|
+
if (bound) return OTHER
|
|
39
|
+
const literal = attribute.quoted ?? attribute.braceString ?? attribute.bare
|
|
40
|
+
// A Svelte `alt="{name} photo"` holds an expression: its text is not what the page renders.
|
|
41
|
+
if (literal !== undefined) return /[{}]/.test(literal) ? OTHER : { kind: "text", text: literal }
|
|
42
|
+
if (attribute.braced !== undefined && /^\s*\d+\s*$/.test(attribute.braced))
|
|
43
|
+
return { kind: "text", text: attribute.braced.trim() }
|
|
44
|
+
return OTHER
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The accessibility findings in the template of a `.vue` or `.svelte` file, judged by the same rules as
|
|
49
|
+
* JSX. `vue` reads a Vue file's `<template>`; otherwise the file is Svelte, whose markup is the whole
|
|
50
|
+
* file outside its `<script>` and `<style>`. Offsets are into `text`.
|
|
51
|
+
*/
|
|
52
|
+
export function a11yTemplateFindings(
|
|
53
|
+
text: string,
|
|
54
|
+
vue: boolean,
|
|
55
|
+
): { readonly findings: readonly A11yFinding[]; readonly expectationLines: readonly string[] } {
|
|
56
|
+
const { tags, expectationLines } = scanTemplate(text, vue)
|
|
57
|
+
const findings: A11yFinding[] = []
|
|
58
|
+
for (const tag of tags) {
|
|
59
|
+
if (!tag.inTemplate || !INTRINSIC.test(tag.written)) continue
|
|
60
|
+
const written = readAttributes(text, tag)
|
|
61
|
+
if (written.some((attribute) => spreads(attribute.name))) continue
|
|
62
|
+
const attributes = new Map<string, ElementAttribute>()
|
|
63
|
+
for (const attribute of written) {
|
|
64
|
+
const named = attributeName(attribute.name)
|
|
65
|
+
if (named === undefined) continue
|
|
66
|
+
attributes.set(named.name, {
|
|
67
|
+
at: attribute.at,
|
|
68
|
+
value: attributeValue(attribute, named.bound),
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
findings.push(...judgeElement(tag.name, attributes, tag.at))
|
|
72
|
+
}
|
|
73
|
+
return { findings: findings.sort((a, b) => a.position - b.position), expectationLines }
|
|
74
|
+
}
|