Skip to content

Commit

Permalink
fix: false positives for for attr in `astro/jsx-a11y/label-has-asso…
Browse files Browse the repository at this point in the history
…ciated-control` rule (#126)

* fix: false positives for `for` attr in `astro/jsx-a11y/label-has-associated-control` rule

* add test

* Create many-dodos-grin.md

* fix
  • Loading branch information
ota-meshi authored Oct 31, 2022
1 parent d469b22 commit 66d753e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-dodos-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": patch
---

fix: false positives for `for` attr in `astro/jsx-a11y/label-has-associated-control` rule
1 change: 1 addition & 0 deletions src/a11y/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const ATTRIBUTE_MAP: Record<string, string | undefined> = {
"set:html": "dangerouslySetInnerHTML",
"set:text": "children",
autofocus: "autoFocus",
for: "htmlFor",
}

/** Get `eslint-plugin-jsx-a11y` rule. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "A form label must be associated with a control.",
"line": 5,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
const domId = "foo"
---

<label>Surname</label>
<input type="text" id={domId} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
---

<form>
<label class="label" for="address">Address</label>
<textarea placeholder="Address" required name="address" id="address"
></textarea>
<input type="submit" value="Submit" />
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
const domId = "foo"
---

<label>
Surname
<input type="text" />
</label>
<label for={domId}>Surname</label>
<input type="text" id={domId} />
20 changes: 20 additions & 0 deletions tests/src/rules/jsx-a11y/label-has-associated-control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RuleTester } from "eslint"
import { rules } from "../../../../src/utils/rules"
import { loadTestCases } from "../../../utils/utils"

const rule = rules.find(
(r) => r.meta.docs.ruleName === "jsx-a11y/label-has-associated-control",
)!

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
})

tester.run(
"jsx-a11y/label-has-associated-control",
rule as any,
loadTestCases("jsx-a11y/label-has-associated-control"),
)

0 comments on commit 66d753e

Please sign in to comment.