Skip to content

Commit 1c19f4f

Browse files
authored
Add ignoreNonDOM option to no-autofocus. (#213)
1 parent acae0a2 commit 1c19f4f

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ const expectedError = {
2323
type: 'JSXAttribute',
2424
};
2525

26+
const ignoreNonDOMSchema = [
27+
{
28+
ignoreNonDOM: true,
29+
},
30+
];
31+
2632
ruleTester.run('no-autofocus', rule, {
2733
valid: [
2834
{ code: '<div />;' },
2935
{ code: '<div autofocus />;' },
3036
{ code: '<input autofocus="true" />;' },
37+
{ code: '<Foo bar />' },
38+
{ code: '<Foo autoFocus />', options: ignoreNonDOMSchema },
3139
].map(parserOptionsMapper),
3240
invalid: [
3341
{ code: '<div autoFocus />', errors: [expectedError] },
@@ -37,5 +45,6 @@ ruleTester.run('no-autofocus', rule, {
3745
{ code: '<div autoFocus="true" />', errors: [expectedError] },
3846
{ code: '<div autoFocus="false" />', errors: [expectedError] },
3947
{ code: '<input autoFocus />', errors: [expectedError] },
48+
{ code: '<Foo autoFocus />', errors: [expectedError] },
4049
].map(parserOptionsMapper),
4150
});

docs/rules/no-autofocus.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ Enforce that autoFocus prop is not used on elements. Autofocusing elements can c
77

88
## Rule details
99

10-
This rule takes no arguments.
10+
This rule takes one optional object argument of type object:
11+
12+
```
13+
{
14+
"rules": {
15+
"jsx-a11y/no-autofocus": [ 2, {
16+
"ignoreNonDOM": true
17+
}],
18+
}
19+
}
20+
```
21+
22+
For the `ignoreNonDOM` option, this determines if developer created components are checked.
1123

1224
### Succeed
1325
```jsx

src/rules/no-autofocus.js

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

10-
import { propName } from 'jsx-ast-utils';
10+
import { propName, elementType } from 'jsx-ast-utils';
11+
import { dom } from 'aria-query';
1112
import { generateObjSchema } from '../util/schemas';
1213

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

15-
const schema = generateObjSchema();
17+
const schema = generateObjSchema({
18+
ignoreNonDOM: {
19+
type: 'boolean',
20+
default: false,
21+
},
22+
});
1623

1724
module.exports = {
1825
meta: {
@@ -22,6 +29,18 @@ module.exports = {
2229

2330
create: context => ({
2431
JSXAttribute: (attribute) => {
32+
// Determine if ignoreNonDOM is set to true
33+
// If true, then do not run rule.
34+
const options = context.options[0] || {};
35+
const ignoreNonDOM = !!options.ignoreNonDOM;
36+
37+
if (ignoreNonDOM) {
38+
const type = elementType(attribute.parent);
39+
if (!dom.get(type)) {
40+
return;
41+
}
42+
}
43+
2544
// Don't normalize, since React only recognizes autoFocus on low-level DOM elements.
2645
if (propName(attribute) === 'autoFocus') {
2746
context.report({

0 commit comments

Comments
 (0)