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
9 changes: 9 additions & 0 deletions docs/rules/template-no-unsupported-role-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ This rule **allows** the following:
</template>
```

An element whose `role` is a dynamic expression is skipped: its role is not
knowable at lint time, so no ARIA attribute on it can be judged against a role.

```gjs
<template>
<div role={{if @useCase "dialog"}} aria-modal={{if @useCase "true"}}></div>
</template>
```

## References

- [Using ARIA, Roles, States, and Properties](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques)
24 changes: 20 additions & 4 deletions lib/rules/template-no-unsupported-role-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,20 @@ function getImplicitRole(node) {
return elementRoles.get(bestKey)[0];
}

const UNKNOWN_ROLE = Symbol('unknown role');

// Returns the statically known role, `undefined` when the element carries no
// `role` attribute (so the caller should fall back to the implicit role), or
// UNKNOWN_ROLE when a `role` attribute is present but its value is dynamic.
function getExplicitRole(node) {
const roleAttr = node.attributes?.find((attr) => attr.name === 'role');
if (roleAttr && roleAttr.value?.type === 'GlimmerTextNode') {
return roleAttr.value.chars.trim();
if (!roleAttr) {
return undefined;
}
return null;
if (roleAttr.value?.type !== 'GlimmerTextNode') {
return UNKNOWN_ROLE;
}
return roleAttr.value.chars.trim();
}

function removeRangeWithAdjacentWhitespace(sourceText, range) {
Expand Down Expand Up @@ -171,7 +179,15 @@ module.exports = {
let role = getExplicitRole(node);
let element;

if (!role) {
// A dynamic `role` leaves the element's role unknowable at lint time.
// Falling back to the implicit role would judge every aria-* attribute
// against a role the element may not have, and the fixer would then
// delete attributes that are valid for the role it resolves to.
if (role === UNKNOWN_ROLE) {
return;
}

if (role === undefined) {
element = node.tag;
role = getImplicitRole(node);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/template-no-unsupported-role-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const validHbs = [
'<input type="password" aria-checked="false" />',
'<input type="password" aria-selected="true" />',

// A dynamic `role` makes the element's role unknowable at lint time, so no
// aria-* attribute can be judged against it. Falling back to the implicit
// role here would flag attributes that are valid for the role the expression
// actually resolves to.
'<div role={{this.role}} aria-modal="true"></div>',
'<div role="{{this.role}}" aria-modal="true"></div>',
'<div role={{if @useCase "dialog"}} aria-modal={{if @useCase "true"}}></div>',
'<span role={{@role}} aria-level="2"></span>',

// <input type="text"> without a list attribute is a textbox — aria-required,
// aria-readonly, aria-placeholder are all supported.
'<input type="text" aria-required="true" />',
Expand Down
Loading