eslint-plugin-primer-react 6.2.0-rc.2c85052 → 7.0.0-rc.414f409
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 +7 -1
- package/README.md +1 -1
- package/docs/rules/a11y-use-accessible-tooltip.md +47 -0
- package/docs/rules/enforce-css-module-default-import.md +53 -0
- package/package-lock.json +39 -536
- package/package.json +1 -1
- package/src/configs/recommended.js +2 -1
- package/src/index.js +2 -1
- package/src/rules/__tests__/a11y-use-accessible-tooltip.test.js +59 -0
- package/src/rules/__tests__/enforce-css-module-default-import.js +88 -0
- package/src/rules/__tests__/enforce-css-module-identifier-casing.test.js +10 -0
- package/src/rules/a11y-use-accessible-tooltip.js +148 -0
- package/src/rules/enforce-css-module-default-import.js +62 -0
- package/src/rules/enforce-css-module-identifier-casing.js +12 -2
- package/src/utils/css-modules.js +2 -2
- 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/package.json
CHANGED
|
@@ -17,10 +17,11 @@ 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',
|
|
24
|
+
'primer-react/enforce-css-module-default-import': ['error', {enforceName: '(^classes$|Classes$)'}],
|
|
24
25
|
},
|
|
25
26
|
settings: {
|
|
26
27
|
github: {
|
package/src/index.js
CHANGED
|
@@ -9,12 +9,13 @@ 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'),
|
|
16
16
|
'prefer-action-list-item-onselect': require('./rules/prefer-action-list-item-onselect'),
|
|
17
17
|
'enforce-css-module-identifier-casing': require('./rules/enforce-css-module-identifier-casing'),
|
|
18
|
+
'enforce-css-module-default-import': require('./rules/enforce-css-module-default-import'),
|
|
18
19
|
},
|
|
19
20
|
configs: {
|
|
20
21
|
recommended: require('./configs/recommended'),
|
|
@@ -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,88 @@
|
|
|
1
|
+
const rule = require('../enforce-css-module-default-import')
|
|
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-css-module-default-import', rule, {
|
|
15
|
+
valid: [
|
|
16
|
+
'import classes from "a.module.css";',
|
|
17
|
+
'import {default as classes} from "a.module.css";',
|
|
18
|
+
'import staticClasses from "a.module.css";',
|
|
19
|
+
'import fooClasses from "a.module.css";',
|
|
20
|
+
'import whatever from "a.js";',
|
|
21
|
+
'import whatever from "a.module.js";',
|
|
22
|
+
'import whatever from "a.css";',
|
|
23
|
+
'import {whatever} from "a.js";',
|
|
24
|
+
'import {whatever} from "a.css";',
|
|
25
|
+
{
|
|
26
|
+
code: 'import classes from "a.module.css";',
|
|
27
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
code: 'import sharedClasses from "a.module.css";',
|
|
31
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
invalid: [
|
|
35
|
+
{
|
|
36
|
+
code: 'import {foo} from "a.module.css";',
|
|
37
|
+
errors: [
|
|
38
|
+
{
|
|
39
|
+
messageId: 'notDefault',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
code: 'import _, {foo} from "a.module.css";',
|
|
45
|
+
errors: [
|
|
46
|
+
{
|
|
47
|
+
messageId: 'notDefault',
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
code: 'import foobar from "a.module.css";',
|
|
53
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
54
|
+
errors: [
|
|
55
|
+
{
|
|
56
|
+
messageId: 'badName',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
code: 'import someclasses from "a.module.css";',
|
|
62
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
63
|
+
errors: [
|
|
64
|
+
{
|
|
65
|
+
messageId: 'badName',
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
code: 'import {default as foobar} from "a.module.css";',
|
|
71
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
72
|
+
errors: [
|
|
73
|
+
{
|
|
74
|
+
messageId: 'badName',
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
code: 'import {default as someclasses} from "a.module.css";',
|
|
80
|
+
options: [{enforceName: '(?:^classes$|Classes$)'}],
|
|
81
|
+
errors: [
|
|
82
|
+
{
|
|
83
|
+
messageId: 'badName',
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
})
|
|
@@ -19,6 +19,7 @@ ruleTester.run('enforce-css-module-identifier-casing', rule, {
|
|
|
19
19
|
'import classes from "a.module.css"; function Foo() { return <Box className={`${classes.Foo}`}/> }',
|
|
20
20
|
'import classes from "a.module.css"; function Foo() { return <Box className={`${classes["Foo"]}`}/> }',
|
|
21
21
|
'import classes from "a.module.css"; let x = "Foo"; function Foo() { return <Box className={`${classes[x]}`}/> }',
|
|
22
|
+
'import {Foo} from "a.module.css"; function Bar() { return <Box className={Foo}/> }',
|
|
22
23
|
],
|
|
23
24
|
invalid: [
|
|
24
25
|
{
|
|
@@ -30,6 +31,15 @@ ruleTester.run('enforce-css-module-identifier-casing', rule, {
|
|
|
30
31
|
},
|
|
31
32
|
],
|
|
32
33
|
},
|
|
34
|
+
{
|
|
35
|
+
code: 'import {foo} from "a.module.css"; function Bar() { return <Box className={foo}/> }',
|
|
36
|
+
errors: [
|
|
37
|
+
{
|
|
38
|
+
messageId: 'pascal',
|
|
39
|
+
data: {name: 'foo'},
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
},
|
|
33
43
|
{
|
|
34
44
|
code: 'import classes from "a.module.css"; function Foo() { return <Box className={clsx(classes.foo)}/> }',
|
|
35
45
|
errors: [
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'suggestion',
|
|
4
|
+
fixable: 'code',
|
|
5
|
+
schema: [
|
|
6
|
+
{
|
|
7
|
+
properties: {
|
|
8
|
+
enforceName: {
|
|
9
|
+
type: 'string',
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
],
|
|
14
|
+
messages: {
|
|
15
|
+
badName: 'This default import should match {{enforceName}}',
|
|
16
|
+
notDefault: 'Class modules should only import the default object.',
|
|
17
|
+
noDefault: 'Class modules should always import default.',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
const enforceName = new RegExp(context.options[0]?.enforceName || '.*')
|
|
22
|
+
return {
|
|
23
|
+
['ImportDeclaration>Literal[value=/.module.css$/]']: function (node) {
|
|
24
|
+
node = node.parent
|
|
25
|
+
const defaultSpecifier = node.specifiers.find(spec => spec.type === 'ImportDefaultSpecifier')
|
|
26
|
+
const otherSpecifiers = node.specifiers.filter(spec => spec.type !== 'ImportDefaultSpecifier')
|
|
27
|
+
const asDefaultSpecifier = otherSpecifiers.find(spec => spec.imported?.name === 'default')
|
|
28
|
+
if (!node.specifiers.length) {
|
|
29
|
+
context.report({
|
|
30
|
+
node,
|
|
31
|
+
messageId: 'noDefault',
|
|
32
|
+
})
|
|
33
|
+
} else if (otherSpecifiers.length === 1 && asDefaultSpecifier) {
|
|
34
|
+
if (!enforceName.test(asDefaultSpecifier.local.name)) {
|
|
35
|
+
context.report({
|
|
36
|
+
node,
|
|
37
|
+
messageId: 'badName',
|
|
38
|
+
data: {enforceName},
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
} else if (otherSpecifiers.length) {
|
|
42
|
+
context.report({
|
|
43
|
+
node,
|
|
44
|
+
messageId: 'notDefault',
|
|
45
|
+
})
|
|
46
|
+
} else if (!defaultSpecifier) {
|
|
47
|
+
context.report({
|
|
48
|
+
node,
|
|
49
|
+
messageId: 'noDefault',
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
if (defaultSpecifier && !enforceName.test(defaultSpecifier.local.name)) {
|
|
53
|
+
context.report({
|
|
54
|
+
node,
|
|
55
|
+
messageId: 'badName',
|
|
56
|
+
data: {enforceName},
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
}
|
|
@@ -24,6 +24,16 @@ module.exports = {
|
|
|
24
24
|
create(context) {
|
|
25
25
|
const casing = context.options[0]?.casing || 'pascal'
|
|
26
26
|
return {
|
|
27
|
+
['JSXAttribute[name.name="className"] JSXExpressionContainer>Identifier']: function (node) {
|
|
28
|
+
if (!identifierIsCSSModuleBinding(node, context)) return
|
|
29
|
+
if (!casingMatches(node.name || '', casing)) {
|
|
30
|
+
context.report({
|
|
31
|
+
node,
|
|
32
|
+
messageId: casing,
|
|
33
|
+
data: {name: node.name},
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
},
|
|
27
37
|
['JSXAttribute[name.name="className"] JSXExpressionContainer MemberExpression[object.type="Identifier"]']:
|
|
28
38
|
function (node) {
|
|
29
39
|
if (!identifierIsCSSModuleBinding(node.object, context)) return
|
|
@@ -44,8 +54,8 @@ module.exports = {
|
|
|
44
54
|
})
|
|
45
55
|
}
|
|
46
56
|
} else if (node.computed) {
|
|
47
|
-
const ref = context
|
|
48
|
-
.getScope()
|
|
57
|
+
const ref = context.sourceCode
|
|
58
|
+
.getScope(node)
|
|
49
59
|
.references.find(reference => reference.identifier.name === node.property.name)
|
|
50
60
|
const def = ref.resolved?.defs?.[0]
|
|
51
61
|
if (def?.node?.init?.type === 'Literal') {
|
package/src/utils/css-modules.js
CHANGED
|
@@ -4,8 +4,8 @@ function importBindingIsFromCSSModuleImport(node) {
|
|
|
4
4
|
|
|
5
5
|
function identifierIsCSSModuleBinding(node, context) {
|
|
6
6
|
if (node.type !== 'Identifier') return false
|
|
7
|
-
const ref = context.getScope().references.find(reference => reference.identifier.name === node.name)
|
|
8
|
-
if (ref.resolved?.defs?.some(importBindingIsFromCSSModuleImport)) {
|
|
7
|
+
const ref = context.sourceCode.getScope(node).references.find(reference => reference.identifier.name === node.name)
|
|
8
|
+
if (ref && ref.resolved?.defs?.some(importBindingIsFromCSSModuleImport)) {
|
|
9
9
|
return true
|
|
10
10
|
}
|
|
11
11
|
return false
|
|
@@ -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
|
-
})
|