Skip to content

Commit 1ca5409

Browse files
committed
fix(template-no-aria-label-misuse): normalize whitespace and expand static attr detection
- Trim text-node chars in hasNonEmptyLabelAttr so aria-label=" " (whitespace only) is treated as empty rather than non-empty. - Extend getStaticAttrString to return '' for valueless attributes (e.g. <img alt>) and return the literal value for mustache string-literal paths (e.g. alt={{""}}), so constraint matching for alt="" → presentation role works correctly.
1 parent 03c3f03 commit 1ca5409

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

lib/rules/template-no-aria-label-misuse.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@ function findAttr(node, name) {
1414

1515
function getStaticAttrString(node, name) {
1616
const attr = findAttr(node, name);
17-
if (!attr || !attr.value || attr.value.type !== 'GlimmerTextNode') {
17+
if (!attr) {
1818
return null;
1919
}
20-
return attr.value.chars;
20+
// Valueless attribute (e.g. `<img alt>`) — treat as empty string.
21+
if (attr.value === null || attr.value === undefined) {
22+
return '';
23+
}
24+
if (attr.value.type === 'GlimmerTextNode') {
25+
return attr.value.chars;
26+
}
27+
// Mustache with a static string literal path (e.g. `alt={{""}}`).
28+
if (
29+
attr.value.type === 'GlimmerMustacheStatement' &&
30+
attr.value.path?.type === 'GlimmerStringLiteral'
31+
) {
32+
return attr.value.path.value;
33+
}
34+
return null;
2135
}
2236

2337
// Score how well an elementRoles entry matches the given node. Returns `null`
@@ -139,7 +153,7 @@ function hasNonEmptyLabelAttr(node, name) {
139153
return false;
140154
}
141155
if (attr.value.type === 'GlimmerTextNode') {
142-
return attr.value.chars !== '';
156+
return attr.value.chars.trim() !== '';
143157
}
144158
// Mustache with a static string literal path: `aria-label={{""}}` is still
145159
// empty, so treat it the same as an empty text node.

0 commit comments

Comments
 (0)