prunelle 0.1.1 → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
// Static RGAA JSX accessibility checks: WAI-ARIA, headings, controls, tables, fieldsets, and focus.
|
|
2
|
+
|
|
3
|
+
import type { JSXAttribute, JSXAttributeItem, JSXChild, JSXElement, Node } from 'oxc-parser'
|
|
4
|
+
import type { CompilerWarning } from './mistakes'
|
|
5
|
+
import { cleanJsxText, isComponentTag, jsxTagName, staticLiteral, unwrapParens, unwrapTsExpr } from './shared'
|
|
6
|
+
|
|
7
|
+
export const RGAA_JSX_RULES = [
|
|
8
|
+
'rgaa/img-alt',
|
|
9
|
+
'rgaa/form-label',
|
|
10
|
+
'rgaa/accessible-name',
|
|
11
|
+
'rgaa/duplicate-id',
|
|
12
|
+
'rgaa/positive-tabindex',
|
|
13
|
+
'rgaa/nested-control',
|
|
14
|
+
'rgaa/link-as-button',
|
|
15
|
+
'rgaa/unknown-role',
|
|
16
|
+
'rgaa/unknown-aria',
|
|
17
|
+
'rgaa/invalid-aria-value',
|
|
18
|
+
'rgaa/skipped-heading',
|
|
19
|
+
'rgaa/table-without-header',
|
|
20
|
+
'rgaa/fieldset-without-legend',
|
|
21
|
+
'rgaa/click-without-key',
|
|
22
|
+
'rgaa/autofocus',
|
|
23
|
+
'rgaa/meta-refresh',
|
|
24
|
+
'rgaa/role-without-focus',
|
|
25
|
+
'rgaa/mouse-only',
|
|
26
|
+
'rgaa/frame-title',
|
|
27
|
+
'rgaa/empty-heading',
|
|
28
|
+
'rgaa/autocomplete',
|
|
29
|
+
] as const
|
|
30
|
+
|
|
31
|
+
export type RgaaJsxRule = (typeof RGAA_JSX_RULES)[number]
|
|
32
|
+
|
|
33
|
+
const NON_LABELLED_INPUT_TYPES = new Set(['hidden', 'submit', 'reset', 'button', 'image'])
|
|
34
|
+
export const INTERACTIVE_ANCESTORS = new Set(['a', 'button', 'summary'])
|
|
35
|
+
const INTERACTIVE_DESCENDANTS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary'])
|
|
36
|
+
const NATIVE_CLICKABLE_TAGS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary', 'label', 'option', 'dialog', 'area'])
|
|
37
|
+
const TABLE_STRUCTURAL_TAGS = new Set(['thead', 'tbody', 'tfoot', 'tr'])
|
|
38
|
+
const HEADING_LEVELS: Record<string, number> = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }
|
|
39
|
+
const PRESENTATIONAL_ROLES = new Set(['presentation', 'none'])
|
|
40
|
+
// Roles a user operates directly, so the element needs a tabindex to be reached from the keyboard.
|
|
41
|
+
const FOCUSABLE_ROLES = new Set(['button', 'link', 'checkbox', 'switch', 'radio', 'slider', 'spinbutton', 'textbox', 'searchbox', 'combobox', 'tab'])
|
|
42
|
+
// Items of a composite widget, moved through with arrow keys: hovering them to highlight is not hover-only behaviour.
|
|
43
|
+
const COMPOSITE_ITEM_ROLES = new Set(['option', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'treeitem', 'gridcell', 'row', 'tab'])
|
|
44
|
+
// Field names that collect personal data, normalized (lowercase, no `-` or `_`), with the autocomplete token they need (RGAA 11.13).
|
|
45
|
+
const AUTOCOMPLETE_BY_NAME = new Map(Object.entries({
|
|
46
|
+
email: 'email', tel: 'tel', phone: 'tel', name: 'name', fullname: 'name',
|
|
47
|
+
givenname: 'given-name', firstname: 'given-name', familyname: 'family-name', lastname: 'family-name', surname: 'family-name',
|
|
48
|
+
address: 'street-address', streetaddress: 'street-address', postalcode: 'postal-code', zip: 'postal-code', zipcode: 'postal-code', postcode: 'postal-code',
|
|
49
|
+
city: 'address-level2', country: 'country-name', username: 'username', organization: 'organization',
|
|
50
|
+
}))
|
|
51
|
+
const NAME_SEPARATORS_RE = /[-_]/g
|
|
52
|
+
const INT_RE = /^-?\d+$/
|
|
53
|
+
const NUM_RE = /^-?(?:\d+\.?\d*|\.\d+)$/
|
|
54
|
+
const TABINDEX_NUM_RE = /^[+-]?\d+(?:\.\d+)?$/
|
|
55
|
+
|
|
56
|
+
// Concrete WAI-ARIA 1.2 & 1.3 roles (abstract roles excluded).
|
|
57
|
+
const VALID_ARIA_ROLES = new Set((
|
|
58
|
+
'alert alertdialog application article associationlist associationlistitemkey associationlistitemvalue '
|
|
59
|
+
+ 'banner blockquote button caption cell checkbox code columnheader combobox comment complementary contentinfo '
|
|
60
|
+
+ 'definition deletion dialog directory document emphasis feed figure form generic '
|
|
61
|
+
+ 'graphics-document graphics-object graphics-symbol grid gridcell group heading image img insertion '
|
|
62
|
+
+ 'link list listbox listitem log main mark marquee math menu menubar menuitem menuitemcheckbox menuitemradio '
|
|
63
|
+
+ 'meter navigation none note option paragraph presentation progressbar radio radiogroup region row rowgroup rowheader '
|
|
64
|
+
+ 'scrollbar search searchbox separator slider spinbutton status strong subscript suggestion superscript switch '
|
|
65
|
+
+ 'tab table tablist tabpanel term textbox time timer toolbar tooltip tree treegrid treeitem'
|
|
66
|
+
).split(' '))
|
|
67
|
+
|
|
68
|
+
// Valid WAI-ARIA 1.1 / 1.2 / 1.3 attributes.
|
|
69
|
+
const VALID_ARIA_ATTRS = new Set((
|
|
70
|
+
'aria-activedescendant aria-atomic aria-autocomplete aria-braillelabel aria-brailleroledescription '
|
|
71
|
+
+ 'aria-busy aria-checked aria-colcount aria-colindex aria-colindextext aria-colspan aria-controls '
|
|
72
|
+
+ 'aria-current aria-describedby aria-description aria-details aria-disabled aria-dropeffect '
|
|
73
|
+
+ 'aria-errormessage aria-expanded aria-flowto aria-grabbed aria-haspopup aria-hidden aria-invalid '
|
|
74
|
+
+ 'aria-keyshortcuts aria-label aria-labelledby aria-level aria-live aria-modal aria-multiline '
|
|
75
|
+
+ 'aria-multiselectable aria-orientation aria-owns aria-placeholder aria-posinset aria-pressed '
|
|
76
|
+
+ 'aria-readonly aria-relevant aria-required aria-roledescription aria-rowcount aria-rowindex '
|
|
77
|
+
+ 'aria-rowindextext aria-rowspan aria-selected aria-setsize aria-sort aria-valuemax aria-valuemin '
|
|
78
|
+
+ 'aria-valuenow aria-valuetext'
|
|
79
|
+
).split(' '))
|
|
80
|
+
|
|
81
|
+
const ARIA_BOOL_ATTRS = new Set((
|
|
82
|
+
'aria-atomic aria-busy aria-disabled aria-expanded aria-grabbed aria-hidden '
|
|
83
|
+
+ 'aria-modal aria-multiline aria-multiselectable aria-readonly aria-required aria-selected'
|
|
84
|
+
).split(' '))
|
|
85
|
+
const ARIA_TRISTATE_ATTRS = new Set(['aria-checked', 'aria-pressed'])
|
|
86
|
+
const ARIA_INT_ATTRS = new Set((
|
|
87
|
+
'aria-level aria-posinset aria-setsize aria-colcount aria-rowcount '
|
|
88
|
+
+ 'aria-colindex aria-rowindex aria-colspan aria-rowspan'
|
|
89
|
+
).split(' '))
|
|
90
|
+
const ARIA_NUM_ATTRS = new Set(['aria-valuemin', 'aria-valuemax', 'aria-valuenow'])
|
|
91
|
+
const ARIA_RELEVANT_TOKENS = new Set(['additions', 'removals', 'text', 'all'])
|
|
92
|
+
const ARIA_ENUM_ATTRS: Record<string, ReadonlySet<string>> = {
|
|
93
|
+
'aria-live': new Set(['off', 'polite', 'assertive']),
|
|
94
|
+
'aria-orientation': new Set(['horizontal', 'vertical']),
|
|
95
|
+
'aria-sort': new Set(['none', 'ascending', 'descending', 'other']),
|
|
96
|
+
'aria-current': new Set(['page', 'step', 'location', 'date', 'time', 'true', 'false']),
|
|
97
|
+
'aria-haspopup': new Set(['false', 'true', 'menu', 'listbox', 'tree', 'grid', 'dialog']),
|
|
98
|
+
'aria-invalid': new Set(['grammar', 'false', 'spelling', 'true']),
|
|
99
|
+
'aria-autocomplete': new Set(['inline', 'list', 'both', 'none']),
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface RgaaScope {
|
|
103
|
+
seenIds: Set<string>
|
|
104
|
+
lastHeading: number
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface RgaaElementContext {
|
|
108
|
+
filename: string
|
|
109
|
+
inLabel: boolean
|
|
110
|
+
inLoop: boolean
|
|
111
|
+
interactiveAncestor: string | undefined
|
|
112
|
+
labelTargets: ReadonlySet<string>
|
|
113
|
+
scope: RgaaScope
|
|
114
|
+
isIgnored: (rule: RgaaJsxRule, elementStart: number, attrStart?: number) => boolean
|
|
115
|
+
warnings: CompilerWarning[]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function createRgaaScope(): RgaaScope {
|
|
119
|
+
return { seenIds: new Set(), lastHeading: 0 }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function cloneRgaaScope(scope: RgaaScope): RgaaScope {
|
|
123
|
+
return { seenIds: new Set(scope.seenIds), lastHeading: scope.lastHeading }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function mergeRgaaScope(target: RgaaScope, branch: RgaaScope): void {
|
|
127
|
+
for (const id of branch.seenIds)
|
|
128
|
+
target.seenIds.add(id)
|
|
129
|
+
if (branch.lastHeading > target.lastHeading)
|
|
130
|
+
target.lastHeading = branch.lastHeading
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function findAttr(attributes: readonly JSXAttributeItem[], name: string): JSXAttribute | undefined {
|
|
134
|
+
for (const attr of attributes) {
|
|
135
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name === name)
|
|
136
|
+
return attr
|
|
137
|
+
}
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function findAttrLower(attributes: readonly JSXAttributeItem[], lowerName: string): JSXAttribute | undefined {
|
|
142
|
+
for (const attr of attributes) {
|
|
143
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name.toLowerCase() === lowerName)
|
|
144
|
+
return attr
|
|
145
|
+
}
|
|
146
|
+
return undefined
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function hasSpread(attributes: readonly JSXAttributeItem[]): boolean {
|
|
150
|
+
return attributes.some(a => a.type === 'JSXSpreadAttribute')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function staticStringValueOf(value: JSXAttribute['value']): string | undefined {
|
|
154
|
+
if (value === null)
|
|
155
|
+
return undefined
|
|
156
|
+
if (value.type === 'Literal' && typeof value.value === 'string')
|
|
157
|
+
return value.value
|
|
158
|
+
if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
|
|
159
|
+
const expr = unwrapTsExpr(value.expression)
|
|
160
|
+
if (expr.type === 'Literal' && typeof expr.value === 'string')
|
|
161
|
+
return expr.value
|
|
162
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
|
|
163
|
+
return expr.quasis[0]?.value.cooked ?? undefined
|
|
164
|
+
}
|
|
165
|
+
return undefined
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function attrTargetKey(attr: JSXAttribute | undefined): string | undefined {
|
|
169
|
+
if (attr === undefined || attr.value === null)
|
|
170
|
+
return undefined
|
|
171
|
+
const str = staticStringValueOf(attr.value)
|
|
172
|
+
if (str !== undefined && str.trim() !== '')
|
|
173
|
+
return `s:${str}`
|
|
174
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
175
|
+
const path = staticPath(attr.value.expression)
|
|
176
|
+
if (path !== undefined)
|
|
177
|
+
return `i:${path}`
|
|
178
|
+
}
|
|
179
|
+
return undefined
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// `ids`, `ids.name` and `ids['name']` as one dotted path, so `for` and `id` holding the same expression match.
|
|
183
|
+
function staticPath(node: Node): string | undefined {
|
|
184
|
+
const expr = unwrapTsExpr(node)
|
|
185
|
+
if (expr.type === 'Identifier')
|
|
186
|
+
return expr.name
|
|
187
|
+
if (expr.type !== 'MemberExpression')
|
|
188
|
+
return undefined
|
|
189
|
+
const object = staticPath(expr.object)
|
|
190
|
+
const property = !expr.computed && expr.property.type === 'Identifier'
|
|
191
|
+
? expr.property.name
|
|
192
|
+
: expr.property.type === 'Literal' && typeof expr.property.value === 'string' ? expr.property.value : undefined
|
|
193
|
+
return object !== undefined && property !== undefined ? `${object}.${property}` : undefined
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function hasNonEmptyAttr(attributes: readonly JSXAttributeItem[], name: string): boolean {
|
|
197
|
+
const attr = findAttr(attributes, name)
|
|
198
|
+
if (attr === undefined || attr.value === null)
|
|
199
|
+
return false
|
|
200
|
+
const str = staticStringValueOf(attr.value)
|
|
201
|
+
if (str !== undefined)
|
|
202
|
+
return str.trim() !== ''
|
|
203
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
204
|
+
const expr = unwrapParens(attr.value.expression)
|
|
205
|
+
if (expr.type === 'Literal' && (expr.value === null || expr.value === false || expr.value === ''))
|
|
206
|
+
return false
|
|
207
|
+
if (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
208
|
+
return false
|
|
209
|
+
return true
|
|
210
|
+
}
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function isExplicitlyFalsyExpr(value: JSXAttribute['value']): boolean {
|
|
215
|
+
if (value === null || value.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression')
|
|
216
|
+
return false
|
|
217
|
+
const expr = unwrapTsExpr(value.expression)
|
|
218
|
+
if (expr.type === 'Literal' && (expr.value === false || expr.value === null))
|
|
219
|
+
return true
|
|
220
|
+
return expr.type === 'Identifier' && expr.name === 'undefined'
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function hasLabelAttr(attributes: readonly JSXAttributeItem[]): boolean {
|
|
224
|
+
return hasNonEmptyAttr(attributes, 'aria-label')
|
|
225
|
+
|| hasNonEmptyAttr(attributes, 'aria-labelledby')
|
|
226
|
+
|| hasNonEmptyAttr(attributes, 'title')
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function isAriaHidden(attributes: readonly JSXAttributeItem[]): boolean {
|
|
230
|
+
const attr = findAttr(attributes, 'aria-hidden')
|
|
231
|
+
if (attr === undefined)
|
|
232
|
+
return false
|
|
233
|
+
if (attr.value === null)
|
|
234
|
+
return true
|
|
235
|
+
if (attr.value.type === 'Literal')
|
|
236
|
+
return attr.value.value === 'true'
|
|
237
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
238
|
+
const expr = unwrapParens(attr.value.expression)
|
|
239
|
+
return expr.type === 'Literal' && (expr.value === true || expr.value === 'true')
|
|
240
|
+
}
|
|
241
|
+
return false
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function isPresentational(attributes: readonly JSXAttributeItem[]): boolean {
|
|
245
|
+
const raw = staticStringValueOf(findAttr(attributes, 'role')?.value ?? null)
|
|
246
|
+
return raw !== undefined && raw.trim().split(/\s+/).some(token => PRESENTATIONAL_ROLES.has(token))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function hasMeaningfulExpression(expr: Node): boolean {
|
|
250
|
+
const inner = unwrapParens(expr)
|
|
251
|
+
if (inner.type === 'Literal')
|
|
252
|
+
return typeof inner.value === 'string' ? inner.value.trim() !== '' : typeof inner.value === 'number'
|
|
253
|
+
return !(inner.type === 'Identifier' && inner.name === 'undefined')
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function hasAccessibleName(children: readonly JSXChild[]): boolean {
|
|
257
|
+
for (const child of children) {
|
|
258
|
+
if (child.type === 'JSXText') {
|
|
259
|
+
if (cleanJsxText(child.value).trim() !== '')
|
|
260
|
+
return true
|
|
261
|
+
}
|
|
262
|
+
else if (child.type === 'JSXExpressionContainer') {
|
|
263
|
+
if (child.expression.type !== 'JSXEmptyExpression' && hasMeaningfulExpression(child.expression))
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
else if (child.type === 'JSXFragment') {
|
|
267
|
+
if (hasAccessibleName(child.children))
|
|
268
|
+
return true
|
|
269
|
+
}
|
|
270
|
+
else if (child.type === 'JSXElement') {
|
|
271
|
+
const attrs = child.openingElement.attributes
|
|
272
|
+
if (isAriaHidden(attrs))
|
|
273
|
+
continue
|
|
274
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
275
|
+
if (hasSpread(attrs) || hasLabelAttr(attrs))
|
|
276
|
+
return true
|
|
277
|
+
if (tag === 'img') {
|
|
278
|
+
if (hasNonEmptyAttr(attrs, 'alt'))
|
|
279
|
+
return true
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
if (tag === 'svg') {
|
|
283
|
+
if (child.children.some(c => c.type === 'JSXElement' && jsxTagName(c.openingElement.name) === 'title' && hasAccessibleName(c.children)))
|
|
284
|
+
return true
|
|
285
|
+
continue
|
|
286
|
+
}
|
|
287
|
+
if (isComponentTag(tag)) {
|
|
288
|
+
if (hasNonEmptyAttr(attrs, 'label') || hasAccessibleName(child.children))
|
|
289
|
+
return true
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
if (hasAccessibleName(child.children))
|
|
293
|
+
return true
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return false
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
type StaticAttrVal =
|
|
300
|
+
| { kind: 'bool', value: boolean }
|
|
301
|
+
| { kind: 'number', value: number }
|
|
302
|
+
| { kind: 'string', value: string }
|
|
303
|
+
|
|
304
|
+
function extractStaticAttrValue(attr: JSXAttribute): StaticAttrVal | undefined {
|
|
305
|
+
if (attr.value === null)
|
|
306
|
+
return { kind: 'bool', value: true }
|
|
307
|
+
if (attr.value.type === 'Literal' && typeof attr.value.value === 'string')
|
|
308
|
+
return { kind: 'string', value: attr.value.value }
|
|
309
|
+
if (attr.value.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
|
|
310
|
+
return undefined
|
|
311
|
+
const expr = unwrapTsExpr(attr.value.expression)
|
|
312
|
+
if (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
313
|
+
return undefined
|
|
314
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
|
|
315
|
+
return { kind: 'string', value: expr.quasis[0]?.value.cooked ?? '' }
|
|
316
|
+
if (expr.type === 'UnaryExpression' && expr.operator === '+' && expr.argument.type === 'Literal' && typeof expr.argument.value === 'number')
|
|
317
|
+
return { kind: 'number', value: expr.argument.value }
|
|
318
|
+
const lit = staticLiteral(expr)
|
|
319
|
+
if (!lit.ok || lit.value === null)
|
|
320
|
+
return undefined
|
|
321
|
+
if (typeof lit.value === 'boolean')
|
|
322
|
+
return { kind: 'bool', value: lit.value }
|
|
323
|
+
if (typeof lit.value === 'number')
|
|
324
|
+
return { kind: 'number', value: lit.value }
|
|
325
|
+
return { kind: 'string', value: lit.value }
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function validateAriaValue(name: string, val: StaticAttrVal): string | undefined {
|
|
329
|
+
if (ARIA_BOOL_ATTRS.has(name))
|
|
330
|
+
return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false')) ? undefined : 'expected "true" or "false"'
|
|
331
|
+
if (ARIA_TRISTATE_ATTRS.has(name))
|
|
332
|
+
return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false' || val.value === 'mixed')) ? undefined : 'expected "true", "false", or "mixed"'
|
|
333
|
+
const enumSet = ARIA_ENUM_ATTRS[name]
|
|
334
|
+
if (enumSet !== undefined) {
|
|
335
|
+
const token = val.kind === 'bool' ? String(val.value) : val.kind === 'string' ? val.value : undefined
|
|
336
|
+
return token !== undefined && enumSet.has(token) ? undefined : `expected one of ${[...enumSet].join(', ')}`
|
|
337
|
+
}
|
|
338
|
+
if (name === 'aria-relevant') {
|
|
339
|
+
const tokens = val.kind === 'string' ? val.value.trim().split(/\s+/).filter(Boolean) : []
|
|
340
|
+
return tokens.length > 0 && tokens.every(t => ARIA_RELEVANT_TOKENS.has(t)) ? undefined : 'expected space-separated tokens from additions, removals, text, all'
|
|
341
|
+
}
|
|
342
|
+
if (ARIA_INT_ATTRS.has(name))
|
|
343
|
+
return (val.kind === 'number' && Number.isInteger(val.value)) || (val.kind === 'string' && INT_RE.test(val.value.trim())) ? undefined : 'expected an integer'
|
|
344
|
+
if (ARIA_NUM_ATTRS.has(name))
|
|
345
|
+
return (val.kind === 'number' && Number.isFinite(val.value)) || (val.kind === 'string' && NUM_RE.test(val.value.trim())) ? undefined : 'expected a number'
|
|
346
|
+
return undefined
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function emitWarning(ctx: RgaaElementContext, rule: RgaaJsxRule, elementStart: number, message: string, attrStart?: number): void {
|
|
350
|
+
if (!ctx.isIgnored(rule, elementStart, attrStart))
|
|
351
|
+
ctx.warnings.push({ rule, start: attrStart ?? elementStart, message })
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function checkRoleAndAria(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
355
|
+
const attrs = element.openingElement.attributes
|
|
356
|
+
const roleAttr = findAttr(attrs, 'role')
|
|
357
|
+
if (roleAttr !== undefined) {
|
|
358
|
+
const raw = staticStringValueOf(roleAttr.value)
|
|
359
|
+
if (raw !== undefined) {
|
|
360
|
+
const tokens = raw.trim().split(/\s+/).filter(Boolean)
|
|
361
|
+
const invalid = tokens.filter(t => !VALID_ARIA_ROLES.has(t))
|
|
362
|
+
if (tokens.length === 0 || invalid.length > 0) {
|
|
363
|
+
const detail = tokens.length === 0 ? 'an empty role' : `unknown WAI-ARIA role "${invalid[0]}"`
|
|
364
|
+
emitWarning(ctx, 'rgaa/unknown-role', element.start, `[rgaa/unknown-role] <${tag} role="${raw}"> in ${ctx.filename} uses ${detail}: use a valid WAI-ARIA role or remove the attribute.`, roleAttr.start)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const attr of attrs) {
|
|
369
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier' || !attr.name.name.startsWith('aria-'))
|
|
370
|
+
continue
|
|
371
|
+
const name = attr.name.name
|
|
372
|
+
if (!VALID_ARIA_ATTRS.has(name)) {
|
|
373
|
+
emitWarning(ctx, 'rgaa/unknown-aria', element.start, `[rgaa/unknown-aria] <${tag} ${name}> in ${ctx.filename} is not a valid WAI-ARIA attribute: check the spelling or remove it.`, attr.start)
|
|
374
|
+
continue
|
|
375
|
+
}
|
|
376
|
+
const val = extractStaticAttrValue(attr)
|
|
377
|
+
if (val === undefined)
|
|
378
|
+
continue
|
|
379
|
+
const err = validateAriaValue(name, val)
|
|
380
|
+
if (err !== undefined) {
|
|
381
|
+
const shown = val.kind === 'string' ? `"${val.value}"` : String(val.value)
|
|
382
|
+
emitWarning(ctx, 'rgaa/invalid-aria-value', element.start, `[rgaa/invalid-aria-value] <${tag} ${name}=${shown}> in ${ctx.filename} has an invalid value (${err}).`, attr.start)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function checkExprForChild(expr: Node, checkElement: (el: JSXElement) => boolean, checkFragment: (children: readonly JSXChild[]) => boolean): boolean {
|
|
388
|
+
const inner = unwrapTsExpr(expr)
|
|
389
|
+
if (inner.type === 'Literal' || (inner.type === 'Identifier' && inner.name === 'undefined'))
|
|
390
|
+
return false
|
|
391
|
+
if (inner.type === 'JSXElement')
|
|
392
|
+
return checkElement(inner)
|
|
393
|
+
if (inner.type === 'JSXFragment')
|
|
394
|
+
return checkFragment(inner.children)
|
|
395
|
+
if (inner.type === 'LogicalExpression') {
|
|
396
|
+
return inner.operator === '&&'
|
|
397
|
+
? checkExprForChild(inner.right, checkElement, checkFragment)
|
|
398
|
+
: checkExprForChild(inner.left, checkElement, checkFragment) || checkExprForChild(inner.right, checkElement, checkFragment)
|
|
399
|
+
}
|
|
400
|
+
if (inner.type === 'ConditionalExpression')
|
|
401
|
+
return checkExprForChild(inner.consequent, checkElement, checkFragment) || checkExprForChild(inner.alternate, checkElement, checkFragment)
|
|
402
|
+
return true
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function tableMayHaveHeader(children: readonly JSXChild[]): boolean {
|
|
406
|
+
const checkEl = (el: JSXElement): boolean => {
|
|
407
|
+
const tag = jsxTagName(el.openingElement.name)
|
|
408
|
+
if (tag === 'th' || isComponentTag(tag))
|
|
409
|
+
return true
|
|
410
|
+
return TABLE_STRUCTURAL_TAGS.has(tag) && tableMayHaveHeader(el.children)
|
|
411
|
+
}
|
|
412
|
+
for (const child of children) {
|
|
413
|
+
if (child.type === 'JSXElement' && checkEl(child))
|
|
414
|
+
return true
|
|
415
|
+
if (child.type === 'JSXFragment' && tableMayHaveHeader(child.children))
|
|
416
|
+
return true
|
|
417
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, tableMayHaveHeader))
|
|
418
|
+
return true
|
|
419
|
+
}
|
|
420
|
+
return false
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function fieldsetMayHaveLegend(children: readonly JSXChild[], isDirect: boolean): boolean {
|
|
424
|
+
const checkEl = (el: JSXElement): boolean => {
|
|
425
|
+
const tag = jsxTagName(el.openingElement.name)
|
|
426
|
+
if (tag === 'legend')
|
|
427
|
+
return true
|
|
428
|
+
if (tag === 'fieldset')
|
|
429
|
+
return false
|
|
430
|
+
return (isDirect && isComponentTag(tag)) || fieldsetMayHaveLegend(el.children, false)
|
|
431
|
+
}
|
|
432
|
+
for (const child of children) {
|
|
433
|
+
if (child.type === 'JSXElement' && checkEl(child))
|
|
434
|
+
return true
|
|
435
|
+
if (child.type === 'JSXFragment' && fieldsetMayHaveLegend(child.children, isDirect))
|
|
436
|
+
return true
|
|
437
|
+
if (isDirect && child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, cs => fieldsetMayHaveLegend(cs, true)))
|
|
438
|
+
return true
|
|
439
|
+
}
|
|
440
|
+
return false
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function checkFocusAndInteraction(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
|
|
444
|
+
const attrs = element.openingElement.attributes
|
|
445
|
+
|
|
446
|
+
const tabAttr = findAttrLower(attrs, 'tabindex')
|
|
447
|
+
if (tabAttr !== undefined) {
|
|
448
|
+
const val = extractStaticAttrValue(tabAttr)
|
|
449
|
+
const num = val?.kind === 'number' ? val.value : val?.kind === 'string' && TABINDEX_NUM_RE.test(val.value.trim()) ? Number(val.value.trim()) : undefined
|
|
450
|
+
if (num !== undefined && Number.isFinite(num) && num > 0) {
|
|
451
|
+
const attrName = tabAttr.name.type === 'JSXIdentifier' ? tabAttr.name.name : 'tabindex'
|
|
452
|
+
emitWarning(ctx, 'rgaa/positive-tabindex', element.start, `[rgaa/positive-tabindex] <${tag} ${attrName}={${num}}> in ${ctx.filename} uses a positive tabindex: use 0, -1, or reorder the DOM to preserve natural keyboard order.`, tabAttr.start)
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const autoFocusAttr = findAttrLower(attrs, 'autofocus')
|
|
457
|
+
if (autoFocusAttr !== undefined && !isExplicitlyFalsyExpr(autoFocusAttr.value)) {
|
|
458
|
+
const attrName = autoFocusAttr.name.type === 'JSXIdentifier' ? autoFocusAttr.name.name : 'autofocus'
|
|
459
|
+
emitWarning(ctx, 'rgaa/autofocus', element.start, `[rgaa/autofocus] <${tag} ${attrName}> in ${ctx.filename} steals focus on load and can disorient keyboard and screen-reader users: remove ${attrName} or manage focus explicitly.`, autoFocusAttr.start)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (ctx.interactiveAncestor !== undefined && INTERACTIVE_DESCENDANTS.has(tag)) {
|
|
463
|
+
const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
|
|
464
|
+
if (inputType !== 'hidden')
|
|
465
|
+
emitWarning(ctx, 'rgaa/nested-control', element.start, `[rgaa/nested-control] <${tag}> in ${ctx.filename} is nested inside <${ctx.interactiveAncestor}>: interactive controls must not be nested; place them as siblings.`)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const onClickAttr = findAttr(attrs, 'onClick')
|
|
469
|
+
const hasClick = onClickAttr !== undefined && !isExplicitlyFalsyExpr(onClickAttr.value)
|
|
470
|
+
if (tag === 'a' && !spread && hasClick && !hasNonEmptyAttr(attrs, 'href'))
|
|
471
|
+
emitWarning(ctx, 'rgaa/link-as-button', element.start, `[rgaa/link-as-button] <a> with onClick and no href in ${ctx.filename}: use <button type="button"> for actions, or add a valid href for navigation.`)
|
|
472
|
+
|
|
473
|
+
if (hasClick && !spread && !NATIVE_CLICKABLE_TAGS.has(tag) && !isPresentational(attrs)) {
|
|
474
|
+
const hasKey = findAttr(attrs, 'onKeyDown') !== undefined || findAttr(attrs, 'onKeyUp') !== undefined || findAttr(attrs, 'onKeyPress') !== undefined
|
|
475
|
+
if (!hasKey)
|
|
476
|
+
emitWarning(ctx, 'rgaa/click-without-key', element.start, `[rgaa/click-without-key] <${tag} onClick={...}> in ${ctx.filename} is not keyboard-accessible: use <button type="button">, or add onKeyDown/onKeyUp with an appropriate role and tabIndex.`, onClickAttr.start)
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function checkStructureAndIdentity(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
|
|
481
|
+
const attrs = element.openingElement.attributes
|
|
482
|
+
|
|
483
|
+
const idAttr = findAttr(attrs, 'id')
|
|
484
|
+
const id = idAttr !== undefined ? staticStringValueOf(idAttr.value) : undefined
|
|
485
|
+
if (id !== undefined && id !== '') {
|
|
486
|
+
if (ctx.inLoop)
|
|
487
|
+
emitWarning(ctx, 'rgaa/duplicate-id', element.start, `[rgaa/duplicate-id] Static id="${id}" on <${tag}> inside .map() in ${ctx.filename} duplicates across iterations: use a unique dynamic id.`, idAttr!.start)
|
|
488
|
+
else if (ctx.scope.seenIds.has(id))
|
|
489
|
+
emitWarning(ctx, 'rgaa/duplicate-id', element.start, `[rgaa/duplicate-id] Duplicate id="${id}" on <${tag}> in ${ctx.filename}: every id in a document must be unique.`, idAttr!.start)
|
|
490
|
+
else
|
|
491
|
+
ctx.scope.seenIds.add(id)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const headingLevel = HEADING_LEVELS[tag]
|
|
495
|
+
if (headingLevel !== undefined) {
|
|
496
|
+
if (ctx.scope.lastHeading > 0 && headingLevel > ctx.scope.lastHeading + 1)
|
|
497
|
+
emitWarning(ctx, 'rgaa/skipped-heading', element.start, `[rgaa/skipped-heading] <h${headingLevel}> in ${ctx.filename} skips heading level(s) after <h${ctx.scope.lastHeading}>: use <h${ctx.scope.lastHeading + 1}> to keep the heading hierarchy continuous.`)
|
|
498
|
+
ctx.scope.lastHeading = headingLevel
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (tag === 'table' && !isPresentational(attrs) && !(element.children.length === 0 && spread) && !tableMayHaveHeader(element.children))
|
|
502
|
+
emitWarning(ctx, 'rgaa/table-without-header', element.start, `[rgaa/table-without-header] <table> in ${ctx.filename} has no <th> header cell: add <th> headers or role="presentation" if used for layout.`)
|
|
503
|
+
|
|
504
|
+
if (tag === 'fieldset' && !(element.children.length === 0 && spread) && !fieldsetMayHaveLegend(element.children, true))
|
|
505
|
+
emitWarning(ctx, 'rgaa/fieldset-without-legend', element.start, `[rgaa/fieldset-without-legend] <fieldset> in ${ctx.filename} has no <legend>: add a <legend> child to label the form group.`)
|
|
506
|
+
|
|
507
|
+
if (tag === 'meta') {
|
|
508
|
+
const httpEquivAttr = findAttrLower(attrs, 'http-equiv') ?? findAttrLower(attrs, 'httpequiv')
|
|
509
|
+
if (httpEquivAttr !== undefined && staticStringValueOf(httpEquivAttr.value)?.trim().toLowerCase() === 'refresh')
|
|
510
|
+
emitWarning(ctx, 'rgaa/meta-refresh', element.start, `[rgaa/meta-refresh] <meta http-equiv="refresh"> in ${ctx.filename} redirects or reloads without user control: use an HTTP redirect or an explicit user action.`, httpEquivAttr.start)
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function hasHandler(attributes: readonly JSXAttributeItem[], names: readonly string[]): boolean {
|
|
515
|
+
return names.some((name) => {
|
|
516
|
+
const attr = findAttr(attributes, name)
|
|
517
|
+
return attr !== undefined && !isExplicitlyFalsyExpr(attr.value)
|
|
518
|
+
})
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Keyboard reach: an operable role without tabindex, hover-only handlers, and a link that goes nowhere.
|
|
522
|
+
function checkKeyboardReach(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
523
|
+
const attrs = element.openingElement.attributes
|
|
524
|
+
const role = staticStringValueOf(findAttr(attrs, 'role')?.value ?? null)?.trim().split(/\s+/)[0]
|
|
525
|
+
if (role !== undefined && FOCUSABLE_ROLES.has(role) && !NATIVE_CLICKABLE_TAGS.has(tag) && findAttrLower(attrs, 'tabindex') === undefined)
|
|
526
|
+
emitWarning(ctx, 'rgaa/role-without-focus', element.start, `[rgaa/role-without-focus] <${tag} role="${role}"> in ${ctx.filename} cannot be reached from the keyboard: add tabIndex={0}, or use the native element (<button>, <a href>, <input>).`)
|
|
527
|
+
|
|
528
|
+
if (role === undefined || !COMPOSITE_ITEM_ROLES.has(role)) {
|
|
529
|
+
const enterOnly = hasHandler(attrs, ['onMouseEnter', 'onMouseOver']) && !hasHandler(attrs, ['onFocus', 'onFocusIn'])
|
|
530
|
+
const leaveOnly = hasHandler(attrs, ['onMouseLeave', 'onMouseOut']) && !hasHandler(attrs, ['onBlur', 'onFocusOut'])
|
|
531
|
+
if (enterOnly || leaveOnly) {
|
|
532
|
+
const pair = enterOnly ? 'onMouseEnter or onMouseOver with onFocus or onFocusIn' : 'onMouseLeave or onMouseOut with onBlur or onFocusOut'
|
|
533
|
+
emitWarning(ctx, 'rgaa/mouse-only', element.start, `[rgaa/mouse-only] <${tag}> in ${ctx.filename} reacts to the mouse only: pair ${pair}, so keyboard users get the same behaviour.`)
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const href = tag === 'a' ? staticStringValueOf(findAttr(attrs, 'href')?.value ?? null)?.trim() : undefined
|
|
538
|
+
if (href === '#' || href?.toLowerCase().startsWith('javascript:') === true)
|
|
539
|
+
emitWarning(ctx, 'rgaa/link-as-button', element.start, `[rgaa/link-as-button] <a href="${href}"> in ${ctx.filename} goes nowhere: use <button type="button"> for an action, or link to a page or an #id.`)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Elements whose name or content assistive tools read out: a frame, a heading, an SVG drawn as an image.
|
|
543
|
+
function checkMissingNames(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
544
|
+
const attrs = element.openingElement.attributes
|
|
545
|
+
if (tag === 'iframe' && !isAriaHidden(attrs) && !hasLabelAttr(attrs))
|
|
546
|
+
emitWarning(ctx, 'rgaa/frame-title', element.start, `[rgaa/frame-title] <iframe> in ${ctx.filename} has no title: add title="..." naming what the frame holds.`)
|
|
547
|
+
|
|
548
|
+
if (HEADING_LEVELS[tag] !== undefined && !hasLabelAttr(attrs) && !hasAccessibleName(element.children))
|
|
549
|
+
emitWarning(ctx, 'rgaa/empty-heading', element.start, `[rgaa/empty-heading] <${tag}> in ${ctx.filename} is empty: give it text, or remove it.`)
|
|
550
|
+
|
|
551
|
+
const role = staticStringValueOf(findAttr(attrs, 'role')?.value ?? null)?.trim()
|
|
552
|
+
if (tag === 'svg' && role === 'img' && !isAriaHidden(attrs) && !hasAccessibleName([element]))
|
|
553
|
+
emitWarning(ctx, 'rgaa/accessible-name', element.start, `[rgaa/accessible-name] <svg role="img"> in ${ctx.filename} has no accessible name: add aria-label="..." or a <title> child.`)
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// A field collecting personal data names its purpose, so browsers and assistive tools can fill it (RGAA 11.13).
|
|
557
|
+
function checkAutocomplete(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
558
|
+
const attrs = element.openingElement.attributes
|
|
559
|
+
if ((tag !== 'input' && tag !== 'select') || findAttrLower(attrs, 'autocomplete') !== undefined)
|
|
560
|
+
return
|
|
561
|
+
const type = tag === 'input' ? (staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() ?? 'text') : 'text'
|
|
562
|
+
const name = staticStringValueOf(findAttr(attrs, 'name')?.value ?? null)
|
|
563
|
+
const token = type === 'email' || type === 'tel'
|
|
564
|
+
? type
|
|
565
|
+
: type === 'text' && name !== undefined ? AUTOCOMPLETE_BY_NAME.get(name.toLowerCase().replace(NAME_SEPARATORS_RE, '')) : undefined
|
|
566
|
+
if (token !== undefined)
|
|
567
|
+
emitWarning(ctx, 'rgaa/autocomplete', element.start, `[rgaa/autocomplete] <${tag}${name !== undefined ? ` name="${name}"` : ''}> in ${ctx.filename} collects personal data without autocomplete: add autocomplete="${token}".`)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// The ui Select draws its own combobox, so it needs a name as a native <select> does.
|
|
571
|
+
const SELECT_COMPONENT_RE = /(?:^|\.)Select$/
|
|
572
|
+
|
|
573
|
+
/** Runs the RGAA checks that apply to a component: a `Select` with no label, id link or `ariaLabel`. */
|
|
574
|
+
export function checkRgaaComponent(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
575
|
+
const attrs = element.openingElement.attributes
|
|
576
|
+
if (!SELECT_COMPONENT_RE.test(tag) || hasSpread(attrs) || ctx.inLabel || hasLabelAttr(attrs) || hasNonEmptyAttr(attrs, 'ariaLabel'))
|
|
577
|
+
return
|
|
578
|
+
const idKey = attrTargetKey(findAttr(attrs, 'id'))
|
|
579
|
+
if (idKey === undefined || !ctx.labelTargets.has(idKey))
|
|
580
|
+
emitWarning(ctx, 'rgaa/form-label', element.start, `[rgaa/form-label] <${tag}> in ${ctx.filename} has no accessible name: put it in <Field label="...">, link <label for="id">, or pass ariaLabel="...".`)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** Runs all static RGAA JSX compliance checks on an intrinsic HTML element. */
|
|
584
|
+
export function checkRgaaElement(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
585
|
+
const attrs = element.openingElement.attributes
|
|
586
|
+
const spread = hasSpread(attrs)
|
|
587
|
+
|
|
588
|
+
// RGAA 1.2 accepts role="presentation" or aria-hidden as well as alt="" for a decorative image.
|
|
589
|
+
if (tag === 'img' && !spread && !isPresentational(attrs) && !isAriaHidden(attrs)) {
|
|
590
|
+
const altAttr = findAttr(attrs, 'alt')
|
|
591
|
+
if (altAttr === undefined || isExplicitlyFalsyExpr(altAttr.value))
|
|
592
|
+
emitWarning(ctx, 'rgaa/img-alt', element.start, `[rgaa/img-alt] <img> in ${ctx.filename} is missing an alt attribute: add alt="description" or alt="" for decorative images.`, altAttr?.start)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if ((tag === 'input' || tag === 'select' || tag === 'textarea') && !spread && !ctx.inLabel && !hasLabelAttr(attrs)) {
|
|
596
|
+
const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
|
|
597
|
+
const idKey = attrTargetKey(findAttr(attrs, 'id'))
|
|
598
|
+
const linked = idKey !== undefined && ctx.labelTargets.has(idKey)
|
|
599
|
+
if ((inputType === undefined || !NON_LABELLED_INPUT_TYPES.has(inputType)) && !linked)
|
|
600
|
+
emitWarning(ctx, 'rgaa/form-label', element.start, `[rgaa/form-label] <${tag}> in ${ctx.filename} has no associated label: wrap it in <label>, link <label for="id">, or add aria-label="..." / aria-labelledby="...".`)
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if ((tag === 'button' || tag === 'a') && !spread && !hasLabelAttr(attrs) && !hasAccessibleName(element.children))
|
|
604
|
+
emitWarning(ctx, 'rgaa/accessible-name', element.start, `[rgaa/accessible-name] <${tag}> in ${ctx.filename} has no accessible name: add visible text or aria-label="...".`)
|
|
605
|
+
|
|
606
|
+
checkRoleAndAria(element, tag, ctx)
|
|
607
|
+
checkFocusAndInteraction(element, tag, spread, ctx)
|
|
608
|
+
checkStructureAndIdentity(element, tag, spread, ctx)
|
|
609
|
+
if (!spread) {
|
|
610
|
+
checkKeyboardReach(element, tag, ctx)
|
|
611
|
+
checkMissingNames(element, tag, ctx)
|
|
612
|
+
checkAutocomplete(element, tag, ctx)
|
|
613
|
+
}
|
|
614
|
+
}
|