eslint-plugin-primer-react 6.2.0-rc.2c85052 → 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 CHANGED
@@ -1,9 +1,15 @@
1
1
  # eslint-plugin-primer-react
2
2
 
3
- ## 6.2.0
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
 
11
+ - [#266](https://github.com/primer/eslint-plugin-primer-react/pull/266) [`90134bc`](https://github.com/primer/eslint-plugin-primer-react/commit/90134bcee073c424e81c53e69632e1518798af93) Thanks [@keithamus](https://github.com/keithamus)! - Add enforce-css-module-default-import rule
12
+
7
13
  - [#258](https://github.com/primer/eslint-plugin-primer-react/pull/258) [`83f29f3`](https://github.com/primer/eslint-plugin-primer-react/commit/83f29f339999b9c21d95167bcc2680c1797cbab6) Thanks [@keithamus](https://github.com/keithamus)! - Add enforce-css-module-identifier-casing rule
8
14
 
9
15
  ## 6.1.6
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-next-tooltip](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/a11y-use-next-tooltip.md)
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
+ ```
@@ -0,0 +1,53 @@
1
+ # Enforce CSS Module Default Import (enforce-css-module-default-import)
2
+
3
+ CSS Modules should import only the default import. Avoid named imports which can often conflict with other variables (including the function name of the React component) when importing css modules.
4
+
5
+ Additionally, for consistency among the codebase the default import should be consistently named. This rule allows enforcing the name of the default import, which by default expects it to be named either `classes` or be suffixed `Classes`.
6
+
7
+ ## Rule details
8
+
9
+ This rule disallows the use of any CSS Module property that does not match the desired casing.
10
+
11
+ 👎 Examples of **incorrect** code for this rule:
12
+
13
+ ```jsx
14
+ /* eslint primer-react/enforce-css-module-default-import: "error" */
15
+ import {Button} from '@primer/react'
16
+ import {Button as ButtonClass} from './some.module.css' // oops! Conflict!
17
+ ```
18
+
19
+ ```jsx
20
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */
21
+ import {Button} from '@primer/react'
22
+ import classnames from './some.module.css' // This default import should match /^classes$/
23
+ ```
24
+
25
+ 👍 Examples of **correct** code for this rule:
26
+
27
+ ```jsx
28
+ /* eslint primer-react/enforce-css-module-default-import: "error" */
29
+ import {Button} from '@primer/react'
30
+ import classes from './some.module.css'
31
+ ```
32
+
33
+ ```jsx
34
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"^classes$"}] */
35
+ import {Button} from '@primer/react'
36
+ import classes from './some.module.css'
37
+ ```
38
+
39
+ ```jsx
40
+ /* eslint primer-react/enforce-css-module-default-import: ["error",{enforceName:"(^classes$|Classes$)"}] */
41
+ import {Button} from '@primer/react'
42
+ import classes from './some.module.css'
43
+ import sharedClasses from './shared.module.css'
44
+ import utilClasses from './util.module.css'
45
+ ```
46
+
47
+ ## Options
48
+
49
+ - `enforceName` (default: `.*`)
50
+
51
+ By default, the `enforce-css-module-default-import` rule will allow any name for the default export,
52
+ however in the _recommended_ ruleset this is set to `(^classes$|Classes$)` meaning that the default
53
+ export name must either be exactly `classes` or end in `Classes`, for example `sharedClasses`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "6.2.0-rc.2c85052",
3
+ "version": "7.0.0-rc.55939f7",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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-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',
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-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'),
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') {
@@ -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
- })
@@ -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
- }