Skip to content

Commit 8a432e8

Browse files
committed
refactor: update logic to reuse imports
1 parent f815572 commit 8a432e8

File tree

2 files changed

+84
-39
lines changed

2 files changed

+84
-39
lines changed

src/rules/__tests__/no-wildcard-imports.test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ruleTester.run('no-wildcard-imports', rule, {
3030
// Test type import
3131
{
3232
code: `import type {SxProp} from '@primer/react/lib-esm/sx'`,
33-
output: `import type {SxProp} from '@primer/react'`,
33+
output: `import {type SxProp} from '@primer/react'`,
3434
errors: [
3535
{
3636
messageId: 'wildcardMigration',
@@ -44,7 +44,7 @@ ruleTester.run('no-wildcard-imports', rule, {
4444
// Test multiple type imports
4545
{
4646
code: `import type {BetterSystemStyleObject, SxProp, BetterCssProperties} from '@primer/react/lib-esm/sx'`,
47-
output: `import type {BetterSystemStyleObject, SxProp, BetterCssProperties} from '@primer/react'`,
47+
output: `import {type BetterSystemStyleObject, type SxProp, type BetterCssProperties} from '@primer/react'`,
4848
errors: [
4949
{
5050
messageId: 'wildcardMigration',
@@ -58,7 +58,7 @@ ruleTester.run('no-wildcard-imports', rule, {
5858
// Test import alias
5959
{
6060
code: `import type {SxProp as RenamedSxProp} from '@primer/react/lib-esm/sx'`,
61-
output: `import type {SxProp as RenamedSxProp} from '@primer/react'`,
61+
output: `import {type SxProp as RenamedSxProp} from '@primer/react'`,
6262
errors: [
6363
{
6464
messageId: 'wildcardMigration',
@@ -108,7 +108,7 @@ ruleTester.run('no-wildcard-imports', rule, {
108108
// Test renamed wildcard imports
109109
{
110110
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList/Item'`,
111-
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
111+
output: `import {type ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
112112
errors: [
113113
{
114114
messageId: 'wildcardMigration',
@@ -137,7 +137,8 @@ ruleTester.run('no-wildcard-imports', rule, {
137137
{
138138
code: `import {Box, type BoxProps} from '@primer/react'
139139
import type {BetterSystemStyleObject} from '@primer/react/lib-esm/sx'`,
140-
output: `import {Box, type BoxProps, type BetterSystemStyleObject} from '@primer/react'`,
140+
output: `import {Box, type BoxProps, type BetterSystemStyleObject} from '@primer/react'
141+
`,
141142
errors: [
142143
{
143144
messageId: 'wildcardMigration',
@@ -169,7 +170,7 @@ import type {BetterSystemStyleObject} from '@primer/react/lib-esm/sx'`,
169170
code: `import {ButtonBase} from '@primer/react/lib-esm/Button/ButtonBase';
170171
import type {ButtonBaseProps} from '@primer/react/lib-esm/Button/ButtonBase'`,
171172
output: `import {ButtonBase} from '@primer/react'
172-
import type {ButtonBaseProps} from '@primer/react'`,
173+
import {type ButtonBaseProps} from '@primer/react'`,
173174
errors: [
174175
{
175176
messageId: 'wildcardMigration',
@@ -187,7 +188,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
187188
},
188189
{
189190
code: `import type {ButtonBaseProps} from '@primer/react/lib-esm/Button/types'`,
190-
output: `import type {ButtonBaseProps} from '@primer/react'`,
191+
output: `import {type ButtonBaseProps} from '@primer/react'`,
191192
errors: [
192193
{
193194
messageId: 'wildcardMigration',
@@ -223,7 +224,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
223224
},
224225
{
225226
code: `import type {SelectPanelProps} from '@primer/react/lib-esm/SelectPanel/SelectPanel'`,
226-
output: `import type {SelectPanelProps} from '@primer/react'`,
227+
output: `import {type SelectPanelProps} from '@primer/react'`,
227228
errors: [
228229
{
229230
messageId: 'wildcardMigration',
@@ -235,7 +236,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
235236
},
236237
{
237238
code: `import type {LabelColorOptions} from '@primer/react/lib-esm/Label/Label'`,
238-
output: `import type {LabelColorOptions} from '@primer/react'`,
239+
output: `import {type LabelColorOptions} from '@primer/react'`,
239240
errors: [
240241
{
241242
messageId: 'wildcardMigration',
@@ -259,7 +260,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
259260
},
260261
{
261262
code: `import type {IssueLabelTokenProps} from '@primer/react/lib-esm/Token/IssueLabelToken'`,
262-
output: `import type {IssueLabelTokenProps} from '@primer/react'`,
263+
output: `import {type IssueLabelTokenProps} from '@primer/react'`,
263264
errors: [
264265
{
265266
messageId: 'wildcardMigration',
@@ -271,7 +272,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
271272
},
272273
{
273274
code: `import type {TokenSizeKeys} from '@primer/react/lib-esm/Token/TokenBase'`,
274-
output: `import type {TokenSizeKeys} from '@primer/react'`,
275+
output: `import {type TokenSizeKeys} from '@primer/react'`,
275276
errors: [
276277
{
277278
messageId: 'wildcardMigration',
@@ -283,7 +284,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
283284
},
284285
{
285286
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList'`,
286-
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
287+
output: `import {type ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
287288
errors: [
288289
{
289290
messageId: 'wildcardMigration',
@@ -295,7 +296,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
295296
},
296297
{
297298
code: `import type {GroupedListProps} from '@primer/react/lib-esm/deprecated/ActionList/List'`,
298-
output: `import type {ActionListGroupedListProps as GroupedListProps} from '@primer/react/deprecated'`,
299+
output: `import {type ActionListGroupedListProps as GroupedListProps} from '@primer/react/deprecated'`,
299300
errors: [
300301
{
301302
messageId: 'wildcardMigration',
@@ -319,7 +320,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
319320
},
320321
{
321322
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList/Item'`,
322-
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
323+
output: `import {type ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
323324
errors: [
324325
{
325326
messageId: 'wildcardMigration',
@@ -389,7 +390,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
389390
},
390391
{
391392
code: `import type {ResponsiveValue} from '@primer/react/lib-esm/hooks/useResponsiveValue'`,
392-
output: `import type {ResponsiveValue} from '@primer/react'`,
393+
output: `import {type ResponsiveValue} from '@primer/react'`,
393394
errors: [
394395
{
395396
messageId: 'wildcardMigration',
@@ -405,7 +406,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
405406
// @primer/react/lib-esm/sx
406407
{
407408
code: `import type {BetterSystemStyleObject, SxProp, BetterCssProperties} from '@primer/react/lib-esm/sx'`,
408-
output: `import type {BetterSystemStyleObject, SxProp, BetterCssProperties} from '@primer/react'`,
409+
output: `import {type BetterSystemStyleObject, type SxProp, type BetterCssProperties} from '@primer/react'`,
409410
errors: [
410411
{
411412
messageId: 'wildcardMigration',
@@ -428,7 +429,5 @@ import type {ButtonBaseProps} from '@primer/react'`,
428429
},
429430
],
430431
},
431-
].filter((item, index) => {
432-
return index === 7
433-
}),
432+
],
434433
})

src/rules/no-wildcard-imports.js

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ module.exports = {
265265
create(context) {
266266
return {
267267
ImportDeclaration(node) {
268+
if (node.source.value === '@primer/react/lib-esm/utils/test-helpers') {
269+
context.report({
270+
node,
271+
messageId: 'wildcardMigration',
272+
data: {
273+
wildcardEntrypoint: node.source.value,
274+
},
275+
fix(fixer) {
276+
return fixer.replaceText(node.source, `'@primer/react/test-helpers'`)
277+
},
278+
})
279+
return
280+
}
281+
268282
if (!node.source.value.startsWith('@primer/react/lib-esm')) {
269283
return
270284
}
@@ -340,31 +354,63 @@ module.exports = {
340354
},
341355
*fix(fixer) {
342356
for (const [entrypoint, importSpecifiers] of changes) {
343-
const namedSpecifiers = importSpecifiers.filter(([imported]) => {
344-
return imported !== 'default'
357+
const importDeclaration = node.parent.body.find(node => {
358+
return node.type === 'ImportDeclaration' && node.source.value === entrypoint
345359
})
346-
const defaultSpecifier = importSpecifiers.find(([imported]) => {
360+
const namedSpecifiers = importSpecifiers
361+
.filter(([imported]) => {
362+
return imported !== 'default'
363+
})
364+
.map(([imported, local, type]) => {
365+
const prefix = type === 'type' ? 'type ' : ''
366+
if (imported !== local) {
367+
return `${prefix}${imported} as ${local}`
368+
}
369+
return `${prefix}${imported}`
370+
})
371+
let defaultSpecifier = importSpecifiers.find(([imported]) => {
347372
return imported === 'default'
348373
})
349-
const specifiers = namedSpecifiers.map(([imported, local, type]) => {
350-
const prefix = type === 'type' ? 'type ' : ''
351-
if (imported !== local) {
352-
return `${prefix}${imported} as ${local}`
374+
if (defaultSpecifier) {
375+
const prefix = defaultSpecifier[2] === 'type' ? 'type ' : ''
376+
defaultSpecifier = `${prefix}${defaultSpecifier[1]}`
377+
}
378+
379+
const hasNamedSpecifiers = namedSpecifiers.length > 0
380+
const hasDefaultSpecifier = !!defaultSpecifier
381+
382+
if (importDeclaration) {
383+
yield fixer.remove(node)
384+
385+
if (importDeclaration.specifiers.length === 0) {
386+
throw new Error('Import Declaration has no specifiers')
387+
}
388+
389+
let lastSpecifier = importDeclaration.specifiers[importDeclaration.specifiers.length - 1]
390+
391+
if (hasDefaultSpecifier) {
392+
console.log('wat')
393+
}
394+
395+
if (hasNamedSpecifiers) {
396+
yield fixer.insertTextAfter(lastSpecifier, `, ${namedSpecifiers.join(', ')}`)
397+
}
398+
} else {
399+
let declaration = 'import '
400+
401+
if (hasDefaultSpecifier) {
402+
declaration += defaultSpecifier
403+
}
404+
405+
if (hasNamedSpecifiers) {
406+
if (hasDefaultSpecifier) {
407+
declaration += ', '
408+
}
409+
declaration += `{${namedSpecifiers.join(', ')}}`
353410
}
354-
return `${prefix}${imported}`
355-
})
356411

357-
if (namedSpecifiers.length > 0 && !defaultSpecifier) {
358-
yield fixer.replaceText(node, `import {${specifiers.join(', ')}} from '${entrypoint}'`)
359-
} else if (namedSpecifiers.length > 0 && defaultSpecifier) {
360-
const prefix = defaultSpecifier[2].type === 'type' ? 'type ' : ''
361-
yield fixer.replaceText(
362-
node,
363-
`import ${prefix}${defaultSpecifier[1]}, {${specifiers.join(', ')}} from '${entrypoint}'`,
364-
)
365-
} else if (defaultSpecifier && namedSpecifiers.length === 0) {
366-
const prefix = defaultSpecifier[2].type === 'type' ? 'type ' : ''
367-
yield fixer.replaceText(node, `import ${prefix}${defaultSpecifier[1]} from '${entrypoint}'`)
412+
declaration += ` from '${entrypoint}'`
413+
yield fixer.replaceText(node, declaration)
368414
}
369415
}
370416
},

0 commit comments

Comments
 (0)