-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
no-noninteractive-element-interactions.js
110 lines (102 loc) · 3.81 KB
/
no-noninteractive-element-interactions.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @fileoverview Enforce non-interactive elements have no interactive handlers.
* @author Jese Beach
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { dom } from 'aria-query';
import {
elementType,
eventHandlersByType,
getPropValue,
getProp,
hasProp,
propName,
} from 'jsx-ast-utils';
import type { JSXOpeningElement } from 'ast-types-flow';
import includes from 'array-includes';
import has from 'has';
import type { ESLintConfig, ESLintContext, ESLintVisitorSelectorConfig } from '../../flow/eslint';
import { arraySchema, generateObjSchema } from '../util/schemas';
import isAbstractRole from '../util/isAbstractRole';
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
import isInteractiveElement from '../util/isInteractiveElement';
import isInteractiveRole from '../util/isInteractiveRole';
import isNonInteractiveElement from '../util/isNonInteractiveElement';
import isNonInteractiveRole from '../util/isNonInteractiveRole';
import isPresentationRole from '../util/isPresentationRole';
const errorMessage = 'Non-interactive elements should not be assigned mouse or keyboard event listeners.';
const domElements = [...dom.keys()];
const defaultInteractiveProps = [].concat(
eventHandlersByType.focus,
eventHandlersByType.image,
eventHandlersByType.keyboard,
eventHandlersByType.mouse,
);
const schema = generateObjSchema({
handlers: arraySchema,
});
export default ({
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-noninteractive-element-interactions.md',
description: 'Non-interactive elements should not be assigned mouse or keyboard event listeners.',
errorOptions: true,
},
schema: [schema],
},
create: (context: ESLintContext): ESLintVisitorSelectorConfig => {
const { options } = context;
return {
JSXOpeningElement: (node: JSXOpeningElement) => {
let { attributes } = node;
const type = elementType(node);
const config = (options[0] || {});
const interactiveProps = config.handlers || defaultInteractiveProps;
// Allow overrides from rule configuration for specific elements and roles.
if (has(config, type)) {
attributes = attributes.filter((attr) => attr.type !== 'JSXSpreadAttribute' && !includes(config[type], propName(attr)));
}
const hasInteractiveProps = interactiveProps
.some((prop) => (
hasProp(attributes, prop)
&& getPropValue(getProp(attributes, prop)) != null
));
if (!includes(domElements, type)) {
// Do not test higher level JSX components, as we do not know what
// low-level DOM element this maps to.
return;
}
if (
!hasInteractiveProps
|| isHiddenFromScreenReader(type, attributes)
|| isPresentationRole(type, attributes)
) {
// Presentation is an intentional signal from the author that this
// element is not meant to be perceivable. For example, a click screen
// to close a dialog .
return;
}
if (
isInteractiveElement(type, attributes)
|| isInteractiveRole(type, attributes)
|| (
!isNonInteractiveElement(type, attributes)
&& !isNonInteractiveRole(type, attributes)
)
|| isAbstractRole(type, attributes)
) {
// This rule has no opinion about abtract roles.
return;
}
// Visible, non-interactive elements should not have an interactive handler.
context.report({
node,
message: errorMessage,
});
},
};
},
}: ESLintConfig);