-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
autocomplete-valid.js
63 lines (55 loc) · 2 KB
/
autocomplete-valid.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
/**
* @fileoverview Ensure autocomplete attribute is correct.
* @author Wilco Fiers
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { runVirtualRule } from 'axe-core';
import { getLiteralPropValue, getProp } from 'jsx-ast-utils';
import { generateObjSchema, arraySchema } from '../util/schemas';
import getElementType from '../util/getElementType';
const schema = generateObjSchema({
inputComponents: arraySchema,
});
export default {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/autocomplete-valid.md',
description: 'Enforce that autocomplete attributes are used correctly.',
},
schema: [schema],
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const options = context.options[0] || {};
const { inputComponents = [] } = options;
const inputTypes = ['input'].concat(inputComponents);
const elType = elementType(node);
const autocomplete = getLiteralPropValue(getProp(node.attributes, 'autocomplete'));
if (typeof autocomplete !== 'string' || !inputTypes.includes(elType)) {
return;
}
const type = getLiteralPropValue(getProp(node.attributes, 'type'));
const { violations } = runVirtualRule('autocomplete-valid', {
nodeName: 'input',
attributes: {
autocomplete,
// Which autocomplete is valid depends on the input type
type: type === null ? undefined : type,
},
});
if (violations.length === 0) {
return;
}
// Since we only test one rule, with one node, return the message from first (and only) instance of each
context.report({
node,
message: violations[0].nodes[0].all[0].message,
});
},
};
},
};