Skip to content

Commit d3b6a1d

Browse files
committed
feat: Refactor import handling in use-styled-react-import rule to consolidate styled-react imports into a single statement with aliasing support
1 parent f163375 commit d3b6a1d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ import { Button } from '@primer/react'
188188
</div>
189189
)`,
190190
output: `import { Button } from '@primer/react'
191-
import { Link } from '@primer/styled-react'
192-
import { Button as StyledButton } from '@primer/styled-react'
191+
import { Button as StyledButton, Link } from '@primer/styled-react'
193192
const Component = () => (
194193
<div>
195194
<Link sx={{ color: 'red' }} />

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,25 @@ module.exports = {
203203
fixer.replaceText(importNode, `import { ${remainingNames.join(', ')} } from '@primer/react'`),
204204
)
205205

206-
// Add new imports for moved components
207-
for (const componentName of changes.toMove) {
208-
fixes.push(
209-
fixer.insertTextAfter(importNode, `\nimport { ${componentName} } from '@primer/styled-react'`),
210-
)
211-
}
206+
// Combine all styled-react imports into a single import statement
207+
const styledReactImports = []
212208

213-
// Add new aliased imports for conflicted components
209+
// Add aliased components first
214210
for (const componentName of changes.toAlias) {
215211
const aliasName = `Styled${componentName}`
212+
styledReactImports.push(`${componentName} as ${aliasName}`)
213+
}
214+
215+
// Add moved components second
216+
for (const componentName of changes.toMove) {
217+
styledReactImports.push(componentName)
218+
}
219+
220+
if (styledReactImports.length > 0) {
216221
fixes.push(
217222
fixer.insertTextAfter(
218223
importNode,
219-
`\nimport { ${componentName} as ${aliasName} } from '@primer/styled-react'`,
224+
`\nimport { ${styledReactImports.join(', ')} } from '@primer/styled-react'`,
220225
),
221226
)
222227
}

0 commit comments

Comments
 (0)