Skip to content
Draft
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
32 changes: 31 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,39 @@ 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 |

Un-documented 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.1/#el-input-checkbox)
— primary-spec source: the native element's accessibility mapping supplies
the required ARIA state via the `checked` IDL attribute, which is what
axobject-query encodes.
- [axobject-query](https://github.com/A11yance/axobject-query) — AX-tree data source for the exemption lookup (secondary, encodes HTML-AAM)
116 changes: 105 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,106 @@ 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>.
return 'input';
}
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).
//
// Perf note: this walks the full `elementAXObjects` map for every call, giving
// an O(n·m) scan per node (n = concepts, m = axObject→roles). In practice the
// map is small (~dozens of entries) and callers only invoke this after a role
// attribute has already been matched, so it hasn't shown up as a hotspot.
// A future optimization could precompute a `{tag,role} → boolean` lookup.
function isSemanticRoleElement(node, role) {
const tag = getTagName(node);
if (!tag || typeof role !== 'string') {
return false;
}
const targetRole = role.toLowerCase();

for (const [concept, axObjectNames] of elementAXObjects) {
if (concept.name !== tag) {
continue;
}
const conceptAttrs = concept.attributes || [];
const allMatch = conceptAttrs.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) {
continue;
}

for (const axName of axObjectNames) {
const axRoles = AXObjectRoles.get(axName);
if (!axRoles) {
continue;
}
for (const axRole of axRoles) {
if (axRole.name === targetRole) {
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 +179,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 +201,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