Skip to content

Commit 2534e80

Browse files
authored
Fix for actionlist weird autofix (#409)
* Fix for actionlist weird autofix * Remove alias behavior * Updaet docs * Use caret for version * Downgrade
1 parent d72e8c4 commit 2534e80

File tree

5 files changed

+69
-170
lines changed

5 files changed

+69
-170
lines changed

docs/rules/use-styled-react-import.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Enforce importing components that use `sx` prop from `@primer/styled-react` inst
1212

1313
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`.
1414

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.
15+
When a component is used with the `sx` prop anywhere in the file, the entire component import is moved to `@primer/styled-react`, simplifying the import structure.
1616

1717
It also moves certain types and utilities to the styled-react package.
1818

@@ -111,12 +111,11 @@ const Component = () => <Button>Click me</Button>
111111
```
112112

113113
```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'
114+
// When a component is used with sx prop anywhere, import from styled-react
115+
import {Button} from '@primer/styled-react'
117116

118117
const Component1 = () => <Button>Click me</Button>
119-
const Component2 = () => <StyledButton sx={{color: 'red'}}>Styled me</StyledButton>
118+
const Component2 = () => <Button sx={{color: 'red'}}>Styled me</Button>
120119
```
121120

122121
## Options

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"dependencies": {
3434
"@styled-system/props": "^5.1.5",
35-
"@typescript-eslint/utils": "8.43.0",
35+
"@typescript-eslint/utils": "^8.39.0",
3636
"eslint-plugin-github": "^6.0.0",
3737
"eslint-plugin-jsx-a11y": "^6.7.1",
3838
"eslint-traverse": "^1.0.0",

src/rules/__tests__/use-styled-react-import.test.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ruleTester.run('use-styled-react-import', rule, {
7070
],
7171
},
7272

73-
// Invalid: FormControl used both with and without sx prop - should use alias
73+
// Invalid: FormControl used both with and without sx prop - should move to styled-react
7474
{
7575
code: `import { FormControl } from '@primer/react'
7676
const Component = () => (
@@ -81,24 +81,19 @@ ruleTester.run('use-styled-react-import', rule, {
8181
</FormControl>
8282
</div>
8383
)`,
84-
output: `import { FormControl } from '@primer/react'
85-
import { FormControl as StyledFormControl } from '@primer/styled-react'
84+
output: `import { FormControl } from '@primer/styled-react'
8685
const Component = () => (
8786
<div>
8887
<FormControl></FormControl>
89-
<StyledFormControl sx={{ color: 'red' }}>
90-
<StyledFormControl.Label visuallyHidden>Label</StyledFormControl.Label>
91-
</StyledFormControl>
88+
<FormControl sx={{ color: 'red' }}>
89+
<FormControl.Label visuallyHidden>Label</FormControl.Label>
90+
</FormControl>
9291
</div>
9392
)`,
9493
errors: [
9594
{
96-
messageId: 'useStyledReactImportWithAlias',
97-
data: {componentName: 'FormControl', aliasName: 'StyledFormControl'},
98-
},
99-
{
100-
messageId: 'useAliasedComponent',
101-
data: {componentName: 'FormControl', aliasName: 'StyledFormControl'},
95+
messageId: 'useStyledReactImport',
96+
data: {componentName: 'FormControl'},
10297
},
10398
],
10499
},
@@ -117,6 +112,41 @@ import { FormControl as StyledFormControl } from '@primer/styled-react'
117112
],
118113
},
119114

115+
// Invalid: ActionList used without sx, ActionList.Item used with sx - should move ActionList to styled-react
116+
{
117+
code: `import { ActionList, ActionMenu } from '@primer/react'
118+
const Component = () => (
119+
<ActionMenu>
120+
<ActionMenu.Overlay>
121+
<ActionList>
122+
<ActionList.Item sx={{ paddingLeft: 'calc(1 * var(--base-size-12))' }}>
123+
Item
124+
</ActionList.Item>
125+
</ActionList>
126+
</ActionMenu.Overlay>
127+
</ActionMenu>
128+
)`,
129+
output: `import { ActionMenu } from '@primer/react'
130+
import { ActionList } from '@primer/styled-react'
131+
const Component = () => (
132+
<ActionMenu>
133+
<ActionMenu.Overlay>
134+
<ActionList>
135+
<ActionList.Item sx={{ paddingLeft: 'calc(1 * var(--base-size-12))' }}>
136+
Item
137+
</ActionList.Item>
138+
</ActionList>
139+
</ActionMenu.Overlay>
140+
</ActionMenu>
141+
)`,
142+
errors: [
143+
{
144+
messageId: 'useStyledReactImport',
145+
data: {componentName: 'ActionList'},
146+
},
147+
],
148+
},
149+
120150
// Invalid: Multiple components, one with sx prop
121151
{
122152
code: `import { Button, Box, Avatar } from '@primer/react'
@@ -185,7 +215,7 @@ import { Button } from '@primer/styled-react'
185215
],
186216
},
187217

188-
// Invalid: <Link /> and <StyledButton /> imported from styled-react but used without sx prop
218+
// Invalid: <Link /> and <Button /> imported from styled-react but used without sx prop
189219
{
190220
code: `import { Button } from '@primer/react'
191221
import { Button as StyledButton, Link } from '@primer/styled-react'
@@ -202,7 +232,7 @@ import { Button as StyledButton, Link } from '@primer/styled-react'
202232
<div>
203233
<Link />
204234
<Button>Regular button</Button>
205-
<Button>Styled button</Button>
235+
<StyledButton>Styled button</StyledButton>
206236
</div>
207237
)`,
208238
errors: [
@@ -214,10 +244,6 @@ import { Button as StyledButton, Link } from '@primer/styled-react'
214244
messageId: 'usePrimerReactImport',
215245
data: {componentName: 'Link'},
216246
},
217-
{
218-
messageId: 'usePrimerReactImport',
219-
data: {componentName: 'Button'},
220-
},
221247
],
222248
},
223249

@@ -260,7 +286,7 @@ import { Button } from '@primer/react'
260286
],
261287
},
262288

263-
// Invalid: Button used both with and without sx prop - should use alias
289+
// Invalid: Button and Link used with sx prop - should move both to styled-react
264290
{
265291
code: `import { Button, Link } from '@primer/react'
266292
const Component = () => (
@@ -270,28 +296,23 @@ import { Button } from '@primer/react'
270296
<Button sx={{ color: 'red' }}>Styled button</Button>
271297
</div>
272298
)`,
273-
output: `import { Button } from '@primer/react'
274-
import { Button as StyledButton, Link } from '@primer/styled-react'
299+
output: `import { Link, Button } from '@primer/styled-react'
275300
const Component = () => (
276301
<div>
277302
<Link sx={{ color: 'red' }} />
278303
<Button>Regular button</Button>
279-
<StyledButton sx={{ color: 'red' }}>Styled button</StyledButton>
304+
<Button sx={{ color: 'red' }}>Styled button</Button>
280305
</div>
281306
)`,
282307
errors: [
283308
{
284-
messageId: 'useStyledReactImportWithAlias',
285-
data: {componentName: 'Button', aliasName: 'StyledButton'},
309+
messageId: 'useStyledReactImport',
310+
data: {componentName: 'Button'},
286311
},
287312
{
288313
messageId: 'useStyledReactImport',
289314
data: {componentName: 'Link'},
290315
},
291-
{
292-
messageId: 'useAliasedComponent',
293-
data: {componentName: 'Button', aliasName: 'StyledButton'},
294-
},
295316
],
296317
},
297318
],

0 commit comments

Comments
 (0)