Skip to content

Commit

Permalink
fix(no-wildcard-imports): support test-helpers, mix-and-match imports (
Browse files Browse the repository at this point in the history
…#242)

* fix(no-wildcard-imports): add test case for mix-and-match

* chore: add changeset

* feat: add support for ResponsiveValue

* feat: add support for useFeatureFlag migration
  • Loading branch information
joshblack authored Oct 7, 2024
1 parent c860192 commit e139c02
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pink-feet-provide.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 work for imports that specify value and type imports
41 changes: 41 additions & 0 deletions src/rules/__tests__/no-wildcard-imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,37 @@ ruleTester.run('no-wildcard-imports', rule, {
],
},

// Test mixed imports
{
code: `import {Dialog, type DialogProps} from '@primer/react/lib-esm/Dialog/Dialog'`,
output: `import {Dialog} from '@primer/react/experimental'
import type {DialogProps} from '@primer/react/experimental'`,
errors: [
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/Dialog/Dialog',
},
},
],
},

// Test migrations

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

// Components --------------------------------------------------------------
{
code: `import {ButtonBase} from '@primer/react/lib-esm/Button/ButtonBase';
Expand Down Expand Up @@ -344,6 +373,18 @@ import type {ButtonBaseProps} from '@primer/react'`,
},
],
},
{
code: `import type {ResponsiveValue} from '@primer/react/lib-esm/hooks/useResponsiveValue'`,
output: `import type {ResponsiveValue} from '@primer/react'`,
errors: [
{
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: '@primer/react/lib-esm/hooks/useResponsiveValue',
},
},
],
},

// Utilities ---------------------------------------------------------------

Expand Down
40 changes: 39 additions & 1 deletion src/rules/no-wildcard-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const wildcardImports = new Map([
from: '@primer/react/experimental',
type: 'type',
},
{
name: 'DialogProps',
from: '@primer/react/experimental',
type: 'type',
},
{
name: 'DialogButtonProps',
from: '@primer/react/experimental',
type: 'type',
},
],
],
[
Expand Down Expand Up @@ -171,6 +181,11 @@ const wildcardImports = new Map([
name: 'useResponsiveValue',
from: '@primer/react',
},
{
type: 'type',
name: 'ResponsiveValue',
from: '@primer/react',
},
],
],

Expand Down Expand Up @@ -204,6 +219,15 @@ const wildcardImports = new Map([
},
],
],
[
'@primer/react/lib-esm/FeatureFlags/useFeatureFlag',
[
{
name: 'useFeatureFlag',
from: '@primer/react/experimental',
},
],
],
[
'@primer/react/lib-esm/theme',
[
Expand Down Expand Up @@ -245,6 +269,20 @@ module.exports = {
return
}

if (node.source.value === '@primer/react/lib-esm/utils/test-helpers') {
context.report({
node,
messageId: 'wildcardMigration',
data: {
wildcardEntrypoint: node.source.value,
},
fix(fixer) {
return fixer.replaceText(node.source, `'@primer/react/test-helpers'`)
},
})
return
}

const wildcardImportMigrations = wildcardImports.get(node.source.value)
if (!wildcardImportMigrations) {
context.report({
Expand Down Expand Up @@ -353,7 +391,7 @@ module.exports = {
yield fixer.replaceText(node, `import {${specifiers.join(', ')}} from '${entrypoint}'`)

if (typeSpecifiers.length > 0) {
const specifiers = valueSpecifiers.map(([imported, local]) => {
const specifiers = typeSpecifiers.map(([imported, local]) => {
if (imported !== local) {
return `${imported} as ${local}`
}
Expand Down

0 comments on commit e139c02

Please sign in to comment.