-
Notifications
You must be signed in to change notification settings - Fork 24
/
has-valid-accessibility-descriptors.js
62 lines (55 loc) · 1.89 KB
/
has-valid-accessibility-descriptors.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
/**
* @fileoverview Ensures that Touchable* components have appropriate props to communicate with assistive technologies
* @author JP Driver
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import type { JSXOpeningElement } from 'ast-types-flow';
import { elementType, hasAnyProp } from 'jsx-ast-utils';
import type { ESLintContext } from '../../flow/eslint';
import isTouchable from '../util/isTouchable';
import { generateObjSchema } from '../util/schemas';
const errorMessage =
'Missing a11y props. Expected one of: accessibilityRole OR role OR BOTH accessibilityLabel + accessibilityHint OR BOTH accessibilityActions + onAccessibilityAction';
const schema = generateObjSchema();
const hasSpreadProps = (attributes) =>
attributes.some((attr) => attr.type === 'JSXSpreadAttribute');
module.exports = {
meta: {
docs: {},
schema: [schema],
fixable: 'code',
},
create: (context: ESLintContext) => ({
JSXOpeningElement: (node: JSXOpeningElement) => {
if (isTouchable(node, context) || elementType(node) === 'TextInput') {
if (
!hasAnyProp(node.attributes, [
'role',
'accessibilityRole',
'accessibilityLabel',
'accessibilityActions',
'accessible',
]) &&
!hasSpreadProps(node.attributes)
) {
context.report({
node,
message: errorMessage,
fix: (fixer) => {
return fixer.insertTextAfterRange(
// $FlowFixMe
node.name.range,
isTouchable(node, context)
? ' accessibilityRole="button"'
: ' accessibilityLabel="Text input field"'
);
},
});
}
}
},
}),
};