eslint-plugin-primer-react 7.0.2 → 7.1.0-rc.57f3453
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/docs/rules/a11y-no-duplicate-form-labels.md +48 -0
- package/package-lock.json +14751 -0
- package/package.json +2 -2
- package/src/configs/recommended.js +1 -0
- package/src/index.js +1 -0
- package/src/rules/__tests__/a11y-no-duplicate-form-labels.test.js +112 -0
- package/src/rules/__tests__/enforce-button-for-link-with-nohref.test.js +86 -0
- package/src/rules/a11y-no-duplicate-form-labels.js +69 -0
- package/src/rules/enforce-button-for-link-with-nohref.js +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-primer-react",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.1.0-rc.57f3453",
|
|
4
4
|
"description": "ESLint rules for Primer React",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"eslint-traverse": "^1.0.0",
|
|
35
35
|
"lodash": "^4.17.21",
|
|
36
36
|
"styled-system": "^5.1.5",
|
|
37
|
-
"@typescript-eslint/utils": "8.
|
|
37
|
+
"@typescript-eslint/utils": "8.38.0",
|
|
38
38
|
"typescript": "^5.8.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
@@ -17,6 +17,7 @@ module.exports = {
|
|
|
17
17
|
'primer-react/new-color-css-vars': 'error',
|
|
18
18
|
'primer-react/a11y-explicit-heading': 'error',
|
|
19
19
|
'primer-react/a11y-no-title-usage': 'error',
|
|
20
|
+
'primer-react/a11y-no-duplicate-form-labels': 'error',
|
|
20
21
|
'primer-react/no-deprecated-props': 'warn',
|
|
21
22
|
'primer-react/a11y-remove-disable-tooltip': 'error',
|
|
22
23
|
'primer-react/a11y-use-accessible-tooltip': 'error',
|
package/src/index.js
CHANGED
|
@@ -12,6 +12,7 @@ module.exports = {
|
|
|
12
12
|
'a11y-remove-disable-tooltip': require('./rules/a11y-remove-disable-tooltip'),
|
|
13
13
|
'a11y-use-accessible-tooltip': require('./rules/a11y-use-accessible-tooltip'),
|
|
14
14
|
'a11y-no-title-usage': require('./rules/a11y-no-title-usage'),
|
|
15
|
+
'a11y-no-duplicate-form-labels': require('./rules/a11y-no-duplicate-form-labels'),
|
|
15
16
|
'use-deprecated-from-deprecated': require('./rules/use-deprecated-from-deprecated'),
|
|
16
17
|
'no-wildcard-imports': require('./rules/no-wildcard-imports'),
|
|
17
18
|
'no-unnecessary-components': require('./rules/no-unnecessary-components'),
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const rule = require('../a11y-no-duplicate-form-labels')
|
|
2
|
+
const {RuleTester} = require('eslint')
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 'latest',
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
ecmaFeatures: {
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
ruleTester.run('a11y-no-duplicate-form-labels', rule, {
|
|
15
|
+
valid: [
|
|
16
|
+
// TextInput without aria-label is valid
|
|
17
|
+
`import {FormControl, TextInput} from '@primer/react';
|
|
18
|
+
<FormControl>
|
|
19
|
+
<FormControl.Label>Form Input Label</FormControl.Label>
|
|
20
|
+
<TextInput />
|
|
21
|
+
</FormControl>`,
|
|
22
|
+
|
|
23
|
+
// TextInput with aria-label but no FormControl.Label is valid
|
|
24
|
+
`import {FormControl, TextInput} from '@primer/react';
|
|
25
|
+
<FormControl>
|
|
26
|
+
<TextInput aria-label="Form Input Label" />
|
|
27
|
+
</FormControl>`,
|
|
28
|
+
|
|
29
|
+
// TextInput with aria-label outside FormControl is valid
|
|
30
|
+
`import {TextInput} from '@primer/react';
|
|
31
|
+
<TextInput aria-label="Form Input Label" />`,
|
|
32
|
+
|
|
33
|
+
// TextInput with visuallyHidden FormControl.Label is valid
|
|
34
|
+
`import {FormControl, TextInput} from '@primer/react';
|
|
35
|
+
<FormControl>
|
|
36
|
+
<FormControl.Label visuallyHidden>Form Input Label</FormControl.Label>
|
|
37
|
+
<TextInput />
|
|
38
|
+
</FormControl>`,
|
|
39
|
+
|
|
40
|
+
// FormControl without FormControl.Label but with aria-label is valid
|
|
41
|
+
`import {FormControl, TextInput} from '@primer/react';
|
|
42
|
+
<FormControl>
|
|
43
|
+
<TextInput aria-label="Form Input Label" />
|
|
44
|
+
</FormControl>`,
|
|
45
|
+
|
|
46
|
+
// Multiple TextInputs with different approaches
|
|
47
|
+
`import {FormControl, TextInput} from '@primer/react';
|
|
48
|
+
<div>
|
|
49
|
+
<FormControl>
|
|
50
|
+
<FormControl.Label>Visible Label</FormControl.Label>
|
|
51
|
+
<TextInput />
|
|
52
|
+
</FormControl>
|
|
53
|
+
<FormControl>
|
|
54
|
+
<TextInput aria-label="Standalone Input" />
|
|
55
|
+
</FormControl>
|
|
56
|
+
</div>`,
|
|
57
|
+
],
|
|
58
|
+
invalid: [
|
|
59
|
+
{
|
|
60
|
+
code: `import {FormControl, TextInput} from '@primer/react';
|
|
61
|
+
<FormControl>
|
|
62
|
+
<FormControl.Label>Form Input Label</FormControl.Label>
|
|
63
|
+
<TextInput aria-label="Form Input Label" />
|
|
64
|
+
</FormControl>`,
|
|
65
|
+
errors: [
|
|
66
|
+
{
|
|
67
|
+
messageId: 'duplicateLabel',
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
code: `import {FormControl, TextInput} from '@primer/react';
|
|
73
|
+
<FormControl>
|
|
74
|
+
<FormControl.Label>Username</FormControl.Label>
|
|
75
|
+
<TextInput aria-label="Enter your username" />
|
|
76
|
+
</FormControl>`,
|
|
77
|
+
errors: [
|
|
78
|
+
{
|
|
79
|
+
messageId: 'duplicateLabel',
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
code: `import {FormControl, TextInput} from '@primer/react';
|
|
85
|
+
<FormControl>
|
|
86
|
+
<FormControl.Label visuallyHidden>Password</FormControl.Label>
|
|
87
|
+
<TextInput aria-label="Enter password" />
|
|
88
|
+
</FormControl>`,
|
|
89
|
+
errors: [
|
|
90
|
+
{
|
|
91
|
+
messageId: 'duplicateLabel',
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
code: `import {FormControl, TextInput} from '@primer/react';
|
|
97
|
+
<div>
|
|
98
|
+
<FormControl>
|
|
99
|
+
<FormControl.Label>Email</FormControl.Label>
|
|
100
|
+
<div>
|
|
101
|
+
<TextInput aria-label="Email address" />
|
|
102
|
+
</div>
|
|
103
|
+
</FormControl>
|
|
104
|
+
</div>`,
|
|
105
|
+
errors: [
|
|
106
|
+
{
|
|
107
|
+
messageId: 'duplicateLabel',
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const rule = require('../enforce-button-for-link-with-nohref')
|
|
2
|
+
const {RuleTester} = require('eslint')
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 'latest',
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
ecmaFeatures: {
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
ruleTester.run('enforce-button-for-link-with-nohref', rule, {
|
|
15
|
+
valid: [
|
|
16
|
+
// Link with href attribute
|
|
17
|
+
`import {Link} from '@primer/react';
|
|
18
|
+
<Link href="https://example.com">Valid Link</Link>`,
|
|
19
|
+
|
|
20
|
+
// Link with href and inline prop
|
|
21
|
+
`import {Link} from '@primer/react';
|
|
22
|
+
<Link inline href="https://example.com">Valid Inline Link</Link>`,
|
|
23
|
+
|
|
24
|
+
// Link with href and className
|
|
25
|
+
`import {Link} from '@primer/react';
|
|
26
|
+
<Link className="some-class" href="https://example.com">Valid Link with Class</Link>`,
|
|
27
|
+
|
|
28
|
+
// Link with href, inline, and className
|
|
29
|
+
`import {Link} from '@primer/react';
|
|
30
|
+
<Link className="some-class" inline href="https://example.com">Valid Inline Link with Class</Link>`,
|
|
31
|
+
|
|
32
|
+
// Link with href as variable
|
|
33
|
+
`import {Link} from '@primer/react';
|
|
34
|
+
const url = '/about';
|
|
35
|
+
<Link href={url}>About</Link>`,
|
|
36
|
+
|
|
37
|
+
// Button component (not Link)
|
|
38
|
+
`import {Button} from '@primer/react';
|
|
39
|
+
<Button onClick={handleClick}>Click me</Button>`,
|
|
40
|
+
|
|
41
|
+
// Regular HTML link (not Primer Link)
|
|
42
|
+
`<a onClick={handleClick}>Click me</a>`,
|
|
43
|
+
|
|
44
|
+
// Link from different package
|
|
45
|
+
`import {Link} from 'react-router-dom';
|
|
46
|
+
<Link to="/about">About</Link>`,
|
|
47
|
+
],
|
|
48
|
+
invalid: [
|
|
49
|
+
{
|
|
50
|
+
code: `import {Link} from '@primer/react';
|
|
51
|
+
<Link>Invalid Link without href</Link>`,
|
|
52
|
+
errors: [
|
|
53
|
+
{
|
|
54
|
+
messageId: 'noLinkWithoutHref',
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
code: `import {Link} from '@primer/react';
|
|
60
|
+
<Link className="some-class">Invalid Link with class but no href</Link>`,
|
|
61
|
+
errors: [
|
|
62
|
+
{
|
|
63
|
+
messageId: 'noLinkWithoutHref',
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
code: `import {Link} from '@primer/react';
|
|
69
|
+
<Link inline>Invalid inline Link without href</Link>`,
|
|
70
|
+
errors: [
|
|
71
|
+
{
|
|
72
|
+
messageId: 'noLinkWithoutHref',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
code: `import {Link} from '@primer/react';
|
|
78
|
+
<Link onClick={handleClick}>Invalid Link with onClick but no href</Link>`,
|
|
79
|
+
errors: [
|
|
80
|
+
{
|
|
81
|
+
messageId: 'noLinkWithoutHref',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const {isPrimerComponent} = require('../utils/is-primer-component')
|
|
2
|
+
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
|
|
3
|
+
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
|
|
4
|
+
|
|
5
|
+
const isFormControl = node => getJSXOpeningElementName(node) === 'FormControl'
|
|
6
|
+
const isFormControlLabel = node => getJSXOpeningElementName(node) === 'FormControl.Label'
|
|
7
|
+
const isTextInput = node => getJSXOpeningElementName(node) === 'TextInput'
|
|
8
|
+
|
|
9
|
+
const hasAriaLabel = node => {
|
|
10
|
+
const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label')
|
|
11
|
+
return !!ariaLabel
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const findFormControlLabel = (node, sourceCode) => {
|
|
15
|
+
// Traverse up the parent chain to find FormControl
|
|
16
|
+
let current = node.parent
|
|
17
|
+
while (current) {
|
|
18
|
+
if (
|
|
19
|
+
current.type === 'JSXElement' &&
|
|
20
|
+
isFormControl(current.openingElement) &&
|
|
21
|
+
isPrimerComponent(current.openingElement.name, sourceCode.getScope(current))
|
|
22
|
+
) {
|
|
23
|
+
// Found FormControl, now check if it has a FormControl.Label child
|
|
24
|
+
return current.children.some(
|
|
25
|
+
child =>
|
|
26
|
+
child.type === 'JSXElement' &&
|
|
27
|
+
isFormControlLabel(child.openingElement) &&
|
|
28
|
+
isPrimerComponent(child.openingElement.name, sourceCode.getScope(child)),
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
current = current.parent
|
|
32
|
+
}
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = {
|
|
37
|
+
meta: {
|
|
38
|
+
type: 'problem',
|
|
39
|
+
docs: {
|
|
40
|
+
description:
|
|
41
|
+
'Prevent duplicate labels on form inputs by disallowing aria-label on TextInput when FormControl.Label is present.',
|
|
42
|
+
url: require('../url')(module),
|
|
43
|
+
},
|
|
44
|
+
schema: [],
|
|
45
|
+
messages: {
|
|
46
|
+
duplicateLabel:
|
|
47
|
+
'TextInput should not have aria-label when FormControl.Label is present. Use FormControl.Label with visuallyHidden prop if needed.',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
create(context) {
|
|
51
|
+
const sourceCode = context.sourceCode ?? context.getSourceCode()
|
|
52
|
+
return {
|
|
53
|
+
JSXOpeningElement(jsxNode) {
|
|
54
|
+
if (isPrimerComponent(jsxNode.name, sourceCode.getScope(jsxNode)) && isTextInput(jsxNode)) {
|
|
55
|
+
// Check if TextInput has aria-label
|
|
56
|
+
if (hasAriaLabel(jsxNode)) {
|
|
57
|
+
// Check if there's a FormControl.Label in the parent FormControl
|
|
58
|
+
if (findFormControlLabel(jsxNode, sourceCode)) {
|
|
59
|
+
context.report({
|
|
60
|
+
node: jsxNode,
|
|
61
|
+
messageId: 'duplicateLabel',
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const url = require('../url')
|
|
2
|
+
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
|
|
3
|
+
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
|
|
4
|
+
const {isPrimerComponent} = require('../utils/is-primer-component')
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: 'error',
|
|
9
|
+
docs: {
|
|
10
|
+
description: 'Disallow usage of Link component without href',
|
|
11
|
+
recommended: true,
|
|
12
|
+
url: url(module),
|
|
13
|
+
},
|
|
14
|
+
messages: {
|
|
15
|
+
noLinkWithoutHref: 'Links without href and other side effects are not accessible. Use a Button instead.',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
create(context) {
|
|
20
|
+
const sourceCode = context.sourceCode ?? context.getSourceCode()
|
|
21
|
+
return {
|
|
22
|
+
JSXElement(node) {
|
|
23
|
+
const openingElement = node.openingElement
|
|
24
|
+
const elementName = getJSXOpeningElementName(openingElement)
|
|
25
|
+
|
|
26
|
+
// Check if this is a Link component from @primer/react
|
|
27
|
+
if (elementName === 'Link' && isPrimerComponent(openingElement.name, sourceCode.getScope(node))) {
|
|
28
|
+
// Check if the Link has an href attribute
|
|
29
|
+
const hrefAttribute = getJSXOpeningElementAttribute(openingElement, 'href')
|
|
30
|
+
|
|
31
|
+
if (!hrefAttribute) {
|
|
32
|
+
context.report({
|
|
33
|
+
node: openingElement,
|
|
34
|
+
messageId: 'noLinkWithoutHref',
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
}
|