eslint-plugin-primer-react 5.4.0 → 6.0.0-rc.0df8090

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": "5.4.0",
3
+ "version": "6.0.0-rc.0df8090",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -33,7 +33,9 @@
33
33
  "eslint-plugin-jsx-a11y": "^6.7.1",
34
34
  "eslint-traverse": "^1.0.0",
35
35
  "lodash": "^4.17.21",
36
- "styled-system": "^5.1.5"
36
+ "styled-system": "^5.1.5",
37
+ "@typescript-eslint/utils": "7.16.0",
38
+ "typescript": "^5.5.3"
37
39
  },
38
40
  "devDependencies": {
39
41
  "@changesets/changelog-github": "^0.5.0",
@@ -44,7 +46,9 @@
44
46
  "eslint-plugin-prettier": "^5.0.1",
45
47
  "jest": "^29.7.0",
46
48
  "markdownlint-cli2": "^0.13.0",
47
- "markdownlint-cli2-formatter-pretty": "^0.0.6"
49
+ "markdownlint-cli2-formatter-pretty": "^0.0.6",
50
+ "@typescript-eslint/rule-tester": "7.16.0",
51
+ "@types/jest": "^29.5.12"
48
52
  },
49
53
  "prettier": "@github/prettier-config"
50
54
  }
@@ -18,6 +18,7 @@ module.exports = {
18
18
  'primer-react/no-deprecated-props': 'warn',
19
19
  'primer-react/a11y-remove-disable-tooltip': 'error',
20
20
  'primer-react/a11y-use-next-tooltip': 'error',
21
+ 'primer-react/no-unnecessary-components': 'error',
21
22
  },
22
23
  settings: {
23
24
  github: {
package/src/index.js CHANGED
@@ -10,6 +10,8 @@ module.exports = {
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
12
  'a11y-use-next-tooltip': require('./rules/a11y-use-next-tooltip'),
13
+ 'use-deprecated-from-deprecated': require('./rules/use-deprecated-from-deprecated'),
14
+ 'primer-react/no-unnecessary-components': require('./rules/no-unnecessary-components'),
13
15
  },
14
16
  configs: {
15
17
  recommended: require('./configs/recommended'),
@@ -0,0 +1 @@
1
+ // https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
@@ -0,0 +1 @@
1
+ // https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
@@ -0,0 +1,7 @@
1
+ // https://typescript-eslint.io/packages/rule-tester/#type-aware-testing
2
+ {
3
+ "compilerOptions": {
4
+ "strict": true
5
+ },
6
+ "include": ["file.ts", "File.tsx"]
7
+ }
@@ -0,0 +1,153 @@
1
+ // @ts-check
2
+
3
+ const {RuleTester} = require('@typescript-eslint/rule-tester')
4
+
5
+ const path = require('node:path')
6
+ const rule = require('../no-unnecessary-components')
7
+ const {components} = require('../no-unnecessary-components')
8
+
9
+ const prcImport = 'import React from "react"; import {Box, Text} from "@primer/react";'
10
+ const brandImport = 'import React from "react"; import {Box, Text} from "@primer/brand";'
11
+
12
+ /** @param {string} content */
13
+ const jsx = content => `export const Component = () => <>${content}</>`
14
+
15
+ const sxObjectDeclaration = `const props = {sx: {color: "red"}};`
16
+ const asObjectDeclaration = `const props = {as: "table"};`
17
+ const stringRecordDeclaration = `const props: Record<string, any> = {};`
18
+ const testIdObjectDeclaration = `const props = {'data-testid': 'xyz'};`
19
+ const componentDeclaration = `const OtherComponent = ({children}: {children: React.ReactNode}) => <>{children}</>;`
20
+ const asConstDeclaration = `const as = "p";`
21
+
22
+ const ruleTester = new RuleTester({
23
+ parser: '@typescript-eslint/parser',
24
+ parserOptions: {
25
+ tsconfigRootDir: path.resolve(__dirname, 'fixtures'),
26
+ project: path.resolve(__dirname, 'fixtures', 'tsconfig.json'),
27
+ },
28
+ defaultFilenames: {
29
+ ts: 'file.ts',
30
+ tsx: 'File.tsx',
31
+ },
32
+ })
33
+
34
+ jest.retryTimes(0, {logErrorsBeforeRetry: true})
35
+
36
+ const filename = 'File.tsx'
37
+
38
+ ruleTester.run('unnecessary-components', rule, {
39
+ valid: [
40
+ {name: 'Unrelated JSX', code: jsx('<span>Hello World</span>'), filename},
41
+ ...Object.keys(components).flatMap(component => [
42
+ {
43
+ name: `Non-PRC ${component}`,
44
+ code: `${brandImport}${jsx(`<${component}>Hello World</${component}>`)}`,
45
+ filename,
46
+ },
47
+ {
48
+ name: `${component} with sx prop`,
49
+ code: `${prcImport}${jsx(`<${component} sx={{color: "red"}}>Hello World</${component}>`)}`,
50
+ filename,
51
+ },
52
+ {
53
+ name: `${component} with any styled-system prop`,
54
+ code: `${prcImport}${jsx(`<${component} flex="row">Hello World</${component}>`)}`,
55
+ filename,
56
+ },
57
+ {
58
+ name: `${component} with spread sx prop`,
59
+ code: `${prcImport}${sxObjectDeclaration}${jsx(`<${component} {...props}>Hello World</${component}>`)}`,
60
+ filename,
61
+ },
62
+ {
63
+ name: `${component} with string index spread props`,
64
+ code: `${prcImport}${stringRecordDeclaration}${jsx(`<${component} {...props}>Hello World</${component}>`)}`,
65
+ filename,
66
+ },
67
+ ]),
68
+ {
69
+ name: `Text with weight prop`,
70
+ code: `${prcImport}${jsx(`<Text weight='medium'>Hello World</Text>`)}`,
71
+ filename,
72
+ },
73
+ {
74
+ name: `Text with size prop`,
75
+ code: `${prcImport}${jsx(`<Text size='small'>Hello World</Text>`)}`,
76
+ filename,
77
+ },
78
+ ],
79
+ invalid: Object.entries(components).flatMap(([component, {messageId, replacement}]) => [
80
+ {
81
+ name: `${component} without any styled-system props`,
82
+ code: `${prcImport}${jsx(`<${component}>Hello World</${component}>`)}`,
83
+ output: `${prcImport}${jsx(`<${replacement}>Hello World</${replacement}>`)}`,
84
+ errors: [{messageId}],
85
+ filename,
86
+ },
87
+ {
88
+ name: `Self-closing ${component} without any styled-system props`,
89
+ code: `${prcImport}${jsx(`<${component} />`)}`,
90
+ output: `${prcImport}${jsx(`<${replacement} />`)}`,
91
+ errors: [{messageId}],
92
+ filename,
93
+ },
94
+ {
95
+ name: `${component} with spread props without sx`,
96
+ code: `${prcImport}${testIdObjectDeclaration}${jsx(`<${component} {...props}>Hello World</${component}>`)}`,
97
+ output: `${prcImport}${testIdObjectDeclaration}${jsx(`<${replacement} {...props}>Hello World</${replacement}>`)}`,
98
+ errors: [{messageId}],
99
+ filename,
100
+ },
101
+ {
102
+ name: `${component} with string element 'as' prop`,
103
+ code: `${prcImport}${jsx(`<${component} as="code">Hello world</${component}>`)}`,
104
+ // There is extra whitespace here we don't worry about since formatters would get rid of it
105
+ output: `${prcImport}${jsx(`<code >Hello world</code>`)}`,
106
+ errors: [{messageId}],
107
+ filename,
108
+ },
109
+ {
110
+ name: `${component} with single-character 'as' prop`,
111
+ code: `${prcImport}${jsx(`<${component} as="p">Hello world</${component}>`)}`,
112
+ output: `${prcImport}${jsx(`<p >Hello world</p>`)}`,
113
+ errors: [{messageId}],
114
+ filename,
115
+ },
116
+ {
117
+ name: `${component} with string element 'as' prop surrounded by unnecessary braces`,
118
+ code: `${prcImport}${jsx(`<${component} as={"code"}>Hello world</${component}>`)}`,
119
+ output: `${prcImport}${jsx(`<code >Hello world</code>`)}`,
120
+ errors: [{messageId}],
121
+ filename,
122
+ },
123
+ {
124
+ name: `${component} with component reference 'as' prop`,
125
+ code: `${prcImport}${componentDeclaration}${jsx(`<${component} as={OtherComponent}>Hello world</${component}>`)}`,
126
+ output: `${prcImport}${componentDeclaration}${jsx(`<OtherComponent >Hello world</OtherComponent>`)}`,
127
+ errors: [{messageId}],
128
+ filename,
129
+ },
130
+ {
131
+ name: `${component} with spread 'as' prop`,
132
+ code: `${prcImport}${asObjectDeclaration}${jsx(`<${component} {...props}>Hello world</${component}>`)}`,
133
+ output: null,
134
+ errors: [{messageId}],
135
+ filename,
136
+ },
137
+ {
138
+ name: `${component} with unusable lowercase reference 'as' prop`,
139
+ code: `${prcImport}${asConstDeclaration}${jsx(`<${component} as={as}>Hello world</${component}>`)}`,
140
+ output: null,
141
+ errors: [{messageId}],
142
+ filename,
143
+ },
144
+ {
145
+ name: `Non-PRC ${component} when \`skipImportCheck\` is enabled`,
146
+ code: `${brandImport}${jsx(`<${component}>Hello World</${component}>`)}`,
147
+ output: `${brandImport}${jsx(`<${replacement}>Hello World</${replacement}>`)}`,
148
+ filename,
149
+ errors: [{messageId}],
150
+ options: [{skipImportCheck: true}],
151
+ },
152
+ ]),
153
+ })
@@ -0,0 +1,66 @@
1
+ 'use strict'
2
+
3
+ const {RuleTester} = require('eslint')
4
+ const rule = require('../use-deprecated-from-deprecated')
5
+
6
+ const ruleTester = new RuleTester({
7
+ parserOptions: {
8
+ ecmaVersion: 'latest',
9
+ sourceType: 'module',
10
+ ecmaFeatures: {
11
+ jsx: true,
12
+ },
13
+ },
14
+ })
15
+
16
+ ruleTester.run('use-deprecated-from-deprecated', rule, {
17
+ valid: [],
18
+ invalid: [
19
+ // Single deprecated import
20
+ {
21
+ code: `import {Tooltip} from '@primer/react'`,
22
+ output: `import {Tooltip} from '@primer/react/deprecated'`,
23
+ errors: ['Import deprecated components from @primer/react/deprecated'],
24
+ },
25
+
26
+ // Single deprecated import with existing deprecated entrypoint
27
+ {
28
+ code: `import {Tooltip} from '@primer/react'
29
+ import {Dialog} from '@primer/react/deprecated'`,
30
+ output: `\nimport {Dialog, Tooltip} from '@primer/react/deprecated'`,
31
+ errors: ['Import deprecated components from @primer/react/deprecated'],
32
+ },
33
+
34
+ // Multiple deprecated imports
35
+ {
36
+ code: `import {Dialog, Tooltip} from '@primer/react'`,
37
+ output: `import {Dialog, Tooltip} from '@primer/react/deprecated'`,
38
+ errors: ['Import deprecated components from @primer/react/deprecated'],
39
+ },
40
+
41
+ // Mixed deprecated and non-deprecated imports
42
+ {
43
+ code: `import {Button, Tooltip} from '@primer/react'`,
44
+ output: `import {Button, } from '@primer/react'
45
+ import {Tooltip} from '@primer/react/deprecated'`,
46
+ errors: ['Import deprecated components from @primer/react/deprecated'],
47
+ },
48
+
49
+ // Mixed deprecated and non-deprecated imports with existing deprecated
50
+ {
51
+ code: `import {Button, Tooltip} from '@primer/react'
52
+ import {Dialog} from '@primer/react/deprecated'`,
53
+ output: `import {Button, } from '@primer/react'
54
+ import {Dialog, Tooltip} from '@primer/react/deprecated'`,
55
+ errors: ['Import deprecated components from @primer/react/deprecated'],
56
+ },
57
+
58
+ // Multiple mixed deprecated and non-deprecated imports
59
+ {
60
+ code: `import {Button, Dialog, Tooltip} from '@primer/react'`,
61
+ output: `import {Button, } from '@primer/react'
62
+ import {Dialog, Tooltip} from '@primer/react/deprecated'`,
63
+ errors: ['Import deprecated components from @primer/react/deprecated'],
64
+ },
65
+ ],
66
+ })
@@ -0,0 +1,160 @@
1
+ // @ts-check
2
+
3
+ const {ESLintUtils} = require('@typescript-eslint/utils')
4
+ const {IndexKind} = require('typescript')
5
+ const {pick: pickStyledSystemProps} = require('@styled-system/props')
6
+ const {isPrimerComponent} = require('../utils/is-primer-component')
7
+
8
+ /** @typedef {import('@typescript-eslint/types').TSESTree.JSXAttribute} JSXAttribute */
9
+
10
+ const components = {
11
+ Box: {
12
+ replacement: 'div',
13
+ messageId: 'unecessaryBox',
14
+ message: 'Prefer plain HTML elements over `Box` when not using `sx` for styling.',
15
+ allowedProps: new Set(['sx']), // + styled-system props
16
+ },
17
+ Text: {
18
+ replacement: 'span',
19
+ messageId: 'unecessarySpan',
20
+ message: 'Prefer plain HTML elements over `Text` when not using `sx` for styling.',
21
+ allowedProps: new Set(['sx', 'size', 'weight']), // + styled-system props
22
+ },
23
+ }
24
+
25
+ const elementNameRegex = /^[a-z]\w*$/
26
+ const componentNameRegex = /^[A-Z][\w._]*$/
27
+
28
+ /** @param {string} propName */
29
+ const isStyledSystemProp = propName => propName in pickStyledSystemProps({[propName]: propName})
30
+
31
+ const rule = ESLintUtils.RuleCreator.withoutDocs({
32
+ meta: {
33
+ docs: {
34
+ description:
35
+ '`Box` and `Text` should only be used to provide access to the `sx` styling system and have a performance cost. If `sx` props are not being used, prefer `div` and `span` instead.',
36
+ },
37
+ messages: {
38
+ [components.Box.messageId]: components.Box.message,
39
+ [components.Text.messageId]: components.Text.message,
40
+ },
41
+ type: 'problem',
42
+ schema: [
43
+ {
44
+ type: 'object',
45
+ properties: {
46
+ skipImportCheck: {
47
+ type: 'boolean',
48
+ },
49
+ },
50
+ additionalProperties: false,
51
+ },
52
+ ],
53
+ fixable: 'code',
54
+ },
55
+ defaultOptions: [{skipImportCheck: false}],
56
+ create(context) {
57
+ return {
58
+ JSXElement({openingElement, closingElement}) {
59
+ const {name, attributes} = openingElement
60
+
61
+ // Ensure this is one of the components we are looking for. Note this doesn't account for import aliases; this
62
+ // is intentional to avoid having to do the scope tree traversal for every component of every name, which would
63
+ // be needlessly expensive. We just ignore aliased imports.
64
+ if (name.type !== 'JSXIdentifier' || !(name.name in components)) return
65
+ const componentConfig = components[/** @type {keyof typeof components} */ (name.name)]
66
+
67
+ // Only continue if the variable declaration is an import from @primer/react. Otherwise it could, for example,
68
+ // be an import from @primer/brand, which would be valid without sx.
69
+ const skipImportCheck = context.options[0]?.skipImportCheck
70
+ const isPrimer = skipImportCheck || isPrimerComponent(name, context.sourceCode.getScope(openingElement))
71
+ if (!isPrimer) return
72
+
73
+ /** @param {string} name */
74
+ const isAllowedProp = name => componentConfig.allowedProps.has(name) || isStyledSystemProp(name)
75
+
76
+ // Validate the attributes and ensure an allowed prop is present or spreaded in
77
+ /** @type {typeof attributes[number] | undefined | null} */
78
+ let asProp = undefined
79
+ for (const attribute of attributes) {
80
+ // If there is a spread type, check if the type of the spreaded value has an allowed property
81
+ if (attribute.type === 'JSXSpreadAttribute') {
82
+ const services = ESLintUtils.getParserServices(context)
83
+ const typeChecker = services.program.getTypeChecker()
84
+
85
+ const spreadType = services.getTypeAtLocation(attribute.argument)
86
+
87
+ // Check if the spread type has a string index signature - this could hide an allowed property
88
+ if (typeChecker.getIndexTypeOfType(spreadType, IndexKind.String) !== undefined) return
89
+
90
+ const spreadPropNames = typeChecker.getPropertiesOfType(spreadType).map(prop => prop.getName())
91
+
92
+ // If an allowed prop gets spread in, this is a valid use of the component
93
+ if (spreadPropNames.some(isAllowedProp)) return
94
+
95
+ // If there is an `as` inside the spread object, we can't autofix reliably
96
+ if (spreadPropNames.includes('as')) asProp = null
97
+
98
+ continue
99
+ }
100
+
101
+ // Has an allowed prop, so should keep using this component
102
+ if (attribute.name.type === 'JSXIdentifier' && isAllowedProp(attribute.name.name)) return
103
+
104
+ // If there is an `as` prop we will need to account for that when autofixing
105
+ if (attribute.name.type === 'JSXIdentifier' && attribute.name.name === 'as') asProp = attribute
106
+ }
107
+
108
+ // Determine a replacement component name accounting for the `as` prop if present
109
+ /** @type {string | null} */
110
+ let replacement = componentConfig.replacement
111
+ if (asProp === null) {
112
+ // {...{as: 'something-unusable'}}
113
+ replacement = null
114
+ } else if (asProp?.type === 'JSXAttribute') {
115
+ // as={ComponentReference}
116
+ if (asProp.value?.type === 'JSXExpressionContainer' && asProp.value.expression.type === 'Identifier') {
117
+ // can't just use expression.name here because we want the whole expression if it's A.B
118
+ const expressionStr = context.sourceCode.getText(asProp.value.expression)
119
+ replacement = componentNameRegex.test(expressionStr) ? expressionStr : null
120
+ }
121
+ // as={'tagName'} (surprisingly common, we really should enable `react/jsx-curly-brace-presence`)
122
+ else if (
123
+ asProp.value?.type === 'JSXExpressionContainer' &&
124
+ asProp.value.expression.type === 'Literal' &&
125
+ typeof asProp.value.expression.value === 'string' &&
126
+ elementNameRegex.test(asProp.value.expression.value)
127
+ ) {
128
+ replacement = asProp.value.expression.value
129
+ }
130
+ // as="tagName"
131
+ else if (
132
+ asProp.value?.type === 'Literal' &&
133
+ typeof asProp.value.value === 'string' &&
134
+ elementNameRegex.test(asProp.value.value)
135
+ ) {
136
+ replacement = asProp.value.value
137
+ }
138
+ // too complex to autofix
139
+ else {
140
+ replacement = null
141
+ }
142
+ }
143
+
144
+ context.report({
145
+ node: name,
146
+ messageId: componentConfig.messageId,
147
+ fix: replacement
148
+ ? function* (fixer) {
149
+ yield fixer.replaceText(name, replacement)
150
+ if (closingElement) yield fixer.replaceText(closingElement.name, replacement)
151
+ if (asProp) yield fixer.remove(asProp)
152
+ }
153
+ : undefined,
154
+ })
155
+ },
156
+ }
157
+ },
158
+ })
159
+
160
+ module.exports = {...rule, components}
@@ -0,0 +1,130 @@
1
+ 'use strict'
2
+
3
+ const url = require('../url')
4
+
5
+ const components = [
6
+ {
7
+ identifier: 'Dialog',
8
+ entrypoint: '@primer/react',
9
+ },
10
+ {
11
+ identifier: 'Octicon',
12
+ entrypoint: '@primer/react',
13
+ },
14
+ {
15
+ identifier: 'Pagehead',
16
+ entrypoint: '@primer/react',
17
+ },
18
+ {
19
+ identifier: 'TabNav',
20
+ entrypoint: '@primer/react',
21
+ },
22
+ {
23
+ identifier: 'Tooltip',
24
+ entrypoint: '@primer/react',
25
+ },
26
+ ]
27
+
28
+ const entrypoints = new Map()
29
+
30
+ for (const component of components) {
31
+ if (!entrypoints.has(component.entrypoint)) {
32
+ entrypoints.set(component.entrypoint, new Set())
33
+ }
34
+ entrypoints.get(component.entrypoint).add(component.identifier)
35
+ }
36
+
37
+ /**
38
+ * @type {import('eslint').Rule.RuleModule}
39
+ */
40
+ module.exports = {
41
+ meta: {
42
+ type: 'problem',
43
+ docs: {
44
+ description: 'Use deprecated components from the `@primer/react/deprecated` entrypoint',
45
+ recommended: true,
46
+ url: url(module),
47
+ },
48
+ fixable: true,
49
+ schema: [],
50
+ },
51
+ create(context) {
52
+ const sourceCode = context.getSourceCode()
53
+
54
+ return {
55
+ ImportDeclaration(node) {
56
+ if (!entrypoints.has(node.source.value)) {
57
+ return
58
+ }
59
+
60
+ const entrypoint = entrypoints.get(node.source.value)
61
+ const deprecated = node.specifiers.filter(specifier => {
62
+ return entrypoint.has(specifier.imported.name)
63
+ })
64
+
65
+ if (deprecated.length === 0) {
66
+ return
67
+ }
68
+
69
+ const deprecatedEntrypoint = node.parent.body.find(node => {
70
+ if (node.type !== 'ImportDeclaration') {
71
+ return false
72
+ }
73
+
74
+ return node.source.value === '@primer/react/deprecated'
75
+ })
76
+
77
+ // All imports are deprecated
78
+ if (deprecated.length === node.specifiers.length) {
79
+ context.report({
80
+ node,
81
+ message: 'Import deprecated components from @primer/react/deprecated',
82
+ *fix(fixer) {
83
+ if (deprecatedEntrypoint) {
84
+ const lastSpecifier = deprecatedEntrypoint.specifiers[deprecatedEntrypoint.specifiers.length - 1]
85
+
86
+ yield fixer.remove(node)
87
+ yield fixer.insertTextAfter(
88
+ lastSpecifier,
89
+ `, ${node.specifiers.map(specifier => specifier.imported.name).join(', ')}`,
90
+ )
91
+ } else {
92
+ yield fixer.replaceText(node.source, `'@primer/react/deprecated'`)
93
+ }
94
+ },
95
+ })
96
+ } else {
97
+ // There is a mix of deprecated and non-deprecated imports
98
+ context.report({
99
+ node,
100
+ message: 'Import deprecated components from @primer/react/deprecated',
101
+ *fix(fixer) {
102
+ for (const specifier of deprecated) {
103
+ yield fixer.remove(specifier)
104
+ const comma = sourceCode.getTokenAfter(specifier)
105
+ if (comma.value === ',') {
106
+ yield fixer.remove(comma)
107
+ }
108
+ }
109
+
110
+ if (deprecatedEntrypoint) {
111
+ const lastSpecifier = deprecatedEntrypoint.specifiers[deprecatedEntrypoint.specifiers.length - 1]
112
+ yield fixer.insertTextAfter(
113
+ lastSpecifier,
114
+ `, ${deprecated.map(specifier => specifier.imported.name).join(', ')}`,
115
+ )
116
+ } else {
117
+ yield fixer.insertTextAfter(
118
+ node,
119
+ `\nimport {${deprecated
120
+ .map(specifier => specifier.imported.name)
121
+ .join(', ')}} from '@primer/react/deprecated'`,
122
+ )
123
+ }
124
+ },
125
+ })
126
+ }
127
+ },
128
+ }
129
+ },
130
+ }
@@ -1,5 +1,6 @@
1
1
  const {isImportedFrom} = require('./is-imported-from')
2
2
 
3
+ /** @returns {boolean} */
3
4
  function isPrimerComponent(name, scope) {
4
5
  let identifier
5
6