Skip to content

Commit 91e39b4

Browse files
committed
[patch] no-autofocus: don't report error if autoFocus is set to false
This change adjusts the error condition for `no-autofocus` to allow for manually disabling `autoFocus`. Now, any usage of `autoFocus="false"` or `autoFocus={false}` will not report.
1 parent bfb0e9e commit 91e39b4

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ To restrict polymorphic linting to specified components, additionally set `polym
263263
| [mouse-events-have-key-events](docs/rules/mouse-events-have-key-events.md) | Enforce that `onMouseOver`/`onMouseOut` are accompanied by `onFocus`/`onBlur` for keyboard-only users. | ☑️ 🔒 | | | |
264264
| [no-access-key](docs/rules/no-access-key.md) | Enforce that the `accessKey` prop is not used on any element to avoid complications with keyboard commands used by a screen reader. | ☑️ 🔒 | | | |
265265
| [no-aria-hidden-on-focusable](docs/rules/no-aria-hidden-on-focusable.md) | Disallow `aria-hidden="true"` from being set on focusable elements. | | | | |
266-
| [no-autofocus](docs/rules/no-autofocus.md) | Enforce autoFocus prop is not used. | ☑️ 🔒 | | | |
266+
| [no-autofocus](docs/rules/no-autofocus.md) | Enforce autoFocus prop is not enabled. | ☑️ 🔒 | | | |
267267
| [no-distracting-elements](docs/rules/no-distracting-elements.md) | Enforce distracting elements are not used. | ☑️ 🔒 | | | |
268268
| [no-interactive-element-to-noninteractive-role](docs/rules/no-interactive-element-to-noninteractive-role.md) | Interactive elements should not be assigned non-interactive roles. | ☑️ 🔒 | | | |
269269
| [no-noninteractive-element-interactions](docs/rules/no-noninteractive-element-interactions.md) | Non-interactive elements should not be assigned mouse or keyboard event listeners. | ☑️ 🔒 | | | |

__tests__/src/rules/no-autofocus-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import rule from '../../../src/rules/no-autofocus';
1919
const ruleTester = new RuleTester();
2020

2121
const expectedError = {
22-
message: 'The autoFocus prop should not be used, as it can reduce usability and accessibility for users.',
22+
message: 'The autoFocus prop should not be enabled, as it can reduce usability and accessibility for users.',
2323
type: 'JSXAttribute',
2424
};
2525

@@ -43,6 +43,8 @@ ruleTester.run('no-autofocus', rule, {
4343
{ code: '<div autofocus />;' },
4444
{ code: '<input autofocus="true" />;' },
4545
{ code: '<Foo bar />' },
46+
{ code: '<div autoFocus={false} />' },
47+
{ code: '<div autoFocus="false" />' },
4648
{ code: '<Foo autoFocus />', options: ignoreNonDOMSchema },
4749
{ code: '<div><div autofocus /></div>', options: ignoreNonDOMSchema },
4850
{ code: '<Button />', settings: componentsSettings },
@@ -51,10 +53,8 @@ ruleTester.run('no-autofocus', rule, {
5153
invalid: parsers.all([].concat(
5254
{ code: '<div autoFocus />', errors: [expectedError] },
5355
{ code: '<div autoFocus={true} />', errors: [expectedError] },
54-
{ code: '<div autoFocus={false} />', errors: [expectedError] },
5556
{ code: '<div autoFocus={undefined} />', errors: [expectedError] },
5657
{ code: '<div autoFocus="true" />', errors: [expectedError] },
57-
{ code: '<div autoFocus="false" />', errors: [expectedError] },
5858
{ code: '<input autoFocus />', errors: [expectedError] },
5959
{ code: '<Foo autoFocus />', errors: [expectedError] },
6060
{ code: '<Button autoFocus />', errors: [expectedError], settings: componentsSettings },

docs/rules/no-autofocus.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- end auto-generated rule header -->
66

7-
Enforce that autoFocus prop is not used on elements. Autofocusing elements can cause usability issues for sighted and non-sighted users, alike.
7+
Enforce that `autoFocus` prop is either not set at all on elements or is only set to `false`. Auto-focusing elements can cause usability issues for sighted and non-sighted users, alike.
88

99
## Rule options
1010

@@ -25,13 +25,13 @@ For the `ignoreNonDOM` option, this determines if developer created components a
2525
### Succeed
2626
```jsx
2727
<div />
28+
<div autoFocus="false" />
2829
```
2930

3031
### Fail
3132
```jsx
3233
<div autoFocus />
3334
<div autoFocus="true" />
34-
<div autoFocus="false" />
3535
<div autoFocus={undefined} />
3636
```
3737

src/rules/no-autofocus.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// Rule Definition
88
// ----------------------------------------------------------------------------
99

10-
import { propName } from 'jsx-ast-utils';
10+
import { propName, getPropValue } from 'jsx-ast-utils';
1111
import { dom } from 'aria-query';
1212
import { generateObjSchema } from '../util/schemas';
1313
import getElementType from '../util/getElementType';
1414

15-
const errorMessage = 'The autoFocus prop should not be used, as it can reduce usability and accessibility for users.';
15+
const errorMessage = 'The autoFocus prop should not be enabled, as it can reduce usability and accessibility for users.';
1616

1717
const schema = generateObjSchema({
1818
ignoreNonDOM: {
@@ -25,7 +25,7 @@ export default {
2525
meta: {
2626
docs: {
2727
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-autofocus.md',
28-
description: 'Enforce autoFocus prop is not used.',
28+
description: 'Enforce autoFocus prop is not enabled.',
2929
},
3030
schema: [schema],
3131
},
@@ -46,8 +46,8 @@ export default {
4646
}
4747
}
4848

49-
// Don't normalize, since React only recognizes autoFocus on low-level DOM elements.
50-
if (propName(attribute) === 'autoFocus') {
49+
// Fail if autoFocus is used and the value is anything other than false (either via an expression or string literal).
50+
if (propName(attribute) === 'autoFocus' && getPropValue(attribute) !== false && getPropValue(attribute) !== 'false') {
5151
context.report({
5252
node: attribute,
5353
message: errorMessage,

0 commit comments

Comments
 (0)