-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlang.js
69 lines (57 loc) · 1.63 KB
/
lang.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
/**
* @fileoverview Enforce lang attribute has a valid value.
* @author Ethan Cohen
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { propName, getLiteralPropValue } from 'jsx-ast-utils';
import tags from 'language-tags';
import { generateObjSchema } from '../util/schemas';
import getElementType from '../util/getElementType';
const errorMessage = 'lang attribute must have a valid value.';
const schema = generateObjSchema();
export default {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/lang.md',
description: 'Enforce lang attribute has a valid value.',
},
schema: [schema],
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXAttribute: (node) => {
const name = propName(node);
if (name && name.toUpperCase() !== 'LANG') {
return;
}
const { parent } = node;
const type = elementType(parent);
if (type && type !== 'html') {
return;
}
const value = getLiteralPropValue(node);
// Don't check identifiers
if (value === null) {
return;
}
if (value === undefined) {
context.report({
node,
message: errorMessage,
});
return;
}
if (tags.check(value)) {
return;
}
context.report({
node,
message: errorMessage,
});
},
};
},
};