eslint-plugin-primer-react 6.2.0-rc.58af654 → 7.0.0-rc.55939f7
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 +5 -1
- package/README.md +1 -1
- package/docs/rules/a11y-use-accessible-tooltip.md +47 -0
- package/package.json +1 -1
- package/src/configs/recommended.js +1 -1
- package/src/index.js +1 -1
- package/src/rules/__tests__/a11y-use-accessible-tooltip.test.js +59 -0
- package/src/rules/a11y-use-accessible-tooltip.js +148 -0
- package/docs/rules/a11y-use-next-tooltip.md +0 -33
- package/src/rules/__tests__/a11y-use-next-tooltip.test.js +0 -61
- package/src/rules/a11y-use-next-tooltip.js +0 -128
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# eslint-plugin-primer-react
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 7.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#270](https://github.com/primer/eslint-plugin-primer-react/pull/270) [`17814a2`](https://github.com/primer/eslint-plugin-primer-react/commit/17814a2d77d752b6675e51124fe3c18671837a10) Thanks [@broccolinisoup](https://github.com/broccolinisoup)! - Update a11y-use-next-tooltip to be a11y-use-accessible-tooltip and make the changes accordingly
|
|
4
8
|
|
|
5
9
|
### Minor Changes
|
|
6
10
|
|
package/README.md
CHANGED
|
@@ -39,4 +39,4 @@ ESLint rules for Primer React
|
|
|
39
39
|
- [a11y-explicit-heading](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-explicit-heading.md)
|
|
40
40
|
- [a11y-link-in-text-block](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-link-in-text-block.md)
|
|
41
41
|
- [a11y-remove-disable-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-remove-disable-tooltip.md)
|
|
42
|
-
- [a11y-use-
|
|
42
|
+
- [a11y-use-accessible-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-use-accessible-tooltip.md)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Recommends to use the new accessible tooltip instead of the deprecated one.
|
|
2
|
+
|
|
3
|
+
## Rule details
|
|
4
|
+
|
|
5
|
+
This rule suggests switching to the new accessible tooltip from @primer/react instead of the deprecated version. Deprecated props like wrap, noDelay, and align should also be removed.
|
|
6
|
+
|
|
7
|
+
Note that the new tooltip is intended for interactive elements only, such as buttons and links, whereas the deprecated tooltip could be applied to any element, though it lacks screen reader accessibility. As a result, the autofix for this rule will only work if the deprecated tooltip is on an interactive element. If it is applied to a non-interactive element, please consult your design team for [an alternative approach](https://primer.style/guides/accessibility/tooltip-alternatives).
|
|
8
|
+
|
|
9
|
+
👎 Examples of **incorrect** code for this rule:
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
import {Tooltip} from '@primer/react/deprecated'
|
|
13
|
+
|
|
14
|
+
const App = () => (
|
|
15
|
+
<Tooltip aria-label="This change cannot be undone" direction="w" wrap={true} noDelay={true} align="left">
|
|
16
|
+
<Button onClick={onClick}>Delete</Button>
|
|
17
|
+
</Tooltip>
|
|
18
|
+
)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
👍 Examples of **correct** code for this rule:
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
import {Tooltip} from '@primer/react'
|
|
25
|
+
|
|
26
|
+
const App = () => (
|
|
27
|
+
<Tooltip text="This change cannot be undone" direction="w">
|
|
28
|
+
<Button onClick={onClick}>Delete</Button>
|
|
29
|
+
</Tooltip>
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Icon buttons and tooltips
|
|
34
|
+
|
|
35
|
+
Even though the below code is perfectly valid, since icon buttons now come with tooltips by default, it is not required to explicitly use the Tooltip component on icon buttons.
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
import {IconButton, Tooltip} from '@primer/react'
|
|
39
|
+
|
|
40
|
+
function ExampleComponent() {
|
|
41
|
+
return (
|
|
42
|
+
<Tooltip text="Search" direction="e">
|
|
43
|
+
<IconButton icon={SearchIcon} aria-label="Search" />
|
|
44
|
+
</Tooltip>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
```
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = {
|
|
|
17
17
|
'primer-react/a11y-explicit-heading': 'error',
|
|
18
18
|
'primer-react/no-deprecated-props': 'warn',
|
|
19
19
|
'primer-react/a11y-remove-disable-tooltip': 'error',
|
|
20
|
-
'primer-react/a11y-use-
|
|
20
|
+
'primer-react/a11y-use-accessible-tooltip': 'error',
|
|
21
21
|
'primer-react/no-unnecessary-components': 'error',
|
|
22
22
|
'primer-react/prefer-action-list-item-onselect': 'error',
|
|
23
23
|
'primer-react/enforce-css-module-identifier-casing': 'error',
|
package/src/index.js
CHANGED
|
@@ -9,7 +9,7 @@ module.exports = {
|
|
|
9
9
|
'no-deprecated-props': require('./rules/no-deprecated-props'),
|
|
10
10
|
'a11y-link-in-text-block': require('./rules/a11y-link-in-text-block'),
|
|
11
11
|
'a11y-remove-disable-tooltip': require('./rules/a11y-remove-disable-tooltip'),
|
|
12
|
-
'a11y-use-
|
|
12
|
+
'a11y-use-accessible-tooltip': require('./rules/a11y-use-accessible-tooltip'),
|
|
13
13
|
'use-deprecated-from-deprecated': require('./rules/use-deprecated-from-deprecated'),
|
|
14
14
|
'no-wildcard-imports': require('./rules/no-wildcard-imports'),
|
|
15
15
|
'no-unnecessary-components': require('./rules/no-unnecessary-components'),
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const rule = require('../a11y-use-accessible-tooltip')
|
|
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-use-accessible-tooltip', rule, {
|
|
15
|
+
valid: [`import {Tooltip} from '@primer/react';`],
|
|
16
|
+
invalid: [
|
|
17
|
+
// Single import from deprecated
|
|
18
|
+
{
|
|
19
|
+
code: `import {Tooltip} from '@primer/react/deprecated';`,
|
|
20
|
+
errors: [{messageId: 'useAccessibleTooltip'}],
|
|
21
|
+
output: `import {Tooltip} from '@primer/react';`,
|
|
22
|
+
},
|
|
23
|
+
// Multiple imports from deprecated
|
|
24
|
+
{
|
|
25
|
+
code: `import {Tooltip, Button} from '@primer/react/deprecated';`,
|
|
26
|
+
errors: [{messageId: 'useAccessibleTooltip'}],
|
|
27
|
+
output: `import {Button} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`,
|
|
28
|
+
},
|
|
29
|
+
// Multiple imports from deprecated
|
|
30
|
+
{
|
|
31
|
+
code: `import {Button, Tooltip, Stack} from '@primer/react/deprecated';`,
|
|
32
|
+
errors: [{messageId: 'useAccessibleTooltip'}],
|
|
33
|
+
output: `import {Button, Stack} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';`,
|
|
34
|
+
},
|
|
35
|
+
// Single import from deprecated with an existing import from @primer/react
|
|
36
|
+
{
|
|
37
|
+
code: `import {Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`,
|
|
38
|
+
errors: [{messageId: 'useAccessibleTooltip', line: 1}],
|
|
39
|
+
output: `import {ActionList, ActionMenu, Tooltip} from '@primer/react';`,
|
|
40
|
+
},
|
|
41
|
+
// Multiple imports from deprecated with an existing import from @primer/react
|
|
42
|
+
{
|
|
43
|
+
code: `import {Dialog, Tooltip} from '@primer/react/deprecated';import {ActionList, ActionMenu} from '@primer/react';`,
|
|
44
|
+
errors: [{messageId: 'useAccessibleTooltip', line: 1}],
|
|
45
|
+
output: `import {Dialog} from '@primer/react/deprecated';import {ActionList, ActionMenu, Tooltip} from '@primer/react';`,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react/deprecated';\n<Tooltip aria-label="tooltip text" noDelay={true} wrap={true} align="left"><Button>Button</Button></Tooltip>`,
|
|
49
|
+
errors: [
|
|
50
|
+
{messageId: 'useAccessibleTooltip', line: 1},
|
|
51
|
+
{messageId: 'useTextProp', line: 2},
|
|
52
|
+
{messageId: 'noDelayRemoved', line: 2},
|
|
53
|
+
{messageId: 'wrapRemoved', line: 2},
|
|
54
|
+
{messageId: 'alignRemoved', line: 2},
|
|
55
|
+
],
|
|
56
|
+
output: `import {ActionList, ActionMenu, Button} from '@primer/react/deprecated';\nimport {Tooltip} from '@primer/react';\n<Tooltip text="tooltip text" ><Button>Button</Button></Tooltip>`,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
})
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const url = require('../url')
|
|
3
|
+
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
|
|
4
|
+
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: 'suggestion',
|
|
9
|
+
docs: {
|
|
10
|
+
description: 'recommends the use of @primer/react Tooltip component',
|
|
11
|
+
category: 'Best Practices',
|
|
12
|
+
recommended: true,
|
|
13
|
+
url: url(module),
|
|
14
|
+
},
|
|
15
|
+
fixable: true,
|
|
16
|
+
schema: [],
|
|
17
|
+
messages: {
|
|
18
|
+
useAccessibleTooltip: 'Please use @primer/react Tooltip component that has accessibility improvements',
|
|
19
|
+
useTextProp: 'Please use the text prop instead of aria-label',
|
|
20
|
+
noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default',
|
|
21
|
+
wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default',
|
|
22
|
+
alignRemoved: 'align prop is removed. Please use the direction prop instead.',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
create(context) {
|
|
26
|
+
return {
|
|
27
|
+
ImportDeclaration(node) {
|
|
28
|
+
if (node.source.value !== '@primer/react/deprecated') {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
const hasTooltip = node.specifiers.some(
|
|
32
|
+
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if (!hasTooltip) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const hasOtherImports = node.specifiers.length > 1
|
|
40
|
+
|
|
41
|
+
const sourceCode = context.getSourceCode()
|
|
42
|
+
// Checking to see if there is an existing root (@primer/react) import
|
|
43
|
+
// Assuming there is one root import per file
|
|
44
|
+
const rootImport = sourceCode.ast.body.find(statement => {
|
|
45
|
+
return statement.type === 'ImportDeclaration' && statement.source.value === '@primer/react'
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const tooltipSpecifier = node.specifiers.find(
|
|
49
|
+
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const hasRootImport = rootImport !== undefined
|
|
53
|
+
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
messageId: 'useAccessibleTooltip',
|
|
57
|
+
fix(fixer) {
|
|
58
|
+
const fixes = []
|
|
59
|
+
if (!hasOtherImports) {
|
|
60
|
+
// If Tooltip is the only import and no existing @primer/react import, replace the whole import statement
|
|
61
|
+
if (!hasRootImport) fixes.push(fixer.replaceText(node.source, `'@primer/react'`))
|
|
62
|
+
if (hasRootImport) {
|
|
63
|
+
// remove the entire import statement
|
|
64
|
+
fixes.push(fixer.remove(node))
|
|
65
|
+
// find the last specifier in the existing @primer/react import and insert Tooltip after that
|
|
66
|
+
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
|
|
67
|
+
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
// There are other imports from the deprecated bundle but no existing @primer/react import, so remove the Tooltip import and add a new import statement with the correct path.
|
|
71
|
+
const previousToken = sourceCode.getTokenBefore(tooltipSpecifier)
|
|
72
|
+
const nextToken = sourceCode.getTokenAfter(tooltipSpecifier)
|
|
73
|
+
const hasTrailingComma = nextToken && nextToken.value === ','
|
|
74
|
+
const hasLeadingComma = previousToken && previousToken.value === ','
|
|
75
|
+
|
|
76
|
+
let rangeToRemove
|
|
77
|
+
|
|
78
|
+
if (hasTrailingComma) {
|
|
79
|
+
rangeToRemove = [tooltipSpecifier.range[0], nextToken.range[1] + 1]
|
|
80
|
+
} else if (hasLeadingComma) {
|
|
81
|
+
rangeToRemove = [previousToken.range[0], tooltipSpecifier.range[1]]
|
|
82
|
+
} else {
|
|
83
|
+
rangeToRemove = [tooltipSpecifier.range[0], tooltipSpecifier.range[1]]
|
|
84
|
+
}
|
|
85
|
+
// Remove Tooltip from the import statement
|
|
86
|
+
fixes.push(fixer.removeRange(rangeToRemove))
|
|
87
|
+
|
|
88
|
+
if (!hasRootImport) {
|
|
89
|
+
fixes.push(fixer.insertTextAfter(node, `\nimport {Tooltip} from '@primer/react';`))
|
|
90
|
+
} else {
|
|
91
|
+
// find the last specifier in the existing @primer/react import and insert Tooltip after that
|
|
92
|
+
const lastSpecifier = rootImport.specifiers[rootImport.specifiers.length - 1]
|
|
93
|
+
fixes.push(fixer.insertTextAfter(lastSpecifier, `, Tooltip`))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return fixes
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
},
|
|
100
|
+
JSXOpeningElement(node) {
|
|
101
|
+
const openingElName = getJSXOpeningElementName(node)
|
|
102
|
+
if (openingElName !== 'Tooltip') {
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label')
|
|
106
|
+
if (ariaLabel !== undefined) {
|
|
107
|
+
context.report({
|
|
108
|
+
node,
|
|
109
|
+
messageId: 'useTextProp',
|
|
110
|
+
fix(fixer) {
|
|
111
|
+
return fixer.replaceText(ariaLabel.name, 'text')
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
const noDelay = getJSXOpeningElementAttribute(node, 'noDelay')
|
|
116
|
+
if (noDelay !== undefined) {
|
|
117
|
+
context.report({
|
|
118
|
+
node,
|
|
119
|
+
messageId: 'noDelayRemoved',
|
|
120
|
+
fix(fixer) {
|
|
121
|
+
return fixer.remove(noDelay)
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
const wrap = getJSXOpeningElementAttribute(node, 'wrap')
|
|
126
|
+
if (wrap !== undefined) {
|
|
127
|
+
context.report({
|
|
128
|
+
node,
|
|
129
|
+
messageId: 'wrapRemoved',
|
|
130
|
+
fix(fixer) {
|
|
131
|
+
return fixer.remove(wrap)
|
|
132
|
+
},
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
const align = getJSXOpeningElementAttribute(node, 'align')
|
|
136
|
+
if (align !== undefined) {
|
|
137
|
+
context.report({
|
|
138
|
+
node,
|
|
139
|
+
messageId: 'alignRemoved',
|
|
140
|
+
fix(fixer) {
|
|
141
|
+
return fixer.remove(align)
|
|
142
|
+
},
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# Recommends to use the `next` tooltip instead of the current stable one.
|
|
2
|
-
|
|
3
|
-
## Rule details
|
|
4
|
-
|
|
5
|
-
This rule recommends using the tooltip that is imported from `@primer/react/next` instead of the main entrypoint. The components that are exported from `@primer/react/next` are recommended over the main entrypoint ones because `next` components are reviewed and remediated for accessibility issues.
|
|
6
|
-
👎 Examples of **incorrect** code for this rule:
|
|
7
|
-
|
|
8
|
-
```tsx
|
|
9
|
-
import {Tooltip} from '@primer/react'
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
👍 Examples of **correct** code for this rule:
|
|
13
|
-
|
|
14
|
-
```tsx
|
|
15
|
-
import {Tooltip} from '@primer/react/next'
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Icon buttons and tooltips
|
|
19
|
-
|
|
20
|
-
Even though the below code is perfectly valid, since icon buttons now come with tooltips by default, it is not required to explicitly use the Tooltip component on icon buttons.
|
|
21
|
-
|
|
22
|
-
```jsx
|
|
23
|
-
import {IconButton} from '@primer/react'
|
|
24
|
-
import {Tooltip} from '@primer/react/next'
|
|
25
|
-
|
|
26
|
-
function ExampleComponent() {
|
|
27
|
-
return (
|
|
28
|
-
<Tooltip text="Search" direction="e">
|
|
29
|
-
<IconButton icon={SearchIcon} aria-label="Search" />
|
|
30
|
-
</Tooltip>
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
```
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
const rule = require('../a11y-use-next-tooltip')
|
|
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-use-next-tooltip', rule, {
|
|
15
|
-
valid: [
|
|
16
|
-
`import {Tooltip} from '@primer/react/next';`,
|
|
17
|
-
`import {UnderlineNav, Button} from '@primer/react';
|
|
18
|
-
import {Tooltip} from '@primer/react/next';`,
|
|
19
|
-
`import {UnderlineNav, Button} from '@primer/react';
|
|
20
|
-
import {Tooltip, SelectPanel} from '@primer/react/next';`,
|
|
21
|
-
],
|
|
22
|
-
invalid: [
|
|
23
|
-
{
|
|
24
|
-
code: `import {Tooltip} from '@primer/react';`,
|
|
25
|
-
errors: [{messageId: 'useNextTooltip'}],
|
|
26
|
-
output: `import {Tooltip} from '@primer/react/next';`,
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
code: `import {Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button>Button</Button></Tooltip>`,
|
|
30
|
-
errors: [{messageId: 'useNextTooltip'}, {messageId: 'useTextProp'}],
|
|
31
|
-
output: `import {Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';\n<Tooltip text="tooltip text"><Button>Button</Button></Tooltip>`,
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button>Button</Button></Tooltip>`,
|
|
35
|
-
errors: [
|
|
36
|
-
{messageId: 'useNextTooltip', line: 1},
|
|
37
|
-
{messageId: 'useTextProp', line: 2},
|
|
38
|
-
],
|
|
39
|
-
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';\n<Tooltip text="tooltip text"><Button>Button</Button></Tooltip>`,
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
code: `import {ActionList, ActionMenu, Button, Tooltip} from '@primer/react';\n<Tooltip aria-label="tooltip text"><Button aria-label="test">Button</Button></Tooltip>`,
|
|
43
|
-
errors: [
|
|
44
|
-
{messageId: 'useNextTooltip', line: 1},
|
|
45
|
-
{messageId: 'useTextProp', line: 2},
|
|
46
|
-
],
|
|
47
|
-
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';\n<Tooltip text="tooltip text"><Button aria-label="test">Button</Button></Tooltip>`,
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
code: `import {ActionList, ActionMenu, Tooltip, Button} from '@primer/react';\n<Tooltip aria-label="tooltip text" noDelay={true} wrap={true} align="left"><Button>Button</Button></Tooltip>`,
|
|
51
|
-
errors: [
|
|
52
|
-
{messageId: 'useNextTooltip', line: 1},
|
|
53
|
-
{messageId: 'useTextProp', line: 2},
|
|
54
|
-
{messageId: 'noDelayRemoved', line: 2},
|
|
55
|
-
{messageId: 'wrapRemoved', line: 2},
|
|
56
|
-
{messageId: 'alignRemoved', line: 2},
|
|
57
|
-
],
|
|
58
|
-
output: `import {ActionList, ActionMenu, Button} from '@primer/react';\nimport {Tooltip} from '@primer/react/next';\n<Tooltip text="tooltip text" ><Button>Button</Button></Tooltip>`,
|
|
59
|
-
},
|
|
60
|
-
],
|
|
61
|
-
})
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
const url = require('../url')
|
|
3
|
-
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
|
|
4
|
-
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
|
|
5
|
-
|
|
6
|
-
module.exports = {
|
|
7
|
-
meta: {
|
|
8
|
-
type: 'suggestion',
|
|
9
|
-
docs: {
|
|
10
|
-
description: 'recommends the use of @primer/react/next Tooltip component',
|
|
11
|
-
category: 'Best Practices',
|
|
12
|
-
recommended: true,
|
|
13
|
-
url: url(module),
|
|
14
|
-
},
|
|
15
|
-
fixable: true,
|
|
16
|
-
schema: [],
|
|
17
|
-
messages: {
|
|
18
|
-
useNextTooltip: 'Please use @primer/react/next Tooltip component that has accessibility improvements',
|
|
19
|
-
useTextProp: 'Please use the text prop instead of aria-label',
|
|
20
|
-
noDelayRemoved: 'noDelay prop is removed. Tooltip now has no delay by default',
|
|
21
|
-
wrapRemoved: 'wrap prop is removed. Tooltip now wraps by default',
|
|
22
|
-
alignRemoved: 'align prop is removed. Please use the direction prop instead.',
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
create(context) {
|
|
26
|
-
return {
|
|
27
|
-
ImportDeclaration(node) {
|
|
28
|
-
if (node.source.value !== '@primer/react') {
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
const hasTooltip = node.specifiers.some(
|
|
32
|
-
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
const hasOtherImports = node.specifiers.length > 1
|
|
36
|
-
if (!hasTooltip) {
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
-
context.report({
|
|
40
|
-
node,
|
|
41
|
-
messageId: 'useNextTooltip',
|
|
42
|
-
fix(fixer) {
|
|
43
|
-
// If Tooltip is the only import, replace the whole import statement
|
|
44
|
-
if (!hasOtherImports) {
|
|
45
|
-
return fixer.replaceText(node.source, `'@primer/react/next'`)
|
|
46
|
-
} else {
|
|
47
|
-
// Otherwise, remove Tooltip from the import statement and add a new import statement with the correct path
|
|
48
|
-
const tooltipSpecifier = node.specifiers.find(
|
|
49
|
-
specifier => specifier.imported && specifier.imported.name === 'Tooltip',
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
const tokensToRemove = [' ', ',']
|
|
53
|
-
const tooltipIsFirstImport = tooltipSpecifier === node.specifiers[0]
|
|
54
|
-
const tooltipIsLastImport = tooltipSpecifier === node.specifiers[node.specifiers.length - 1]
|
|
55
|
-
const tooltipIsNotFirstOrLastImport = !tooltipIsFirstImport && !tooltipIsLastImport
|
|
56
|
-
|
|
57
|
-
const sourceCode = context.getSourceCode()
|
|
58
|
-
const canRemoveBefore = tooltipIsNotFirstOrLastImport
|
|
59
|
-
? false
|
|
60
|
-
: tokensToRemove.includes(sourceCode.getTokenBefore(tooltipSpecifier).value)
|
|
61
|
-
const canRemoveAfter = tokensToRemove.includes(sourceCode.getTokenAfter(tooltipSpecifier).value)
|
|
62
|
-
const start = canRemoveBefore
|
|
63
|
-
? sourceCode.getTokenBefore(tooltipSpecifier).range[0]
|
|
64
|
-
: tooltipSpecifier.range[0]
|
|
65
|
-
const end = canRemoveAfter
|
|
66
|
-
? sourceCode.getTokenAfter(tooltipSpecifier).range[1] + 1
|
|
67
|
-
: tooltipSpecifier.range[1]
|
|
68
|
-
return [
|
|
69
|
-
// remove tooltip specifier and the space and comma after it
|
|
70
|
-
fixer.removeRange([start, end]),
|
|
71
|
-
fixer.insertTextAfterRange(
|
|
72
|
-
[node.range[1], node.range[1]],
|
|
73
|
-
`\nimport {Tooltip} from '@primer/react/next';`,
|
|
74
|
-
),
|
|
75
|
-
]
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
})
|
|
79
|
-
},
|
|
80
|
-
JSXOpeningElement(node) {
|
|
81
|
-
const openingElName = getJSXOpeningElementName(node)
|
|
82
|
-
if (openingElName !== 'Tooltip') {
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
const ariaLabel = getJSXOpeningElementAttribute(node, 'aria-label')
|
|
86
|
-
if (ariaLabel !== undefined) {
|
|
87
|
-
context.report({
|
|
88
|
-
node,
|
|
89
|
-
messageId: 'useTextProp',
|
|
90
|
-
fix(fixer) {
|
|
91
|
-
return fixer.replaceText(ariaLabel.name, 'text')
|
|
92
|
-
},
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
const noDelay = getJSXOpeningElementAttribute(node, 'noDelay')
|
|
96
|
-
if (noDelay !== undefined) {
|
|
97
|
-
context.report({
|
|
98
|
-
node,
|
|
99
|
-
messageId: 'noDelayRemoved',
|
|
100
|
-
fix(fixer) {
|
|
101
|
-
return fixer.remove(noDelay)
|
|
102
|
-
},
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
const wrap = getJSXOpeningElementAttribute(node, 'wrap')
|
|
106
|
-
if (wrap !== undefined) {
|
|
107
|
-
context.report({
|
|
108
|
-
node,
|
|
109
|
-
messageId: 'wrapRemoved',
|
|
110
|
-
fix(fixer) {
|
|
111
|
-
return fixer.remove(wrap)
|
|
112
|
-
},
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
const align = getJSXOpeningElementAttribute(node, 'align')
|
|
116
|
-
if (align !== undefined) {
|
|
117
|
-
context.report({
|
|
118
|
-
node,
|
|
119
|
-
messageId: 'alignRemoved',
|
|
120
|
-
fix(fixer) {
|
|
121
|
-
return fixer.remove(align)
|
|
122
|
-
},
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
},
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
}
|