Skip to content

Commit c1f763a

Browse files
authored
Merge pull request #439 from ljharb/fix_label_has_for_nesting
[Fix] `label-has-for`: fix issue with improper nesting validation
2 parents 0fcf3ef + 672cd3d commit c1f763a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

__tests__/src/rules/label-has-for-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,15 @@ ruleTester.run('label-has-for', rule, {
165165
{ code: '<label>First Name</label>', errors: [expectedSomeError], options: optionsRequiredSome },
166166
{ code: '<label>{children}</label>', errors: [expectedSomeError], options: optionsRequiredSome },
167167
{ code: '<label>{children}</label>', errors: [expectedNestingError], options: optionsRequiredNesting },
168+
{
169+
code: '<form><input type="text" id="howmuch" value="1" /><label htmlFor="howmuch">How much ?</label></form>',
170+
errors: [expectedEveryError],
171+
options: optionsRequiredEvery,
172+
},
173+
{
174+
code: '<form><input type="text" id="howmuch" value="1" /><label htmlFor="howmuch">How much ?<span /></label></form>',
175+
errors: [expectedEveryError],
176+
options: optionsRequiredEvery,
177+
},
168178
].map(parserOptionsMapper),
169179
});

src/rules/label-has-for.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const schema = {
2727
},
2828
};
2929

30-
const validateNesting = node => node.parent.children.some(child => child.type === 'JSXElement');
30+
const validateNesting = node => node.parent.children.some((child) => {
31+
const opener = child.openingElement;
32+
return child.type === 'JSXElement' && opener && opener.name.name === 'input';
33+
});
3134

3235
const validateId = (node) => {
3336
const htmlForAttr = getProp(node.attributes, 'htmlFor');

0 commit comments

Comments
 (0)