eslint-plugin-primer-react 6.2.0-rc.2c85052 → 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.
@@ -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
- }