prunelle 0.1.1 → 0.6.0

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.
Files changed (120) hide show
  1. package/llms.txt +130 -38
  2. package/package.json +8 -2
  3. package/src/a11y/declaration.ts +335 -0
  4. package/src/a11y/index.ts +446 -0
  5. package/src/a11y/rgaa-map.ts +184 -0
  6. package/src/attrs.ts +11 -3
  7. package/src/build/bundle.ts +80 -36
  8. package/src/build/client-entries.ts +3 -2
  9. package/src/build/client-plugin.ts +8 -4
  10. package/src/build/client-prune.ts +56 -6
  11. package/src/build/compile.ts +11 -9
  12. package/src/build/ecoindex.ts +229 -0
  13. package/src/build/index.ts +174 -108
  14. package/src/build/islands.ts +2 -2
  15. package/src/build/manifest.ts +66 -5
  16. package/src/build/output.ts +2 -2
  17. package/src/build/plugin-host.ts +92 -33
  18. package/src/build/precompress.ts +15 -2
  19. package/src/build/prerender.ts +128 -46
  20. package/src/build/report.ts +23 -2
  21. package/src/build/resolve.ts +8 -6
  22. package/src/build/routes.ts +175 -10
  23. package/src/build/static.ts +137 -39
  24. package/src/build/status-pages.ts +127 -0
  25. package/src/build/style-bundles.ts +526 -0
  26. package/src/cli/check-forms.ts +561 -0
  27. package/src/cli/check-links.ts +632 -0
  28. package/src/cli/check.ts +75 -20
  29. package/src/cli/commands.ts +85 -4
  30. package/src/cli/docs.ts +402 -0
  31. package/src/cli/inspect.ts +103 -14
  32. package/src/cli/mcp-tools.ts +490 -0
  33. package/src/cli/mcp.ts +205 -0
  34. package/src/cli/new.ts +2 -2
  35. package/src/cli/render.ts +421 -0
  36. package/src/cli/upgrade-names.ts +73 -13
  37. package/src/cli/upgrade.ts +137 -16
  38. package/src/compiler/analyze.ts +42 -9
  39. package/src/compiler/browser-globals.ts +140 -17
  40. package/src/compiler/cacheability.ts +636 -0
  41. package/src/compiler/compliance-rgaa.ts +508 -0
  42. package/src/compiler/compliance.ts +241 -289
  43. package/src/compiler/data-keys.ts +199 -192
  44. package/src/compiler/dev-source.ts +87 -0
  45. package/src/compiler/dom-attrs.ts +138 -65
  46. package/src/compiler/dom.ts +517 -442
  47. package/src/compiler/expr-context.ts +167 -57
  48. package/src/compiler/mistakes.ts +4 -1
  49. package/src/compiler/props.ts +87 -36
  50. package/src/compiler/scope.ts +400 -333
  51. package/src/compiler/server-only.ts +190 -151
  52. package/src/compiler/shared.ts +40 -12
  53. package/src/compiler/signal-reads.ts +6 -2
  54. package/src/compiler/ssr-prunelle-tags.ts +27 -14
  55. package/src/compiler/ssr.ts +218 -87
  56. package/src/dev/error-log.ts +151 -0
  57. package/src/dev/index.ts +84 -34
  58. package/src/dev/open-editor.ts +204 -0
  59. package/src/dev/protocol.ts +1 -0
  60. package/src/dev/reload.ts +5 -3
  61. package/src/dev/supervisor.ts +42 -25
  62. package/src/dev/watchers.ts +47 -11
  63. package/src/dom/action.ts +43 -20
  64. package/src/dom/attrs.ts +15 -3
  65. package/src/dom/extras.ts +28 -12
  66. package/src/dom/index.ts +26 -8
  67. package/src/dom/island.ts +35 -28
  68. package/src/dom/loop.ts +10 -9
  69. package/src/plugins/config.ts +8 -7
  70. package/src/plugins/index.ts +2 -2
  71. package/src/plugins/load.ts +26 -7
  72. package/src/plugins/order.ts +38 -1
  73. package/src/plugins/public.ts +5 -2
  74. package/src/plugins/services.ts +81 -0
  75. package/src/plugins/types.ts +34 -0
  76. package/src/reactivity/graph.ts +10 -0
  77. package/src/reactivity/index.ts +24 -16
  78. package/src/reactivity/selector.ts +31 -4
  79. package/src/reactivity/store.ts +45 -13
  80. package/src/server/action-protect.ts +198 -0
  81. package/src/server/action-registry.ts +1 -1
  82. package/src/server/action.ts +82 -27
  83. package/src/server/assets.ts +34 -2
  84. package/src/server/compress.ts +91 -9
  85. package/src/server/document.ts +25 -10
  86. package/src/server/errors.ts +303 -65
  87. package/src/server/form-schema.ts +343 -0
  88. package/src/server/health.ts +244 -0
  89. package/src/server/http.ts +131 -32
  90. package/src/server/index.ts +231 -261
  91. package/src/server/loader.ts +5 -0
  92. package/src/server/param-matchers.ts +112 -0
  93. package/src/server/plugin-host.ts +123 -88
  94. package/src/server/public.ts +3 -3
  95. package/src/server/rate-limit.ts +58 -35
  96. package/src/server/request-body.ts +1 -1
  97. package/src/server/route-cache.ts +423 -0
  98. package/src/server/route-table.ts +339 -8
  99. package/src/server/router.ts +125 -40
  100. package/src/server/routing.ts +196 -63
  101. package/src/server/security.ts +208 -32
  102. package/src/server/serve-handlers.ts +21 -7
  103. package/src/server/start.ts +60 -16
  104. package/src/server/static-dir.ts +61 -8
  105. package/src/server/stream.ts +41 -22
  106. package/src/ssr/context.ts +85 -3
  107. package/src/ssr/index.ts +130 -3
  108. package/src/ssr/region.ts +31 -19
  109. package/src/test/browser.ts +28 -13
  110. package/src/test/index.ts +97 -12
  111. package/src/ts-plugin/diagnostics.ts +505 -0
  112. package/src/ts-plugin/fixes.ts +364 -0
  113. package/src/ts-plugin/index.ts +194 -0
  114. package/templates/_shared/AGENTS.md +2 -2
  115. package/templates/_shared/mcp.json +11 -0
  116. package/templates/_shared/src/routes/legal.tsx +5 -0
  117. package/templates/_shared/tsconfig.json +5 -0
  118. package/templates/blog/prunelle.config.ts +1 -1
  119. package/templates/blog/src/routes/index.tsx +4 -0
  120. package/ts-plugin.cjs +4277 -0
@@ -0,0 +1,508 @@
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
+ ] as const
25
+
26
+ export type RgaaJsxRule = (typeof RGAA_JSX_RULES)[number]
27
+
28
+ const NON_LABELLED_INPUT_TYPES = new Set(['hidden', 'submit', 'reset', 'button', 'image'])
29
+ export const INTERACTIVE_ANCESTORS = new Set(['a', 'button', 'summary'])
30
+ const INTERACTIVE_DESCENDANTS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary'])
31
+ const NATIVE_CLICKABLE_TAGS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary', 'label', 'option', 'dialog', 'area'])
32
+ const TABLE_STRUCTURAL_TAGS = new Set(['thead', 'tbody', 'tfoot', 'tr'])
33
+ const HEADING_LEVELS: Record<string, number> = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }
34
+ const PRESENTATIONAL_ROLES = new Set(['presentation', 'none'])
35
+ const INT_RE = /^-?\d+$/
36
+ const NUM_RE = /^-?(?:\d+\.?\d*|\.\d+)$/
37
+ const TABINDEX_NUM_RE = /^[+-]?\d+(?:\.\d+)?$/
38
+
39
+ // Concrete WAI-ARIA 1.2 & 1.3 roles (abstract roles excluded).
40
+ const VALID_ARIA_ROLES = new Set((
41
+ 'alert alertdialog application article associationlist associationlistitemkey associationlistitemvalue '
42
+ + 'banner blockquote button caption cell checkbox code columnheader combobox comment complementary contentinfo '
43
+ + 'definition deletion dialog directory document emphasis feed figure form generic '
44
+ + 'graphics-document graphics-object graphics-symbol grid gridcell group heading image img insertion '
45
+ + 'link list listbox listitem log main mark marquee math menu menubar menuitem menuitemcheckbox menuitemradio '
46
+ + 'meter navigation none note option paragraph presentation progressbar radio radiogroup region row rowgroup rowheader '
47
+ + 'scrollbar search searchbox separator slider spinbutton status strong subscript suggestion superscript switch '
48
+ + 'tab table tablist tabpanel term textbox time timer toolbar tooltip tree treegrid treeitem'
49
+ ).split(' '))
50
+
51
+ // Valid WAI-ARIA 1.1 / 1.2 / 1.3 attributes.
52
+ const VALID_ARIA_ATTRS = new Set((
53
+ 'aria-activedescendant aria-atomic aria-autocomplete aria-braillelabel aria-brailleroledescription '
54
+ + 'aria-busy aria-checked aria-colcount aria-colindex aria-colindextext aria-colspan aria-controls '
55
+ + 'aria-current aria-describedby aria-description aria-details aria-disabled aria-dropeffect '
56
+ + 'aria-errormessage aria-expanded aria-flowto aria-grabbed aria-haspopup aria-hidden aria-invalid '
57
+ + 'aria-keyshortcuts aria-label aria-labelledby aria-level aria-live aria-modal aria-multiline '
58
+ + 'aria-multiselectable aria-orientation aria-owns aria-placeholder aria-posinset aria-pressed '
59
+ + 'aria-readonly aria-relevant aria-required aria-roledescription aria-rowcount aria-rowindex '
60
+ + 'aria-rowindextext aria-rowspan aria-selected aria-setsize aria-sort aria-valuemax aria-valuemin '
61
+ + 'aria-valuenow aria-valuetext'
62
+ ).split(' '))
63
+
64
+ const ARIA_BOOL_ATTRS = new Set((
65
+ 'aria-atomic aria-busy aria-disabled aria-expanded aria-grabbed aria-hidden '
66
+ + 'aria-modal aria-multiline aria-multiselectable aria-readonly aria-required aria-selected'
67
+ ).split(' '))
68
+ const ARIA_TRISTATE_ATTRS = new Set(['aria-checked', 'aria-pressed'])
69
+ const ARIA_INT_ATTRS = new Set((
70
+ 'aria-level aria-posinset aria-setsize aria-colcount aria-rowcount '
71
+ + 'aria-colindex aria-rowindex aria-colspan aria-rowspan'
72
+ ).split(' '))
73
+ const ARIA_NUM_ATTRS = new Set(['aria-valuemin', 'aria-valuemax', 'aria-valuenow'])
74
+ const ARIA_RELEVANT_TOKENS = new Set(['additions', 'removals', 'text', 'all'])
75
+ const ARIA_ENUM_ATTRS: Record<string, ReadonlySet<string>> = {
76
+ 'aria-live': new Set(['off', 'polite', 'assertive']),
77
+ 'aria-orientation': new Set(['horizontal', 'vertical']),
78
+ 'aria-sort': new Set(['none', 'ascending', 'descending', 'other']),
79
+ 'aria-current': new Set(['page', 'step', 'location', 'date', 'time', 'true', 'false']),
80
+ 'aria-haspopup': new Set(['false', 'true', 'menu', 'listbox', 'tree', 'grid', 'dialog']),
81
+ 'aria-invalid': new Set(['grammar', 'false', 'spelling', 'true']),
82
+ 'aria-autocomplete': new Set(['inline', 'list', 'both', 'none']),
83
+ }
84
+
85
+ export interface RgaaScope {
86
+ seenIds: Set<string>
87
+ lastHeading: number
88
+ }
89
+
90
+ export interface RgaaElementContext {
91
+ filename: string
92
+ inLabel: boolean
93
+ inLoop: boolean
94
+ interactiveAncestor: string | undefined
95
+ labelTargets: ReadonlySet<string>
96
+ scope: RgaaScope
97
+ isIgnored: (rule: RgaaJsxRule, elementStart: number, attrStart?: number) => boolean
98
+ warnings: CompilerWarning[]
99
+ }
100
+
101
+ export function createRgaaScope(): RgaaScope {
102
+ return { seenIds: new Set(), lastHeading: 0 }
103
+ }
104
+
105
+ export function cloneRgaaScope(scope: RgaaScope): RgaaScope {
106
+ return { seenIds: new Set(scope.seenIds), lastHeading: scope.lastHeading }
107
+ }
108
+
109
+ export function mergeRgaaScope(target: RgaaScope, branch: RgaaScope): void {
110
+ for (const id of branch.seenIds)
111
+ target.seenIds.add(id)
112
+ if (branch.lastHeading > target.lastHeading)
113
+ target.lastHeading = branch.lastHeading
114
+ }
115
+
116
+ export function findAttr(attributes: readonly JSXAttributeItem[], name: string): JSXAttribute | undefined {
117
+ for (const attr of attributes) {
118
+ if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name === name)
119
+ return attr
120
+ }
121
+ return undefined
122
+ }
123
+
124
+ function findAttrLower(attributes: readonly JSXAttributeItem[], lowerName: string): JSXAttribute | undefined {
125
+ for (const attr of attributes) {
126
+ if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name.toLowerCase() === lowerName)
127
+ return attr
128
+ }
129
+ return undefined
130
+ }
131
+
132
+ export function hasSpread(attributes: readonly JSXAttributeItem[]): boolean {
133
+ return attributes.some(a => a.type === 'JSXSpreadAttribute')
134
+ }
135
+
136
+ export function staticStringValueOf(value: JSXAttribute['value']): string | undefined {
137
+ if (value === null)
138
+ return undefined
139
+ if (value.type === 'Literal' && typeof value.value === 'string')
140
+ return value.value
141
+ if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
142
+ const expr = unwrapTsExpr(value.expression)
143
+ if (expr.type === 'Literal' && typeof expr.value === 'string')
144
+ return expr.value
145
+ if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
146
+ return expr.quasis[0]?.value.cooked ?? undefined
147
+ }
148
+ return undefined
149
+ }
150
+
151
+ export function attrTargetKey(attr: JSXAttribute | undefined): string | undefined {
152
+ if (attr === undefined || attr.value === null)
153
+ return undefined
154
+ const str = staticStringValueOf(attr.value)
155
+ if (str !== undefined && str.trim() !== '')
156
+ return `s:${str}`
157
+ if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
158
+ const expr = unwrapTsExpr(attr.value.expression)
159
+ if (expr.type === 'Identifier')
160
+ return `i:${expr.name}`
161
+ }
162
+ return undefined
163
+ }
164
+
165
+ export function hasNonEmptyAttr(attributes: readonly JSXAttributeItem[], name: string): boolean {
166
+ const attr = findAttr(attributes, name)
167
+ if (attr === undefined || attr.value === null)
168
+ return false
169
+ const str = staticStringValueOf(attr.value)
170
+ if (str !== undefined)
171
+ return str.trim() !== ''
172
+ if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
173
+ const expr = unwrapParens(attr.value.expression)
174
+ if (expr.type === 'Literal' && (expr.value === null || expr.value === false || expr.value === ''))
175
+ return false
176
+ if (expr.type === 'Identifier' && expr.name === 'undefined')
177
+ return false
178
+ return true
179
+ }
180
+ return false
181
+ }
182
+
183
+ function isExplicitlyFalsyExpr(value: JSXAttribute['value']): boolean {
184
+ if (value === null || value.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression')
185
+ return false
186
+ const expr = unwrapTsExpr(value.expression)
187
+ if (expr.type === 'Literal' && (expr.value === false || expr.value === null))
188
+ return true
189
+ return expr.type === 'Identifier' && expr.name === 'undefined'
190
+ }
191
+
192
+ function hasLabelAttr(attributes: readonly JSXAttributeItem[]): boolean {
193
+ return hasNonEmptyAttr(attributes, 'aria-label')
194
+ || hasNonEmptyAttr(attributes, 'aria-labelledby')
195
+ || hasNonEmptyAttr(attributes, 'title')
196
+ }
197
+
198
+ function isAriaHidden(attributes: readonly JSXAttributeItem[]): boolean {
199
+ const attr = findAttr(attributes, 'aria-hidden')
200
+ if (attr === undefined)
201
+ return false
202
+ if (attr.value === null)
203
+ return true
204
+ if (attr.value.type === 'Literal')
205
+ return attr.value.value === 'true'
206
+ if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
207
+ const expr = unwrapParens(attr.value.expression)
208
+ return expr.type === 'Literal' && (expr.value === true || expr.value === 'true')
209
+ }
210
+ return false
211
+ }
212
+
213
+ function isPresentational(attributes: readonly JSXAttributeItem[]): boolean {
214
+ const raw = staticStringValueOf(findAttr(attributes, 'role')?.value ?? null)
215
+ return raw !== undefined && raw.trim().split(/\s+/).some(token => PRESENTATIONAL_ROLES.has(token))
216
+ }
217
+
218
+ function hasMeaningfulExpression(expr: Node): boolean {
219
+ const inner = unwrapParens(expr)
220
+ if (inner.type === 'Literal')
221
+ return typeof inner.value === 'string' ? inner.value.trim() !== '' : typeof inner.value === 'number'
222
+ return !(inner.type === 'Identifier' && inner.name === 'undefined')
223
+ }
224
+
225
+ function hasAccessibleName(children: readonly JSXChild[]): boolean {
226
+ for (const child of children) {
227
+ if (child.type === 'JSXText') {
228
+ if (cleanJsxText(child.value).trim() !== '')
229
+ return true
230
+ }
231
+ else if (child.type === 'JSXExpressionContainer') {
232
+ if (child.expression.type !== 'JSXEmptyExpression' && hasMeaningfulExpression(child.expression))
233
+ return true
234
+ }
235
+ else if (child.type === 'JSXFragment') {
236
+ if (hasAccessibleName(child.children))
237
+ return true
238
+ }
239
+ else if (child.type === 'JSXElement') {
240
+ const attrs = child.openingElement.attributes
241
+ if (isAriaHidden(attrs))
242
+ continue
243
+ const tag = jsxTagName(child.openingElement.name)
244
+ if (hasSpread(attrs) || hasLabelAttr(attrs))
245
+ return true
246
+ if (tag === 'img') {
247
+ if (hasNonEmptyAttr(attrs, 'alt'))
248
+ return true
249
+ continue
250
+ }
251
+ if (tag === 'svg') {
252
+ if (child.children.some(c => c.type === 'JSXElement' && jsxTagName(c.openingElement.name) === 'title' && hasAccessibleName(c.children)))
253
+ return true
254
+ continue
255
+ }
256
+ if (isComponentTag(tag)) {
257
+ if (hasNonEmptyAttr(attrs, 'label') || hasAccessibleName(child.children))
258
+ return true
259
+ continue
260
+ }
261
+ if (hasAccessibleName(child.children))
262
+ return true
263
+ }
264
+ }
265
+ return false
266
+ }
267
+
268
+ type StaticAttrVal =
269
+ | { kind: 'bool', value: boolean }
270
+ | { kind: 'number', value: number }
271
+ | { kind: 'string', value: string }
272
+
273
+ function extractStaticAttrValue(attr: JSXAttribute): StaticAttrVal | undefined {
274
+ if (attr.value === null)
275
+ return { kind: 'bool', value: true }
276
+ if (attr.value.type === 'Literal' && typeof attr.value.value === 'string')
277
+ return { kind: 'string', value: attr.value.value }
278
+ if (attr.value.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
279
+ return undefined
280
+ const expr = unwrapTsExpr(attr.value.expression)
281
+ if (expr.type === 'Identifier' && expr.name === 'undefined')
282
+ return undefined
283
+ if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
284
+ return { kind: 'string', value: expr.quasis[0]?.value.cooked ?? '' }
285
+ if (expr.type === 'UnaryExpression' && expr.operator === '+' && expr.argument.type === 'Literal' && typeof expr.argument.value === 'number')
286
+ return { kind: 'number', value: expr.argument.value }
287
+ const lit = staticLiteral(expr)
288
+ if (!lit.ok || lit.value === null)
289
+ return undefined
290
+ if (typeof lit.value === 'boolean')
291
+ return { kind: 'bool', value: lit.value }
292
+ if (typeof lit.value === 'number')
293
+ return { kind: 'number', value: lit.value }
294
+ return { kind: 'string', value: lit.value }
295
+ }
296
+
297
+ function validateAriaValue(name: string, val: StaticAttrVal): string | undefined {
298
+ if (ARIA_BOOL_ATTRS.has(name))
299
+ return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false')) ? undefined : 'expected "true" or "false"'
300
+ if (ARIA_TRISTATE_ATTRS.has(name))
301
+ return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false' || val.value === 'mixed')) ? undefined : 'expected "true", "false", or "mixed"'
302
+ const enumSet = ARIA_ENUM_ATTRS[name]
303
+ if (enumSet !== undefined) {
304
+ const token = val.kind === 'bool' ? String(val.value) : val.kind === 'string' ? val.value : undefined
305
+ return token !== undefined && enumSet.has(token) ? undefined : `expected one of ${[...enumSet].join(', ')}`
306
+ }
307
+ if (name === 'aria-relevant') {
308
+ const tokens = val.kind === 'string' ? val.value.trim().split(/\s+/).filter(Boolean) : []
309
+ return tokens.length > 0 && tokens.every(t => ARIA_RELEVANT_TOKENS.has(t)) ? undefined : 'expected space-separated tokens from additions, removals, text, all'
310
+ }
311
+ if (ARIA_INT_ATTRS.has(name))
312
+ return (val.kind === 'number' && Number.isInteger(val.value)) || (val.kind === 'string' && INT_RE.test(val.value.trim())) ? undefined : 'expected an integer'
313
+ if (ARIA_NUM_ATTRS.has(name))
314
+ return (val.kind === 'number' && Number.isFinite(val.value)) || (val.kind === 'string' && NUM_RE.test(val.value.trim())) ? undefined : 'expected a number'
315
+ return undefined
316
+ }
317
+
318
+ function emitWarning(ctx: RgaaElementContext, rule: RgaaJsxRule, elementStart: number, message: string, attrStart?: number): void {
319
+ if (!ctx.isIgnored(rule, elementStart, attrStart))
320
+ ctx.warnings.push({ rule, start: attrStart ?? elementStart, message })
321
+ }
322
+
323
+ function checkRoleAndAria(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
324
+ const attrs = element.openingElement.attributes
325
+ const roleAttr = findAttr(attrs, 'role')
326
+ if (roleAttr !== undefined) {
327
+ const raw = staticStringValueOf(roleAttr.value)
328
+ if (raw !== undefined) {
329
+ const tokens = raw.trim().split(/\s+/).filter(Boolean)
330
+ const invalid = tokens.filter(t => !VALID_ARIA_ROLES.has(t))
331
+ if (tokens.length === 0 || invalid.length > 0) {
332
+ const detail = tokens.length === 0 ? 'an empty role' : `unknown WAI-ARIA role "${invalid[0]}"`
333
+ 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)
334
+ }
335
+ }
336
+ }
337
+ for (const attr of attrs) {
338
+ if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier' || !attr.name.name.startsWith('aria-'))
339
+ continue
340
+ const name = attr.name.name
341
+ if (!VALID_ARIA_ATTRS.has(name)) {
342
+ 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)
343
+ continue
344
+ }
345
+ const val = extractStaticAttrValue(attr)
346
+ if (val === undefined)
347
+ continue
348
+ const err = validateAriaValue(name, val)
349
+ if (err !== undefined) {
350
+ const shown = val.kind === 'string' ? `"${val.value}"` : String(val.value)
351
+ 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)
352
+ }
353
+ }
354
+ }
355
+
356
+ function checkExprForChild(expr: Node, checkElement: (el: JSXElement) => boolean, checkFragment: (children: readonly JSXChild[]) => boolean): boolean {
357
+ const inner = unwrapTsExpr(expr)
358
+ if (inner.type === 'Literal' || (inner.type === 'Identifier' && inner.name === 'undefined'))
359
+ return false
360
+ if (inner.type === 'JSXElement')
361
+ return checkElement(inner)
362
+ if (inner.type === 'JSXFragment')
363
+ return checkFragment(inner.children)
364
+ if (inner.type === 'LogicalExpression') {
365
+ return inner.operator === '&&'
366
+ ? checkExprForChild(inner.right, checkElement, checkFragment)
367
+ : checkExprForChild(inner.left, checkElement, checkFragment) || checkExprForChild(inner.right, checkElement, checkFragment)
368
+ }
369
+ if (inner.type === 'ConditionalExpression')
370
+ return checkExprForChild(inner.consequent, checkElement, checkFragment) || checkExprForChild(inner.alternate, checkElement, checkFragment)
371
+ return true
372
+ }
373
+
374
+ function tableMayHaveHeader(children: readonly JSXChild[]): boolean {
375
+ const checkEl = (el: JSXElement): boolean => {
376
+ const tag = jsxTagName(el.openingElement.name)
377
+ if (tag === 'th' || isComponentTag(tag))
378
+ return true
379
+ return TABLE_STRUCTURAL_TAGS.has(tag) && tableMayHaveHeader(el.children)
380
+ }
381
+ for (const child of children) {
382
+ if (child.type === 'JSXElement' && checkEl(child))
383
+ return true
384
+ if (child.type === 'JSXFragment' && tableMayHaveHeader(child.children))
385
+ return true
386
+ if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, tableMayHaveHeader))
387
+ return true
388
+ }
389
+ return false
390
+ }
391
+
392
+ function fieldsetMayHaveLegend(children: readonly JSXChild[], isDirect: boolean): boolean {
393
+ const checkEl = (el: JSXElement): boolean => {
394
+ const tag = jsxTagName(el.openingElement.name)
395
+ if (tag === 'legend')
396
+ return true
397
+ if (tag === 'fieldset')
398
+ return false
399
+ return (isDirect && isComponentTag(tag)) || fieldsetMayHaveLegend(el.children, false)
400
+ }
401
+ for (const child of children) {
402
+ if (child.type === 'JSXElement' && checkEl(child))
403
+ return true
404
+ if (child.type === 'JSXFragment' && fieldsetMayHaveLegend(child.children, isDirect))
405
+ return true
406
+ if (isDirect && child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, cs => fieldsetMayHaveLegend(cs, true)))
407
+ return true
408
+ }
409
+ return false
410
+ }
411
+
412
+ function checkFocusAndInteraction(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
413
+ const attrs = element.openingElement.attributes
414
+
415
+ const tabAttr = findAttrLower(attrs, 'tabindex')
416
+ if (tabAttr !== undefined) {
417
+ const val = extractStaticAttrValue(tabAttr)
418
+ const num = val?.kind === 'number' ? val.value : val?.kind === 'string' && TABINDEX_NUM_RE.test(val.value.trim()) ? Number(val.value.trim()) : undefined
419
+ if (num !== undefined && Number.isFinite(num) && num > 0) {
420
+ const attrName = tabAttr.name.type === 'JSXIdentifier' ? tabAttr.name.name : 'tabindex'
421
+ 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)
422
+ }
423
+ }
424
+
425
+ const autoFocusAttr = findAttrLower(attrs, 'autofocus')
426
+ if (autoFocusAttr !== undefined && !isExplicitlyFalsyExpr(autoFocusAttr.value)) {
427
+ const attrName = autoFocusAttr.name.type === 'JSXIdentifier' ? autoFocusAttr.name.name : 'autofocus'
428
+ 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)
429
+ }
430
+
431
+ if (ctx.interactiveAncestor !== undefined && INTERACTIVE_DESCENDANTS.has(tag)) {
432
+ const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
433
+ if (inputType !== 'hidden')
434
+ 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.`)
435
+ }
436
+
437
+ const onClickAttr = findAttr(attrs, 'onClick')
438
+ const hasClick = onClickAttr !== undefined && !isExplicitlyFalsyExpr(onClickAttr.value)
439
+ if (tag === 'a' && !spread && hasClick && !hasNonEmptyAttr(attrs, 'href'))
440
+ 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.`)
441
+
442
+ if (hasClick && !spread && !NATIVE_CLICKABLE_TAGS.has(tag) && !isPresentational(attrs)) {
443
+ const hasKey = findAttr(attrs, 'onKeyDown') !== undefined || findAttr(attrs, 'onKeyUp') !== undefined || findAttr(attrs, 'onKeyPress') !== undefined
444
+ if (!hasKey)
445
+ 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)
446
+ }
447
+ }
448
+
449
+ function checkStructureAndIdentity(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
450
+ const attrs = element.openingElement.attributes
451
+
452
+ const idAttr = findAttr(attrs, 'id')
453
+ const id = idAttr !== undefined ? staticStringValueOf(idAttr.value) : undefined
454
+ if (id !== undefined && id !== '') {
455
+ if (ctx.inLoop)
456
+ 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)
457
+ else if (ctx.scope.seenIds.has(id))
458
+ 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)
459
+ else
460
+ ctx.scope.seenIds.add(id)
461
+ }
462
+
463
+ const headingLevel = HEADING_LEVELS[tag]
464
+ if (headingLevel !== undefined) {
465
+ if (ctx.scope.lastHeading > 0 && headingLevel > ctx.scope.lastHeading + 1)
466
+ 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.`)
467
+ ctx.scope.lastHeading = headingLevel
468
+ }
469
+
470
+ if (tag === 'table' && !isPresentational(attrs) && !(element.children.length === 0 && spread) && !tableMayHaveHeader(element.children))
471
+ 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.`)
472
+
473
+ if (tag === 'fieldset' && !(element.children.length === 0 && spread) && !fieldsetMayHaveLegend(element.children, true))
474
+ 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.`)
475
+
476
+ if (tag === 'meta') {
477
+ const httpEquivAttr = findAttrLower(attrs, 'http-equiv') ?? findAttrLower(attrs, 'httpequiv')
478
+ if (httpEquivAttr !== undefined && staticStringValueOf(httpEquivAttr.value)?.trim().toLowerCase() === 'refresh')
479
+ 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)
480
+ }
481
+ }
482
+
483
+ /** Runs all static RGAA JSX compliance checks on an intrinsic HTML element. */
484
+ export function checkRgaaElement(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
485
+ const attrs = element.openingElement.attributes
486
+ const spread = hasSpread(attrs)
487
+
488
+ if (tag === 'img' && !spread) {
489
+ const altAttr = findAttr(attrs, 'alt')
490
+ if (altAttr === undefined || isExplicitlyFalsyExpr(altAttr.value))
491
+ 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)
492
+ }
493
+
494
+ if ((tag === 'input' || tag === 'select' || tag === 'textarea') && !spread && !ctx.inLabel && !hasLabelAttr(attrs)) {
495
+ const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
496
+ const idKey = attrTargetKey(findAttr(attrs, 'id'))
497
+ const linked = idKey !== undefined && ctx.labelTargets.has(idKey)
498
+ if ((inputType === undefined || !NON_LABELLED_INPUT_TYPES.has(inputType)) && !linked)
499
+ 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="...".`)
500
+ }
501
+
502
+ if ((tag === 'button' || tag === 'a') && !spread && !hasLabelAttr(attrs) && !hasAccessibleName(element.children))
503
+ emitWarning(ctx, 'rgaa/accessible-name', element.start, `[rgaa/accessible-name] <${tag}> in ${ctx.filename} has no accessible name: add visible text or aria-label="...".`)
504
+
505
+ checkRoleAndAria(element, tag, ctx)
506
+ checkFocusAndInteraction(element, tag, spread, ctx)
507
+ checkStructureAndIdentity(element, tag, spread, ctx)
508
+ }