-
-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]: Add no-duplicate-ids lint rule
Enforces that no `id` attributes are reused. This rule does a basic check to ensure that `id` attribute values are not the same. In the case of a JSX expression, it checks that no `id` attributes reuse the same expression.
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @fileoverview Disallow duplicate ids. | ||
* @author Chris Ng | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import { RuleTester } from 'eslint'; | ||
import parserOptionsMapper from '../../__util__/parserOptionsMapper'; | ||
import parsers from '../../__util__/helpers/parsers'; | ||
import rule from '../../../src/rules/no-duplicate-ids'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const expectedError = (idValue) => ({ | ||
message: `Duplicate ID "${idValue}" found. ID attribute values must be unique.`, | ||
type: 'JSXOpeningElement', | ||
}); | ||
|
||
const expectedJSXError = (idValue) => ({ | ||
message: `Duplicate ID "${idValue}" found. ID attribute JSX experssions must be unique.`, | ||
type: 'JSXOpeningElement', | ||
}); | ||
|
||
ruleTester.run('no-duplicate-ids', rule, { | ||
valid: parsers.all([].concat( | ||
{ code: '<div><div id="chris"></div><div id="chris2"></div></div>' }, | ||
{ code: '<div><div id={chris}></div><div id={chris2}></div></div>' }, | ||
{ code: '<div><MyComponent id="chris" /><MyComponent id="chris2" /></div>' }, | ||
{ code: '<div><div id="chris"></div><MyComponent id={chris} /></div>' }, | ||
)).map(parserOptionsMapper), | ||
invalid: parsers.all([].concat( | ||
{ code: '<div><div id="chris"></div><div id="chris"></div></div>', errors: [expectedError('chris')] }, | ||
{ code: '<div><div id={chris}></div><div id={chris}></div></div>', errors: [expectedJSXError('chris')] }, | ||
{ code: '<div><MyComponent id="chris" /><MyComponent id="chris" /></div>', errors: [expectedError('chris')] }, | ||
{ code: '<div><div id={chris}></div><MyComponent id={chris} /></div>', errors: [expectedJSXError('chris')] }, | ||
)).map(parserOptionsMapper), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# jsx-a11y/no-duplicate-ids | ||
|
||
💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Enforces that no `id` attributes are reused. This rule does a basic check to ensure that `id` attribute values are not the same. In the case of a JSX expression, it checks that no `id` attributes reuse the same expression. | ||
|
||
## Rule details | ||
|
||
This rule takes no arguments. | ||
|
||
### Succeed | ||
|
||
```jsx | ||
<div id="chris"></div><div id="chris2"></div> | ||
<div id={chris}></div><div id={chris2}></div> | ||
<MyComponent id="chris" /><MyComponent id="chris2" /> | ||
<div id="chris"></div><MyComponent id={chris} /> | ||
``` | ||
|
||
### Fail | ||
|
||
```jsx | ||
<div id="chris"></div><div id="chris"></div> | ||
<div id={chris}></div><div id={chris}></div> | ||
<MyComponent id="chris" /><MyComponent id="chris" /> | ||
<MyComponent id={chris} /><div id={chris}></div> | ||
``` | ||
|
||
## Accessibility guidelines | ||
- [WCAG 4.1.1](https://www.w3.org/WAI/WCAG21/Understanding/parsing.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |