eslint-plugin-primer-react 6.2.0-rc.58af654 → 7.0.0-rc.0a66f81

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "6.2.0-rc.58af654",
3
+ "version": "7.0.0-rc.0a66f81",
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": "7.16.0",
37
+ "@typescript-eslint/utils": "8.18.1",
38
38
  "typescript": "^5.5.3"
39
39
  },
40
40
  "devDependencies": {
@@ -45,7 +45,7 @@
45
45
  "eslint": "^8.42.0",
46
46
  "eslint-plugin-prettier": "^5.2.1",
47
47
  "jest": "^29.7.0",
48
- "markdownlint-cli2": "^0.14.0",
48
+ "markdownlint-cli2": "^0.17.1",
49
49
  "markdownlint-cli2-formatter-pretty": "^0.0.7",
50
50
  "@typescript-eslint/rule-tester": "7.16.0",
51
51
  "@types/jest": "^29.5.13"
@@ -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-next-tooltip': 'error',
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-next-tooltip': require('./rules/a11y-use-next-tooltip'),
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
- }