Skip to content

Commit ef13138

Browse files
Just keep the global setting.
1 parent 2031c01 commit ef13138

6 files changed

+26
-17
lines changed

__tests__/src/rules/label-has-associated-control-test.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,23 @@ const componentsSettings = {
3535
},
3636
};
3737

38+
const attributesSettings = {
39+
'jsx-a11y': {
40+
attributes: {
41+
for: ['htmlFor', 'for'],
42+
},
43+
},
44+
};
45+
3846
const htmlForValid = [
3947
{ code: '<label htmlFor="js_id"><span><span><span>A label</span></span></span></label>', options: [{ depth: 4 }] },
4048
{ code: '<label htmlFor="js_id" aria-label="A label" />' },
4149
{ code: '<label htmlFor="js_id" aria-labelledby="A label" />' },
4250
{ code: '<div><label htmlFor="js_id">A label</label><input id="js_id" /></div>' },
43-
{ code: '<label for="js_id"><span><span><span>A label</span></span></span></label>', options: [{ depth: 4, htmlForAttributes: ['htmlFor', 'for'] }] },
44-
{ code: '<label for="js_id" aria-label="A label" />', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
45-
{ code: '<label for="js_id" aria-labelledby="A label" />', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
46-
{ code: '<div><label for="js_id">A label</label><input id="js_id" /></div>', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
51+
{ code: '<label for="js_id"><span><span><span>A label</span></span></span></label>', options: [{ depth: 4 }], settings: attributesSettings },
52+
{ code: '<label for="js_id" aria-label="A label" />', settings: attributesSettings },
53+
{ code: '<label for="js_id" aria-labelledby="A label" />', settings: attributesSettings },
54+
{ code: '<div><label for="js_id">A label</label><input id="js_id" /></div>', settings: attributesSettings },
4755
// Custom label component.
4856
{ code: '<CustomLabel htmlFor="js_id" aria-label="A label" />', options: [{ labelComponents: ['CustomLabel'] }] },
4957
{ code: '<CustomLabel htmlFor="js_id" label="A label" />', options: [{ labelAttributes: ['label'], labelComponents: ['CustomLabel'] }] },

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,28 @@ const optionsChildrenAllowed = [{
5050
allowChildren: true,
5151
}];
5252

53+
const attributesSettings = {
54+
'jsx-a11y': {
55+
attributes: {
56+
for: ['htmlFor', 'for'],
57+
},
58+
},
59+
};
60+
5361
ruleTester.run('label-has-for', rule, {
5462
valid: parsers.all([].concat(
5563
// DEFAULT ELEMENT 'label' TESTS
5664
{ code: '<div />' },
5765
{ code: '<label htmlFor="foo"><input /></label>' },
5866
{ code: '<label htmlFor="foo"><textarea /></label>' },
59-
{ code: '<label for="foo"><input /></label>', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
60-
{ code: '<label for="foo"><textarea /></label>', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
67+
{ code: '<label for="foo"><input /></label>', settings: attributesSettings },
68+
{ code: '<label for="foo"><textarea /></label>', settings: attributesSettings },
6169
{ code: '<Label />' }, // lower-case convention refers to real HTML elements.
6270
{ code: '<Label htmlFor="foo" />' },
63-
{ code: '<Label for="foo" />', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
71+
{ code: '<Label for="foo" />', settings: attributesSettings },
6472
{ code: '<Descriptor />' },
6573
{ code: '<Descriptor htmlFor="foo">Test!</Descriptor>' },
66-
{ code: '<Descriptor for="foo">Test!</Descriptor>', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
74+
{ code: '<Descriptor for="foo">Test!</Descriptor>', settings: attributesSettings },
6775
{ code: '<UX.Layout>test</UX.Layout>' },
6876

6977
// CUSTOM ELEMENT ARRAY OPTION TESTS

docs/rules/label-has-associated-control.md

-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ And the configuration:
5353
{
5454
"rules": {
5555
"jsx-a11y/label-has-associated-control": [ 2, {
56-
"htmlForAttributes": ["htmlFor", "for"],
5756
"labelComponents": ["CustomInputLabel"],
5857
"labelAttributes": ["label"],
5958
"controlComponents": ["CustomInput"],
@@ -104,7 +103,6 @@ This rule takes one optional object argument of type object:
104103
}
105104
```
106105

107-
`htmlForAttributes`: is an array of strings that specify the attribute to check for an associated control. Default is `["htmlFor"]`.
108106
`labelComponents` is a list of custom React Component names that should be checked for an associated control.
109107
`labelAttributes` is a list of attributes to check on the label component and its children for a label. Use this if you have a custom component that uses a string passed on a prop to render an HTML `label`, for example.
110108
`controlComponents` is a list of custom React Components names that will output an input element. [Glob format](https://linuxhint.com/bash_globbing_tutorial/) is also supported for specifying names (e.g., `Label*` matches `LabelComponent` but not `CustomLabel`, `????Label` matches `LinkLabel` but not `CustomLabel`).

docs/rules/label-has-for.md

-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ This rule takes one optional object argument of type object:
2525
{
2626
"rules": {
2727
"jsx-a11y/label-has-for": [ 2, {
28-
"htmlForAttributes": ["htmlFor", "for"],
2928
"components": [ "Label" ],
3029
"required": {
3130
"every": [ "nesting", "id" ]
@@ -36,8 +35,6 @@ This rule takes one optional object argument of type object:
3635
}
3736
```
3837

39-
The `htmlForAttributes` allows you to specify which prop to check for. This is useful when you want to use a different property instead of `htmlFor`, for example `for`.
40-
4138
For the `components` option, these strings determine which JSX elements (**always including** `<label>`) should be checked for having `htmlFor` prop. This is a good use case when you have a wrapper component that simply renders a `label` element (like in React):
4239

4340
```js

src/rules/label-has-associated-control.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import mayHaveAccessibleLabel from '../util/mayHaveAccessibleLabel';
2020
const errorMessage = 'A form label must be associated with a control.';
2121

2222
const schema = generateObjSchema({
23-
htmlForAttributes: arraySchema,
2423
labelComponents: arraySchema,
2524
labelAttributes: arraySchema,
2625
controlComponents: arraySchema,
@@ -62,7 +61,7 @@ export default ({
6261
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
6362
const { settings } = context;
6463
const options = context.options[0] || {};
65-
const htmlForAttributes = options.htmlForAttributes ?? settings['jsx-a11y']?.attributes?.for ?? ['htmlFor'];
64+
const htmlForAttributes = settings['jsx-a11y']?.attributes?.for ?? ['htmlFor'];
6665
const labelComponents = options.labelComponents || [];
6766
const assertType = options.assert || 'either';
6867
const componentNames = ['label'].concat(labelComponents);

src/rules/label-has-for.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const enumValues = ['nesting', 'id'];
1616
const schema = {
1717
type: 'object',
1818
properties: {
19-
htmlForAttributes: arraySchema,
2019
components: arraySchema,
2120
required: {
2221
oneOf: [
@@ -110,7 +109,7 @@ export default {
110109
JSXOpeningElement: (node) => {
111110
const { settings } = context;
112111
const options = context.options[0] || {};
113-
const htmlForAttributes = options.htmlForAttributes ?? settings['jsx-a11y']?.attributes?.for ?? ['htmlFor'];
112+
const htmlForAttributes = settings['jsx-a11y']?.attributes?.for ?? ['htmlFor'];
114113
const componentOptions = options.components || [];
115114
const typesToValidate = ['label'].concat(componentOptions);
116115
const nodeType = elementType(node);

0 commit comments

Comments
 (0)