Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-wildcard-imports): use local name when changing imports #234

Merged
merged 10 commits into from
Sep 24, 2024
5 changes: 5 additions & 0 deletions .changeset/shy-news-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-primer-react': patch
---

Update no-wildcard-imports rule to use local name for updated imports
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 30 additions & 16 deletions src/rules/__tests__/no-wildcard-imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ ruleTester.run('no-wildcard-imports', rule, {

// Test default import
{
code: `import useIsomorphicLayoutEffect from '@primer/react/lib-esm/useIsomorphicLayoutEffect'`,
code: `import useIsomorphicLayoutEffect from '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect'`,
output: `import {useIsomorphicLayoutEffect} from '@primer/react'`,
errors: [
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/useIsomorphicLayoutEffect',
wildcardEntrypoint: '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect',
},
},
],
},

// Test multiple wildcard imports into single entrypoint
{
code: `import useResizeObserver from '@primer/react/lib-esm/hooks/useResizeObserver'
import useIsomorphicLayoutEffect from '@primer/react/lib-esm/useIsomorphicLayoutEffect'`,
code: `import {useResizeObserver} from '@primer/react/lib-esm/hooks/useResizeObserver'
import useIsomorphicLayoutEffect from '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect'`,
output: `import {useResizeObserver} from '@primer/react'
import {useIsomorphicLayoutEffect} from '@primer/react'`,
errors: [
Expand All @@ -99,7 +99,21 @@ ruleTester.run('no-wildcard-imports', rule, {
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/useIsomorphicLayoutEffect',
wildcardEntrypoint: '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect',
},
},
],
},

// Test renamed wildcard imports
{
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList/Item'`,
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
errors: [
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/deprecated/ActionList/Item',
},
},
],
Expand Down Expand Up @@ -154,7 +168,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import {SelectPanel} from '@primer/react/lib-esm/SelectPanel/SelectPanel'`,
output: `import {SelectPanel} from '@primer/react/experimental'`,
output: `import {SelectPanel} from '@primer/react'`,
errors: [
{
messageId: 'wildcardMigration',
Expand All @@ -166,7 +180,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import type {SelectPanelProps} from '@primer/react/lib-esm/SelectPanel/SelectPanel'`,
output: `import type {SelectPanelProps} from '@primer/react/experimental'`,
output: `import type {SelectPanelProps} from '@primer/react'`,
errors: [
{
messageId: 'wildcardMigration',
Expand Down Expand Up @@ -226,7 +240,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList'`,
output: `import type {ActionListItemProps} from '@primer/react/deprecated'`,
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
errors: [
{
messageId: 'wildcardMigration',
Expand All @@ -238,7 +252,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import type {GroupedListProps} from '@primer/react/lib-esm/deprecated/ActionList/List'`,
output: `import type {ActionListGroupedListProps} from '@primer/react/deprecated'`,
output: `import type {ActionListGroupedListProps as GroupedListProps} from '@primer/react/deprecated'`,
errors: [
{
messageId: 'wildcardMigration',
Expand All @@ -250,7 +264,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import {ItemInput} from '@primer/react/lib-esm/deprecated/ActionList/List'`,
output: `import {ActionListItemInput} from '@primer/react/deprecated'`,
output: `import {ActionListItemInput as ItemInput} from '@primer/react/deprecated'`,
errors: [
{
messageId: 'wildcardMigration',
Expand All @@ -262,7 +276,7 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
{
code: `import type {ItemProps} from '@primer/react/lib-esm/deprecated/ActionList/Item'`,
output: `import type {ActionListItemProps} from '@primer/react/deprecated'`,
output: `import type {ActionListItemProps as ItemProps} from '@primer/react/deprecated'`,
errors: [
{
messageId: 'wildcardMigration',
Expand All @@ -277,21 +291,21 @@ import type {ButtonBaseProps} from '@primer/react'`,

// @primer/react/lib-esm/useIsomorphicLayoutEffect
{
code: `import useIsomorphicLayoutEffect from '@primer/react/lib-esm/useIsomorphicLayoutEffect'`,
code: `import useIsomorphicLayoutEffect from '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect'`,
output: `import {useIsomorphicLayoutEffect} from '@primer/react'`,
errors: [
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/useIsomorphicLayoutEffect',
wildcardEntrypoint: '@primer/react/lib-esm/utils/useIsomorphicLayoutEffect',
},
},
],
},

// @primer/react/lib-esm/hooks/useResizeObserver
{
code: `import useResizeObserver from '@primer/react/lib-esm/hooks/useResizeObserver'`,
code: `import {useResizeObserver} from '@primer/react/lib-esm/hooks/useResizeObserver'`,
output: `import {useResizeObserver} from '@primer/react'`,
errors: [
{
Expand All @@ -305,7 +319,7 @@ import type {ButtonBaseProps} from '@primer/react'`,

// @primer/react/lib-esm/hooks/useProvidedRefOrCreate
{
code: `import useProvidedRefOrCreate from '@primer/react/lib-esm/hooks/useProvidedRefOrCreate'`,
code: `import {useProvidedRefOrCreate} from '@primer/react/lib-esm/hooks/useProvidedRefOrCreate'`,
output: `import {useProvidedRefOrCreate} from '@primer/react'`,
errors: [
{
Expand All @@ -319,7 +333,7 @@ import type {ButtonBaseProps} from '@primer/react'`,

// @primer/react/lib-esm/hooks/useResponsiveValue
{
code: `import useResponsiveValue from '@primer/react/lib-esm/hooks/useResponsiveValue'`,
code: `import {useResponsiveValue} from '@primer/react/lib-esm/hooks/useResponsiveValue'`,
output: `import {useResponsiveValue} from '@primer/react'`,
errors: [
{
Expand Down
24 changes: 13 additions & 11 deletions src/rules/no-wildcard-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ const wildcardImports = new Map([
name: 'Dialog',
from: '@primer/react/experimental',
},
{
name: 'DialogHeaderProps',
from: '@primer/react/experimental',
type: 'type',
},
],
],
[
'@primer/react/lib-esm/SelectPanel/SelectPanel',
[
{
name: 'SelectPanel',
from: '@primer/react/experimental',
from: '@primer/react',
},
{
type: 'type',
name: 'SelectPanelProps',
from: '@primer/react/experimental',
from: '@primer/react',
},
],
],
Expand Down Expand Up @@ -132,7 +137,7 @@ const wildcardImports = new Map([

// Hooks
[
'@primer/react/lib-esm/useIsomorphicLayoutEffect',
'@primer/react/lib-esm/utils/useIsomorphicLayoutEffect',
[
{
name: 'default',
Expand All @@ -145,29 +150,26 @@ const wildcardImports = new Map([
'@primer/react/lib-esm/hooks/useResizeObserver',
[
{
name: 'default',
name: 'useResizeObserver',
from: '@primer/react',
as: 'useResizeObserver',
},
],
],
[
'@primer/react/lib-esm/hooks/useProvidedRefOrCreate',
[
{
name: 'default',
name: 'useProvidedRefOrCreate',
from: '@primer/react',
as: 'useProvidedRefOrCreate',
},
],
],
[
'@primer/react/lib-esm/hooks/useResponsiveValue',
[
{
name: 'default',
name: 'useResponsiveValue',
from: '@primer/react',
as: 'useResponsiveValue',
},
],
],
Expand Down Expand Up @@ -282,7 +284,7 @@ module.exports = {
}

if (migration.as) {
changes.get(migration.from).push([migration.as, migration.as, migration.type])
changes.get(migration.from).push([migration.as, specifier.local.name, migration.type])
} else {
changes.get(migration.from).push([migration.name, specifier.local.name, migration.type])
}
Expand Down Expand Up @@ -357,7 +359,7 @@ module.exports = {
}
return imported
})
yield fixer.insertTextAfter(node, `import type {${specifiers.join(', ')}} from '${entrypoint}'`)
yield fixer.insertTextAfter(node, `\nimport type {${specifiers.join(', ')}} from '${entrypoint}'`)
}
}
},
Expand Down