Skip to content

Commit 41bcb2e

Browse files
committed
test(require-input-label): cover id + aria-labelledby and wrapped-label cases
- Add valid: <input id aria-labelledby> (GJS + HBS) - Add invalid: <label><input id aria-label></label> still multipleLabels — locks in the corrected behavior after the validLabel && hasId carve-out was removed. - Drop peer-plugin reference from the id-skip comment; keep the spec-anchored reason (static analysis can't verify the <label for>) and inline the one-shot aria-label/labelledby locals.
1 parent 7316baf commit 41bcb2e

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

lib/rules/template-require-input-label.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,21 @@ module.exports = {
160160
labelCount++;
161161
}
162162

163-
const hasAriaLabel = hasAttr(node, 'aria-label');
164-
const hasAriaLabelledBy = hasAttr(node, 'aria-labelledby');
165-
if (hasAriaLabel) {
163+
if (hasAttr(node, 'aria-label')) {
166164
labelCount++;
167165
}
168-
if (hasAriaLabelledBy) {
166+
if (hasAttr(node, 'aria-labelledby')) {
169167
labelCount++;
170168
}
171169

172170
if (labelCount === 1) {
173171
return;
174172
}
175173

176-
// id alone is treated as a potential <label for> reference that static
177-
// analysis cannot verify (see vuejs-accessibility form-control-has-label
178-
// for the same rationale). Skip only when no other label is present to
179-
// avoid a false positive when id is combined with aria-label/labelledby.
174+
// An `id` may pair with a sibling `<label for>` we can't see in this
175+
// template. Treat id-only as valid to avoid false positives, but don't
176+
// count it toward labelCount — otherwise id + aria-label is wrongly
177+
// flagged as multiple labels.
180178
if (labelCount === 0 && hasAttr(node, 'id')) {
181179
return;
182180
}

tests/lib/rules/template-require-input-label.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ ruleTester.run('template-require-input-label', rule, {
2121
'<template><input aria-labelledby="someIdValue" /></template>',
2222
// https://github.com/ember-template-lint/ember-template-lint/issues/3388
2323
// id alone does not establish a labeling relationship — a <label for> is
24-
// needed and cannot be verified statically. Only aria-label should count.
24+
// needed and cannot be verified statically. Only aria-label/labelledby
25+
// should count.
2526
'<template><input id="hello" aria-label="hello" /></template>',
27+
'<template><input id="hello" aria-labelledby="someIdValue" /></template>',
2628
'<template><div></div></template>',
2729
'<template><Input id="foo" /></template>',
2830
'<template>{{input id="foo"}}</template>',
@@ -105,6 +107,13 @@ ruleTester.run('template-require-input-label', rule, {
105107
output: null,
106108
errors: [{ message: MULTIPLE_LABELS }],
107109
},
110+
{
111+
// id is irrelevant for labelling here — wrapping <label> + aria-label is
112+
// still multiple labels.
113+
code: '<template><label>Input label<input id="foo" aria-label="Custom label"></label></template>',
114+
output: null,
115+
errors: [{ message: MULTIPLE_LABELS }],
116+
},
108117
{
109118
code: '<template>{{input type="button"}}</template>',
110119
output: null,
@@ -172,6 +181,9 @@ hbsRuleTester.run('template-require-input-label', rule, {
172181
'<input id="probablyHasLabel" />',
173182
'<input aria-label={{labelText}} />',
174183
'<input aria-labelledby="someIdValue" />',
184+
// https://github.com/ember-template-lint/ember-template-lint/issues/3388
185+
'<input id="hello" aria-label="hello" />',
186+
'<input id="hello" aria-labelledby="someIdValue" />',
175187
'<div></div>',
176188
'<Input id="foo" />',
177189
'{{input id="foo"}}',
@@ -264,6 +276,11 @@ hbsRuleTester.run('template-require-input-label', rule, {
264276
output: null,
265277
errors: [{ message: MULTIPLE_LABELS }],
266278
},
279+
{
280+
code: '<label>Input label<input id="foo" aria-label="Custom label"></label>',
281+
output: null,
282+
errors: [{ message: MULTIPLE_LABELS }],
283+
},
267284
{
268285
code: '{{input type="button"}}',
269286
output: null,

0 commit comments

Comments
 (0)