eslint-plugin-primer-react 8.0.0-rc.d848c88 → 8.1.0-rc.764fa1e

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.
Files changed (34) hide show
  1. package/.github/copilot-instructions.md +159 -0
  2. package/.github/workflows/add-to-inbox.yml +2 -2
  3. package/.markdownlint-cli2.cjs +2 -2
  4. package/CHANGELOG.md +12 -2
  5. package/docs/rules/use-styled-react-import.md +128 -0
  6. package/eslint.config.js +54 -0
  7. package/package-lock.json +2000 -8406
  8. package/package.json +15 -9
  9. package/src/index.js +1 -0
  10. package/src/rules/__tests__/a11y-explicit-heading.test.js +5 -3
  11. package/src/rules/__tests__/a11y-link-in-text-block.test.js +5 -3
  12. package/src/rules/__tests__/a11y-no-duplicate-form-labels.test.js +5 -9
  13. package/src/rules/__tests__/a11y-no-title-usage.test.js +5 -3
  14. package/src/rules/__tests__/a11y-remove-disable-tooltip.test.js +5 -3
  15. package/src/rules/__tests__/a11y-tooltip-interactive-trigger.test.js +5 -3
  16. package/src/rules/__tests__/a11y-use-accessible-tooltip.test.js +5 -3
  17. package/src/rules/__tests__/direct-slot-children.test.js +5 -3
  18. package/src/rules/__tests__/enforce-button-for-link-with-nohref.test.js +5 -3
  19. package/src/rules/__tests__/enforce-css-module-identifier-casing.test.js +5 -3
  20. package/src/rules/__tests__/new-color-css-vars.test.js +5 -3
  21. package/src/rules/__tests__/no-deprecated-entrypoints.test.js +5 -3
  22. package/src/rules/__tests__/no-deprecated-experimental-components.test.js +5 -3
  23. package/src/rules/__tests__/no-deprecated-props.test.js +5 -3
  24. package/src/rules/__tests__/no-system-props.test.js +5 -4
  25. package/src/rules/__tests__/no-unnecessary-components.test.js +6 -4
  26. package/src/rules/__tests__/no-wildcard-imports.test.js +6 -43
  27. package/src/rules/__tests__/prefer-action-list-item-onselect.test.js +6 -4
  28. package/src/rules/__tests__/use-deprecated-from-deprecated.test.js +5 -3
  29. package/src/rules/__tests__/use-styled-react-import.test.js +251 -0
  30. package/src/rules/no-deprecated-experimental-components.js +0 -1
  31. package/src/rules/no-system-props.js +1 -0
  32. package/src/rules/use-styled-react-import.js +483 -0
  33. package/.eslintignore +0 -2
  34. package/.eslintrc.js +0 -39
@@ -0,0 +1,159 @@
1
+ # eslint-plugin-primer-react
2
+
3
+ ESLint plugin for Primer React components. This is a JavaScript-based ESLint plugin that provides rules for validating and auto-fixing Primer React component usage.
4
+
5
+ **Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here.**
6
+
7
+ ## Working Effectively
8
+
9
+ ### Bootstrap and Setup
10
+
11
+ - Install Node.js v20+ (v20 is the current standard):
12
+ - Check version: `node --version && npm --version`
13
+ - Install dependencies: `npm ci` -- takes 60 seconds. Set timeout to 90+ seconds.
14
+ - **NO BUILD STEP REQUIRED** - This is a direct JavaScript project with main entry at `src/index.js`
15
+
16
+ ### Development Commands
17
+
18
+ - Run tests: `npm test` -- takes 5 seconds. Fast, no long timeout needed.
19
+ - Run linting: `npm run lint` -- takes 1.5 seconds. Very fast.
20
+ - Run markdown linting: `npm run lint:md` -- takes under 1 second. Very fast.
21
+ - Check formatting: `npm run format:check` -- takes 0.5 seconds. Very fast.
22
+ - Fix formatting: `npm run format` -- applies Prettier formatting fixes.
23
+
24
+ ### Testing and Validation
25
+
26
+ - **ALWAYS** run `npm test` after making changes to rules - tests run in 5 seconds
27
+ - **ALWAYS** run `npm run lint && npm run lint:md` before committing - both complete in under 3 seconds total
28
+ - **ALWAYS** run `npm run format:check` to verify formatting - completes in 0.5 seconds
29
+ - All validation commands are very fast - no need for long timeouts or cancellation warnings
30
+
31
+ ### Manual Rule Testing
32
+
33
+ You can manually test individual rules using this pattern:
34
+
35
+ ```bash
36
+ node -e "
37
+ const rule = require('./src/rules/RULE_NAME');
38
+ const {RuleTester} = require('eslint');
39
+ const ruleTester = new RuleTester({
40
+ parserOptions: {
41
+ ecmaVersion: 'latest',
42
+ sourceType: 'module',
43
+ ecmaFeatures: { jsx: true }
44
+ }
45
+ });
46
+ ruleTester.run('test', rule, {
47
+ valid: [{ code: 'VALID_CODE_HERE' }],
48
+ invalid: [{ code: 'INVALID_CODE_HERE', errors: [{ messageId: 'MESSAGE_ID' }] }]
49
+ });
50
+ "
51
+ ```
52
+
53
+ ## Repository Structure and Navigation
54
+
55
+ ### Key Directories
56
+
57
+ - `src/rules/` - ESLint rule implementations
58
+ - `src/rules/__tests__/` - Jest tests for each rule using ESLint RuleTester
59
+ - `docs/rules/` - Markdown documentation for each rule
60
+ - `src/configs/` - ESLint configuration presets (e.g., recommended.js)
61
+ - `src/utils/` - Utility functions shared across rules
62
+ - `.github/workflows/` - CI pipeline definitions
63
+
64
+ ### Important Files
65
+
66
+ - `src/index.js` - Main entry point, exports all rules and configs
67
+ - `package.json` - Scripts and dependencies (no build scripts needed)
68
+ - `jest.config.js` - Jest test configuration
69
+ - `.eslintrc.js` - ESLint configuration for the project itself
70
+ - `.nvmrc` - Node.js version specification (v20)
71
+
72
+ ### Rule Development Pattern
73
+
74
+ Each rule follows this structure:
75
+
76
+ 1. Rule implementation: `src/rules/rule-name.js`
77
+ 2. Test file: `src/rules/__tests__/rule-name.test.js`
78
+ 3. Documentation: `docs/rules/rule-name.md`
79
+ 4. Export from: `src/index.js` (add to rules object)
80
+ 5. Optional: Add to `src/configs/recommended.js` if should be in recommended preset
81
+
82
+ ## Validation Scenarios
83
+
84
+ ### After Making Rule Changes
85
+
86
+ 1. Run the rule's specific test: `npm test -- --testNamePattern="rule-name"`
87
+ 2. Run all tests: `npm test` (5 seconds)
88
+ 3. Test the rule manually using the Node.js snippet pattern above
89
+ 4. Verify the rule is exported properly from `src/index.js`
90
+
91
+ ### Before Committing
92
+
93
+ 1. `npm run lint` - JavaScript linting (1.5 seconds)
94
+ 2. `npm run lint:md` - Markdown linting (<1 second)
95
+ 3. `npm run format:check` - Formatting validation (0.5 seconds)
96
+ 4. `npm test` - Full test suite (5 seconds)
97
+
98
+ ### Testing Plugin Integration
99
+
100
+ The plugin can be tested by:
101
+
102
+ 1. Using manual Node.js rule testing (shown above)
103
+ 2. Running existing test suite which validates all rules
104
+ 3. Creating test files and using ESLint RuleTester in the **tests** files
105
+
106
+ ## Common Development Tasks
107
+
108
+ ### Adding a New Rule
109
+
110
+ 1. Create rule implementation: `src/rules/new-rule-name.js`
111
+ 2. Create test file: `src/rules/__tests__/new-rule-name.test.js`
112
+ 3. Add to exports in `src/index.js`
113
+ 4. Create documentation: `docs/rules/new-rule-name.md`
114
+ 5. Optionally add to `src/configs/recommended.js`
115
+ 6. Run tests: `npm test`
116
+ 7. Run linting: `npm run lint`
117
+
118
+ ### Modifying Existing Rules
119
+
120
+ 1. Edit rule in `src/rules/rule-name.js`
121
+ 2. Update tests in `src/rules/__tests__/rule-name.test.js`
122
+ 3. Update documentation in `docs/rules/rule-name.md` if needed
123
+ 4. Run tests: `npm test`
124
+ 5. Test manually using Node.js snippet if needed
125
+
126
+ ### Working with Changesets (for releases)
127
+
128
+ - `npx changeset` - Create a changeset for changes
129
+ - `npx changeset status` - Check changeset status
130
+ - Changesets are used for versioning and publishing to npm
131
+
132
+ ## Troubleshooting
133
+
134
+ ### Common Issues
135
+
136
+ - **Node.js version**: Use Node.js v20+ (v20 is the current standard)
137
+ - **Dependencies**: Always use `npm ci` instead of `npm install` for consistent installs
138
+ - **Test failures**: Run `npm test` to see specific failures - tests are fast and detailed
139
+ - **Lint failures**: Run `npm run lint` and `npm run lint:md` to see specific issues
140
+ - **Format issues**: Run `npm run format` to auto-fix formatting
141
+
142
+ ### Rule Testing Issues
143
+
144
+ - Use the RuleTester pattern shown above for manual testing
145
+ - Check that messageId in tests matches the rule's meta.messages
146
+ - Verify JSX parsing works by including ecmaFeatures.jsx in parserOptions
147
+
148
+ ## Command Reference
149
+
150
+ Essential commands and their typical execution times:
151
+
152
+ - `npm ci` - Install dependencies (60 seconds)
153
+ - `npm test` - Run all tests (5 seconds)
154
+ - `npm run lint` - Lint JavaScript (1.5 seconds)
155
+ - `npm run lint:md` - Lint Markdown (<1 second)
156
+ - `npm run format:check` - Check formatting (0.5 seconds)
157
+ - `npm run format` - Fix formatting (similar time)
158
+
159
+ All commands except `npm ci` are very fast. No need for extended timeouts or cancellation warnings on validation commands.
@@ -12,7 +12,7 @@ jobs:
12
12
  PROJECT_ID: 4503
13
13
  steps:
14
14
  - id: get-primer-access-token
15
- uses: actions/create-github-app-token@v1
15
+ uses: actions/create-github-app-token@v2
16
16
  with:
17
17
  app-id: ${{ vars.PRIMER_ISSUE_TRIAGE_APP_ID }}
18
18
  private-key: ${{ secrets.PRIMER_ISSUE_TRIAGE_APP_PRIVATE_KEY }}
@@ -22,7 +22,7 @@ jobs:
22
22
  env:
23
23
  GH_TOKEN: ${{ steps.get-primer-access-token.outputs.token }}
24
24
  - id: get-github-access-token
25
- uses: actions/create-github-app-token@v1
25
+ uses: actions/create-github-app-token@v2
26
26
  with:
27
27
  app-id: ${{ vars.PRIMER_ISSUE_TRIAGE_APP_ID_FOR_GITHUB }}
28
28
  private-key: ${{ secrets.PRIMER_ISSUE_TRIAGE_APP_PRIVATE_KEY_FOR_GITHUB }}
@@ -16,11 +16,11 @@ const options = githubMarkdownOpinions.init({
16
16
  'no-hard-tabs': false,
17
17
  'first-line-heading': false,
18
18
  'no-space-in-emphasis': false,
19
- 'blanks-around-fences': false
19
+ 'blanks-around-fences': false,
20
20
  })
21
21
 
22
22
  module.exports = {
23
23
  config: options,
24
24
  customRules: ['@github/markdownlint-github'],
25
- outputFormatters: [['markdownlint-cli2-formatter-pretty', {appendLink: true}]]
25
+ outputFormatters: [['markdownlint-cli2-formatter-pretty', {appendLink: true}]],
26
26
  }
package/CHANGELOG.md CHANGED
@@ -1,9 +1,21 @@
1
1
  # eslint-plugin-primer-react
2
2
 
3
+ ## 8.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#382](https://github.com/primer/eslint-plugin-primer-react/pull/382) [`93e4b76`](https://github.com/primer/eslint-plugin-primer-react/commit/93e4b76269bf816bfc24991180f73ef22266a6f5) Thanks [@jonrohan](https://github.com/jonrohan)! - Add rule `use-styled-react-import` to enforce importing components with sx prop from @primer/styled-react
8
+
9
+ ### Patch Changes
10
+
11
+ - [#389](https://github.com/primer/eslint-plugin-primer-react/pull/389) [`747fc82`](https://github.com/primer/eslint-plugin-primer-react/commit/747fc82dc9fc584005d98b36f8417985469a083a) Thanks [@llastflowers](https://github.com/llastflowers)! - Add exception for height and width for SkeletonBox in no-system-props rule
12
+
3
13
  ## 8.0.0
4
14
 
5
15
  ### Major Changes
6
16
 
17
+ - [#381](https://github.com/primer/eslint-plugin-primer-react/pull/381) [`52f3be6`](https://github.com/primer/eslint-plugin-primer-react/commit/52f3be6881a522b0c9b261fe5acbe0c84a7deb5a) Thanks [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)! - Upgrade to ESLint v9 support with eslint-plugin-github v6
18
+
7
19
  - [#380](https://github.com/primer/eslint-plugin-primer-react/pull/380) [`d42d5c0`](https://github.com/primer/eslint-plugin-primer-react/commit/d42d5c03a0e7df44efeed84baff2eca95af05113) Thanks [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)! - Update repository to Node.js v20
8
20
 
9
21
  ### Minor Changes
@@ -169,7 +181,6 @@
169
181
  ### Major Changes
170
182
 
171
183
  - [#174](https://github.com/primer/eslint-plugin-primer-react/pull/174) [`d9832b8`](https://github.com/primer/eslint-plugin-primer-react/commit/d9832b850cbcf808ddcdfd3efbbab7d2bf913ccd) Thanks [@langermank](https://github.com/langermank)! - - Remove `no-deprecated-colors` plugin
172
-
173
184
  - Remove dependency on `primer/primitives`
174
185
 
175
186
  - [#172](https://github.com/primer/eslint-plugin-primer-react/pull/172) [`8e24d66`](https://github.com/primer/eslint-plugin-primer-react/commit/8e24d660065b3c690a14d826580c793d7b305068) Thanks [@langermank](https://github.com/langermank)! - [Breaking] Remove `new-color-css-vars-have-fallback`
@@ -380,7 +391,6 @@
380
391
  - [#7](https://github.com/primer/eslint-plugin-primer-react/pull/7) [`d9dfb8d`](https://github.com/primer/eslint-plugin-primer-react/commit/d9dfb8de6d6dc42efe606517db7a0dd90d5c5578) Thanks [@colebemis](https://github.com/colebemis)! - Add `skipImportCheck` option. By default, the `no-deprecated-colors` rule will only check for deprecated colors used in functions and components that are imported from `@primer/react`. You can disable this behavior by setting `skipImportCheck` to `true`. This is useful for linting custom components that pass color-related props down to Primer React components.
381
392
 
382
393
  * [#6](https://github.com/primer/eslint-plugin-primer-react/pull/6) [`dd14594`](https://github.com/primer/eslint-plugin-primer-react/commit/dd14594b05e4d800baa76771f5b911d77352a983) Thanks [@colebemis](https://github.com/colebemis)! - The `no-deprecated-colors` rule can now find deprecated colors in the following cases:
383
-
384
394
  - Nested `sx` properties:
385
395
 
386
396
  ```jsx
@@ -0,0 +1,128 @@
1
+ # use-styled-react-import
2
+
3
+ 💼 This rule is _disabled_ in the ✅ `recommended` config.
4
+
5
+ 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6
+
7
+ <!-- end auto-generated rule header -->
8
+
9
+ Enforce importing components that use `sx` prop from `@primer/styled-react` instead of `@primer/react`, and vice versa.
10
+
11
+ ## Rule Details
12
+
13
+ This rule detects when certain Primer React components are used with the `sx` prop and ensures they are imported from the temporary `@primer/styled-react` package instead of `@primer/react`. When the same components are used without the `sx` prop, it ensures they are imported from `@primer/react` instead of `@primer/styled-react`.
14
+
15
+ When a component is used both with and without the `sx` prop in the same file, the rule will import the styled version with an alias (e.g., `StyledButton`) and update the JSX usage accordingly to avoid naming conflicts.
16
+
17
+ It also moves certain types and utilities to the styled-react package.
18
+
19
+ ### Components that should be imported from `@primer/styled-react` when used with `sx`:
20
+
21
+ - ActionList
22
+ - ActionMenu
23
+ - Box
24
+ - Breadcrumbs
25
+ - Button
26
+ - Flash
27
+ - FormControl
28
+ - Heading
29
+ - IconButton
30
+ - Label
31
+ - Link
32
+ - LinkButton
33
+ - PageLayout
34
+ - Text
35
+ - TextInput
36
+ - Truncate
37
+ - Octicon
38
+ - Dialog
39
+
40
+ ### Types and utilities that should always be imported from `@primer/styled-react`:
41
+
42
+ - `BoxProps` (type)
43
+ - `SxProp` (type)
44
+ - `BetterSystemStyleObject` (type)
45
+ - `sx` (utility)
46
+
47
+ ## Examples
48
+
49
+ ### ❌ Incorrect
50
+
51
+ ```jsx
52
+ import {Button, Link} from '@primer/react'
53
+
54
+ const Component = () => <Button sx={{color: 'red'}}>Click me</Button>
55
+ ```
56
+
57
+ ```jsx
58
+ import {Box} from '@primer/react'
59
+
60
+ const Component = () => <Box sx={{padding: 2}}>Content</Box>
61
+ ```
62
+
63
+ ```jsx
64
+ import {sx} from '@primer/react'
65
+ ```
66
+
67
+ ```jsx
68
+ import {Button} from '@primer/styled-react'
69
+
70
+ const Component = () => <Button>Click me</Button>
71
+ ```
72
+
73
+ ```jsx
74
+ import {Button} from '@primer/react'
75
+
76
+ const Component1 = () => <Button>Click me</Button>
77
+ const Component2 = () => <Button sx={{color: 'red'}}>Styled me</Button>
78
+ ```
79
+
80
+ ### ✅ Correct
81
+
82
+ ```jsx
83
+ import {Link} from '@primer/react'
84
+ import {Button} from '@primer/styled-react'
85
+
86
+ const Component = () => <Button sx={{color: 'red'}}>Click me</Button>
87
+ ```
88
+
89
+ ```jsx
90
+ import {Box} from '@primer/styled-react'
91
+
92
+ const Component = () => <Box sx={{padding: 2}}>Content</Box>
93
+ ```
94
+
95
+ ```jsx
96
+ import {sx} from '@primer/styled-react'
97
+ ```
98
+
99
+ ```jsx
100
+ // Components without sx prop can stay in @primer/react
101
+ import {Button} from '@primer/react'
102
+
103
+ const Component = () => <Button>Click me</Button>
104
+ ```
105
+
106
+ ```jsx
107
+ // Components imported from styled-react but used without sx prop should be moved back
108
+ import {Button} from '@primer/react'
109
+
110
+ const Component = () => <Button>Click me</Button>
111
+ ```
112
+
113
+ ```jsx
114
+ // When a component is used both ways, use an alias for the styled version
115
+ import {Button} from '@primer/react'
116
+ import {Button as StyledButton} from '@primer/styled-react'
117
+
118
+ const Component1 = () => <Button>Click me</Button>
119
+ const Component2 = () => <StyledButton sx={{color: 'red'}}>Styled me</StyledButton>
120
+ ```
121
+
122
+ ## Options
123
+
124
+ This rule has no options.
125
+
126
+ ## When Not To Use It
127
+
128
+ This rule is specifically for migrating components that use the `sx` prop to the temporary `@primer/styled-react` package. If you're not using the `sx` prop or not participating in this migration, you can disable this rule.
@@ -0,0 +1,54 @@
1
+ 'use strict'
2
+
3
+ const js = require('@eslint/js')
4
+ const globals = require('globals')
5
+ const github = require('eslint-plugin-github')
6
+
7
+ /**
8
+ * @type {import('eslint').Linter.FlatConfig[]}
9
+ */
10
+ module.exports = [
11
+ {
12
+ ignores: ['node_modules/**', '.git/**'],
13
+ },
14
+ js.configs.recommended,
15
+ github.default.getFlatConfigs().recommended,
16
+ {
17
+ languageOptions: {
18
+ ecmaVersion: 'latest',
19
+ sourceType: 'commonjs',
20
+ globals: {
21
+ ...globals.commonjs,
22
+ ...globals.node,
23
+ },
24
+ },
25
+ rules: {
26
+ // Override specific rules for the repository
27
+ 'import/no-commonjs': 'off',
28
+ 'import/no-dynamic-require': 'off', // Allow dynamic requires in tests
29
+ 'no-shadow': 'off',
30
+ 'no-unused-vars': [
31
+ 'error',
32
+ {
33
+ varsIgnorePattern: '^_',
34
+ },
35
+ ],
36
+ 'github/filenames-match-regex': 'off', // Allow various file naming patterns
37
+ 'i18n-text/no-en': 'off', // Allow English text in this repository
38
+ },
39
+ },
40
+ {
41
+ files: ['**/*.test.js'],
42
+ languageOptions: {
43
+ globals: {
44
+ ...globals.jest,
45
+ },
46
+ },
47
+ },
48
+ {
49
+ files: ['eslint.config.js'],
50
+ rules: {
51
+ 'no-undef': 'off', // Allow require() in config file
52
+ },
53
+ },
54
+ ]