-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathno-duplicate-ids.js
55 lines (48 loc) · 1.63 KB
/
no-duplicate-ids.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
/**
* @fileoverview Disallow duplicate ids.
* @author Chris Ng
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import { getProp, getPropValue } from 'jsx-ast-utils';
import { generateObjSchema } from '../util/schemas';
const schema = generateObjSchema();
export default {
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-duplicate-ids.md',
description: 'Disallow duplicate ids.',
},
schema: [schema],
},
create(context) {
const idsUsedSet = new Set();
const jsxExperissionIDsUsedSet = new Set();
return {
JSXOpeningElement(node) {
const { attributes } = node;
const idProp = getProp(attributes, 'id');
const idValue = getPropValue(idProp);
// Special case if id is assigned using JSXExpressionContainer
if (idProp && idProp.type === 'JSXAttribute' && idProp.value.type === 'JSXExpressionContainer') {
if (jsxExperissionIDsUsedSet.has(idValue)) {
context.report({
node,
message: `Duplicate ID "${idValue}" found. ID attribute JSX experssions must be unique.`,
});
} else {
jsxExperissionIDsUsedSet.add(idValue);
}
} else if (idsUsedSet.has(idValue)) {
context.report({
node,
message: `Duplicate ID "${idValue}" found. ID attribute values must be unique.`,
});
} else {
idsUsedSet.add(idValue);
}
},
};
},
};