eslint-plugin-primer-react 8.1.0 → 8.2.0-rc.b646af2

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": "8.1.0",
3
+ "version": "8.2.0-rc.b646af2",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "engines": {
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@styled-system/props": "^5.1.5",
35
- "@typescript-eslint/utils": "8.39.0",
35
+ "@typescript-eslint/utils": "8.43.0",
36
36
  "eslint-plugin-github": "^6.0.0",
37
37
  "eslint-plugin-jsx-a11y": "^6.7.1",
38
38
  "eslint-traverse": "^1.0.0",
@@ -46,7 +46,7 @@
46
46
  "@github/markdownlint-github": "^0.6.3",
47
47
  "@github/prettier-config": "0.0.6",
48
48
  "@types/jest": "^30.0.0",
49
- "@typescript-eslint/rule-tester": "8.39.0",
49
+ "@typescript-eslint/rule-tester": "8.42.0",
50
50
  "eslint": "^9.0.0",
51
51
  "eslint-plugin-eslint-comments": "^3.2.0",
52
52
  "eslint-plugin-filenames": "^1.3.2",
@@ -249,3 +249,64 @@ import { Button as StyledButton, Link } from '@primer/styled-react'
249
249
  },
250
250
  ],
251
251
  })
252
+
253
+ // Test configuration options
254
+ ruleTester.run('use-styled-react-import with custom configuration', rule, {
255
+ valid: [
256
+ // Valid: Custom component not in default list
257
+ {
258
+ code: `import { CustomButton } from '@primer/react'
259
+ const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
260
+ options: [{}], // Using default configuration
261
+ },
262
+
263
+ // Valid: Custom component in custom list used without sx prop
264
+ {
265
+ code: `import { CustomButton } from '@primer/react'
266
+ const Component = () => <CustomButton>Click me</CustomButton>`,
267
+ options: [{styledComponents: ['CustomButton']}],
268
+ },
269
+
270
+ // Valid: Custom component with sx prop imported from styled-react
271
+ {
272
+ code: `import { CustomButton } from '@primer/styled-react'
273
+ const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
274
+ options: [{styledComponents: ['CustomButton']}],
275
+ },
276
+
277
+ // Valid: Box not in custom list, so sx usage is allowed from @primer/react
278
+ {
279
+ code: `import { Box } from '@primer/react'
280
+ const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`,
281
+ options: [{styledComponents: ['CustomButton']}], // Box not included
282
+ },
283
+ ],
284
+ invalid: [
285
+ // Invalid: Custom component with sx prop should be from styled-react
286
+ {
287
+ code: `import { CustomButton } from '@primer/react'
288
+ const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
289
+ output: `import { CustomButton } from '@primer/styled-react'
290
+ const Component = () => <CustomButton sx={{ color: 'red' }}>Click me</CustomButton>`,
291
+ options: [{styledComponents: ['CustomButton']}],
292
+ errors: [
293
+ {
294
+ messageId: 'useStyledReactImport',
295
+ data: {componentName: 'CustomButton'},
296
+ },
297
+ ],
298
+ },
299
+ // Invalid: Custom utility should be from styled-react
300
+ {
301
+ code: `import { customSx } from '@primer/react'`,
302
+ output: `import { customSx } from '@primer/styled-react'`,
303
+ options: [{styledUtilities: ['customSx']}],
304
+ errors: [
305
+ {
306
+ messageId: 'moveToStyledReact',
307
+ data: {importName: 'customSx'},
308
+ },
309
+ ],
310
+ },
311
+ ],
312
+ })
@@ -3,8 +3,8 @@
3
3
  const url = require('../url')
4
4
  const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
5
5
 
6
- // Components that should be imported from @primer/styled-react when used with sx prop
7
- const styledComponents = new Set([
6
+ // Default components that should be imported from @primer/styled-react when used with sx prop
7
+ const defaultStyledComponents = [
8
8
  'ActionList',
9
9
  'ActionMenu',
10
10
  'Box',
@@ -23,13 +23,13 @@ const styledComponents = new Set([
23
23
  'Truncate',
24
24
  'Octicon',
25
25
  'Dialog',
26
- ])
26
+ ]
27
27
 
28
- // Types that should be imported from @primer/styled-react
29
- const styledTypes = new Set(['BoxProps', 'SxProp', 'BetterSystemStyleObject'])
28
+ // Default types that should be imported from @primer/styled-react
29
+ const defaultStyledTypes = ['BoxProps', 'SxProp', 'BetterSystemStyleObject']
30
30
 
31
- // Utilities that should be imported from @primer/styled-react
32
- const styledUtilities = new Set(['sx'])
31
+ // Default utilities that should be imported from @primer/styled-react
32
+ const defaultStyledUtilities = ['sx']
33
33
 
34
34
  /**
35
35
  * @type {import('eslint').Rule.RuleModule}
@@ -43,7 +43,29 @@ module.exports = {
43
43
  url: url(module),
44
44
  },
45
45
  fixable: 'code',
46
- schema: [],
46
+ schema: [
47
+ {
48
+ type: 'object',
49
+ properties: {
50
+ styledComponents: {
51
+ type: 'array',
52
+ items: {type: 'string'},
53
+ description: 'Components that should be imported from @primer/styled-react when used with sx prop',
54
+ },
55
+ styledTypes: {
56
+ type: 'array',
57
+ items: {type: 'string'},
58
+ description: 'Types that should be imported from @primer/styled-react',
59
+ },
60
+ styledUtilities: {
61
+ type: 'array',
62
+ items: {type: 'string'},
63
+ description: 'Utilities that should be imported from @primer/styled-react',
64
+ },
65
+ },
66
+ additionalProperties: false,
67
+ },
68
+ ],
47
69
  messages: {
48
70
  useStyledReactImport: 'Import {{ componentName }} from "@primer/styled-react" when using with sx prop',
49
71
  useStyledReactImportWithAlias:
@@ -54,6 +76,11 @@ module.exports = {
54
76
  },
55
77
  },
56
78
  create(context) {
79
+ // Get configuration options or use defaults
80
+ const options = context.options[0] || {}
81
+ const styledComponents = new Set(options.styledComponents || defaultStyledComponents)
82
+ const styledTypes = new Set(options.styledTypes || defaultStyledTypes)
83
+ const styledUtilities = new Set(options.styledUtilities || defaultStyledUtilities)
57
84
  const componentsWithSx = new Set()
58
85
  const componentsWithoutSx = new Set() // Track components used without sx
59
86
  const allUsedComponents = new Set() // Track all used components