/
no-autofocus.js
54 lines (46 loc) · 1.44 KB
/
no-autofocus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @fileoverview Enforce autoFocus prop is not used.
* @author Ethan Cohen <@evcohen>
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { propName, elementType } from 'jsx-ast-utils';
import { dom } from 'aria-query';
import { generateObjSchema } from '../util/schemas';
const errorMessage = 'The autoFocus prop should not be used, as it can reduce usability and accessibility for users.';
const schema = generateObjSchema({
ignoreNonDOM: {
type: 'boolean',
default: false,
},
});
export default {
meta: {
docs: {
url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/no-autofocus.md',
},
schema: [schema],
},
create: (context) => ({
JSXAttribute: (attribute) => {
// Determine if ignoreNonDOM is set to true
// If true, then do not run rule.
const options = context.options[0] || {};
const ignoreNonDOM = !!options.ignoreNonDOM;
if (ignoreNonDOM) {
const type = elementType(attribute.parent);
if (!dom.get(type)) {
return;
}
}
// Don't normalize, since React only recognizes autoFocus on low-level DOM elements.
if (propName(attribute) === 'autoFocus') {
context.report({
node: attribute,
message: errorMessage,
});
}
},
}),
};