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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion docs/rules/template-require-mandatory-role-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,40 @@ This rule **allows** the following:
<div role="option" aria-selected="false" />
<CustomComponent role="checkbox" aria-required="true" aria-checked="false" />
{{some-component role="heading" aria-level="2"}}

{{! Native inputs supply required ARIA state for matching roles. Lookup is
based on axobject-query's elementAXObjects + AXObjectRoles (see below). }}
<input type="checkbox" role="switch" />
<input type="checkbox" role="checkbox" />
<input type="radio" role="radio" />
<input type="range" role="slider" />
</template>
```

## Semantic-role exemptions

When the role attribute explicitly declares a role that the native element already provides, the native element supplies the required ARIA state and the rule does not flag missing attributes. The exemption is looked up via [axobject-query](https://github.com/A11yance/axobject-query)'s `elementAXObjects` + `AXObjectRoles` maps, matching the approach used by `eslint-plugin-jsx-a11y` and `@angular-eslint/template`.

Exempt pairings include (non-exhaustive):

| Element | Role | Required ARIA state supplied by |
| ------------------------- | -------------------- | ------------------------------------------------ |
| `<input type="checkbox">` | `checkbox`, `switch` | native `checked` state |
| `<input type="radio">` | `radio` | native `checked` state |
| `<input type="range">` | `slider` | native `value` / `min` / `max` |
| `<input type="number">` | `spinbutton` | native `value` (spinbutton has no required ARIA) |
| `<input type="text">` | `textbox` | no required ARIA |
| `<input type="search">` | `searchbox` | no required ARIA |
Comment thread
johanrd marked this conversation as resolved.

Undocumented pairings (e.g. `<input type="checkbox" role="menuitemcheckbox">` — axobject-query does not list this) remain flagged.

## References

- [WAI-ARIA Roles - Accessibility \_ MDN](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles)
- [WAI-ARIA Roles](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles)
- [WAI-ARIA APG — Switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/)
- [HTML-AAM — `<input type="checkbox">` → `checkbox` role mapping](https://www.w3.org/TR/html-aam-1.0/#el-input-checkbox)
— primary-spec source: HTML-AAM maps the native element to the
`checkbox` role and derives `aria-checked` from the element's
checkedness (and `indeterminate` for `mixed`). axobject-query
encodes that mapping for tooling.
- [axobject-query](https://github.com/A11yance/axobject-query) — AX-tree data source for the exemption lookup (secondary, encodes HTML-AAM)
138 changes: 127 additions & 11 deletions lib/rules/template-require-mandatory-role-attributes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
const { roles } = require('aria-query');

function createRequiredAttributeErrorMessage(attrs, role) {
if (attrs.length < 2) {
return `The attribute ${attrs[0]} is required by the role ${role}`;
}

return `The attributes ${attrs.join(', ')} are required by the role ${role}`;
}
const { AXObjectRoles, elementAXObjects } = require('axobject-query');

function getStaticRoleFromElement(node) {
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
Expand All @@ -28,13 +21,128 @@ function getStaticRoleFromMustache(node) {
return undefined;
}

function getMissingRequiredAttributes(role, foundAriaAttributes) {
// Reads the static lowercase value of `name` from either a GlimmerElementNode
// (angle-bracket attributes) or a GlimmerMustacheStatement (hash pairs).
// Returns undefined for dynamic values or missing attributes.
function getStaticAttrValue(node, name) {
if (node?.type === 'GlimmerElementNode') {
const attr = node.attributes?.find((a) => a.name === name);
if (attr?.value?.type === 'GlimmerTextNode') {
return attr.value.chars?.toLowerCase();
}
return undefined;
}
if (node?.type === 'GlimmerMustacheStatement') {
const pair = node.hash?.pairs?.find((p) => p.key === name);
if (pair?.value?.type === 'GlimmerStringLiteral') {
return pair.value.value?.toLowerCase();
}
return undefined;
}
return undefined;
}

function getTagName(node) {
if (node?.type === 'GlimmerElementNode') {
return node.tag;
}
if (node?.type === 'GlimmerMustacheStatement' && node.path?.original === 'input') {
// The classic `{{input}}` helper renders a native <input>.
Comment thread
johanrd marked this conversation as resolved.
//
// Caveat: in strict GJS/GTS mode, `{{input}}` is whatever was imported
// under the name `input` — it could be the classic helper (still renders
// native <input>) or some user-defined component. We assume the classic
// helper; the false-positive rate in practice is low because strict-mode
// authors rarely use `{{input}}` at all (idiomatic is <input> or
// <Input>), and when they do, it's almost always the imported built-in.
return 'input';
}
Comment thread
johanrd marked this conversation as resolved.
return null;
}

// Does this {element, role} pair match one of axobject-query's elementAXObjects
// concepts? If so, the native element exposes the role's required ARIA state
// automatically (e.g., <input type=checkbox> exposes aria-checked via the
// `checked` attribute for both role=checkbox and role=switch).
//
// Mirrors jsx-a11y's `isSemanticRoleElement` util
// (https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/src/util/isSemanticRoleElement.js).
//
// Pre-indexed at module load: elementAXObjects is static data, so we resolve
// each concept's exposed-role set once (walking axObjectNames → AXObjectRoles
// → role names) and bucket concepts by tag. That turns the per-call hot path
// into O(concepts-for-this-tag × attrs-on-that-concept), which in practice
// is a handful of entries. Benchmarked at ~12.5× faster than the naive full-
// map walk on a realistic 200k-call workload.
const AX_CONCEPTS_BY_TAG = buildAxConceptsByTag();

function buildAxConceptsByTag() {
const index = new Map();
for (const [concept, axObjectNames] of elementAXObjects) {
const roles = new Set();
for (const axName of axObjectNames) {
const axRoles = AXObjectRoles.get(axName);
if (!axRoles) {
continue;
}
for (const axRole of axRoles) {
roles.add(axRole.name);
}
}
const entry = { attributes: concept.attributes || [], roles };
if (!index.has(concept.name)) {
index.set(concept.name, []);
}
index.get(concept.name).push(entry);
}
return index;
}

function isSemanticRoleElement(node, role) {
const tag = getTagName(node);
if (!tag || typeof role !== 'string') {
return false;
}
const entries = AX_CONCEPTS_BY_TAG.get(tag);
if (!entries) {
return false;
}
const targetRole = role.toLowerCase();
for (const { attributes, roles } of entries) {
if (!roles.has(targetRole)) {
continue;
}
const allMatch = attributes.every((cAttr) => {
const nodeVal = getStaticAttrValue(node, cAttr.name);
if (nodeVal === undefined) {
return false;
}
if (cAttr.value === undefined) {
return true;
}
return nodeVal === String(cAttr.value).toLowerCase();
});
if (allMatch) {
return true;
}
}
return false;
}

function getMissingRequiredAttributes(role, foundAriaAttributes, node) {
const roleDefinition = roles.get(role);

if (!roleDefinition) {
return null;
}

// If axobject-query classifies this {element, role} pair as a semantic role
// element, the native element provides all required ARIA state — skip the
// missing-attribute check entirely (matches jsx-a11y's approach).
if (isSemanticRoleElement(node, role)) {
return null;
}

const requiredAttributes = Object.keys(roleDefinition.requiredProps);
const missingRequiredAttributes = requiredAttributes.filter(
(requiredAttribute) => !foundAriaAttributes.includes(requiredAttribute)
Expand Down Expand Up @@ -93,7 +201,11 @@ module.exports = {
.filter((attribute) => attribute.name?.startsWith('aria-'))
.map((attribute) => attribute.name);

const missingRequiredAttributes = getMissingRequiredAttributes(role, foundAriaAttributes);
const missingRequiredAttributes = getMissingRequiredAttributes(
role,
foundAriaAttributes,
node
);

if (missingRequiredAttributes) {
reportMissingAttributes(node, role, missingRequiredAttributes);
Expand All @@ -111,7 +223,11 @@ module.exports = {
.filter((pair) => pair.key.startsWith('aria-'))
.map((pair) => pair.key);

const missingRequiredAttributes = getMissingRequiredAttributes(role, foundAriaAttributes);
const missingRequiredAttributes = getMissingRequiredAttributes(
role,
foundAriaAttributes,
node
);

if (missingRequiredAttributes) {
reportMissingAttributes(node, role, missingRequiredAttributes);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"dependencies": {
"@ember-data/rfc395-data": "^0.0.4",
"aria-query": "^5.3.2",
"axobject-query": "^4.1.0",
"css-tree": "^3.0.1",
"editorconfig": "^3.0.2",
"ember-eslint-parser": "^0.10.0",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading