eslint-plugin-jsx-a11y icon indicating copy to clipboard operation
eslint-plugin-jsx-a11y copied to clipboard

label-has-associated-control and label-has-for complaining in my .ts files

Open MendelBak opened this issue 3 years ago • 8 comments

I have .ts services/data store files in my React projects where I'm receiving complaints about the above linting errors. I literally cannot fix those issues to the satisfaction of ESLint, as I haven't written any JSX in those files, only JS/TS.

MendelBak avatar Jul 07 '22 13:07 MendelBak

Can you share the code that’s being warned?

ljharb avatar Jul 07 '22 15:07 ljharb

I am having this problem as well. I am receiving the following linting errors:

  Line 46:9:   A form label must be associated with a control  jsx-a11y/label-has-associated-control
  Line 51:9:   A form label must be associated with a control  jsx-a11y/label-has-associated-control

which correspond to the following lines in my "offending" tsx file:

<label htmlFor="input1">
  Input 1
  <input id="input1" />
</label>

<label htmlFor="input2">
  Input 2
  <input id="input2" /> 
</label>

My dependency version is "eslint-plugin-jsx-a11y": "6.4.1",

clarknc avatar Jul 12 '22 15:07 clarknc

@clarknc i agree, that should be valid. You should be using ^, so that you get the latest in-range - can you try updating to ^6.6.0 and see if the problem is fixed?

ljharb avatar Jul 12 '22 16:07 ljharb

I've made the suggested version change to my packjage.json and removed/reinstalled the dependency, but the problem seems to still be occurring.

clarknc avatar Jul 18 '22 15:07 clarknc

Same here with [email protected] in a tsx file. Here are the two code snippets that trigger false positives:

// real app code
<label style={{ marginLeft: 20 }}>
  <input
    type="checkbox"
    onChange={(evt) => setReadonly(evt.target.checked)}
  />
  Read only mode
</label>
// from docs
<label>
  Surname
  <input type="text" />
</label>

kachkaev avatar Aug 09 '22 14:08 kachkaev

@kachkaev those both also need an ID and a "for" to link the label and input.

ljharb avatar Aug 09 '22 18:08 ljharb

Hmm why should the last example fail? I copied it from rule docs.

kachkaev avatar Aug 09 '22 21:08 kachkaev

It depends on what the assert config is set to. "Both" requires both.

ljharb avatar Aug 09 '22 21:08 ljharb