@@ -340,64 +340,31 @@ module.exports = {
340
340
} ,
341
341
* fix ( fixer ) {
342
342
for ( const [ entrypoint , importSpecifiers ] of changes ) {
343
- const typeSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
344
- return type === 'type '
343
+ const namedSpecifiers = importSpecifiers . filter ( ( [ imported ] ) => {
344
+ return imported !== 'default '
345
345
} )
346
-
347
- // If all imports are type imports, emit emit as `import type {specifier} from '...'`
348
- if ( typeSpecifiers . length === importSpecifiers . length ) {
349
- const namedSpecifiers = typeSpecifiers . filter ( ( [ imported ] ) => {
350
- return imported !== 'default'
351
- } )
352
- const defaultSpecifier = typeSpecifiers . find ( ( [ imported ] ) => {
353
- return imported === 'default'
354
- } )
355
-
356
- if ( namedSpecifiers . length > 0 && ! defaultSpecifier ) {
357
- const specifiers = namedSpecifiers . map ( ( [ imported , local ] ) => {
358
- if ( imported !== local ) {
359
- return `${ imported } as ${ local } `
360
- }
361
- return imported
362
- } )
363
- yield fixer . replaceText ( node , `import type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
364
- } else if ( namedSpecifiers . length > 0 && defaultSpecifier ) {
365
- yield fixer . replaceText (
366
- node ,
367
- `import type ${ defaultSpecifier [ 1 ] } , ${ specifiers . join ( ', ' ) } from '${ entrypoint } '` ,
368
- )
369
- } else if ( defaultSpecifier && namedSpecifiers . length === 0 ) {
370
- yield fixer . replaceText ( node , `import type ${ defaultSpecifier [ 1 ] } from '${ entrypoint } '` )
371
- }
372
-
373
- return
374
- }
375
-
376
- // Otherwise, we have a mix of type and value imports to emit
377
- const valueSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
378
- return type !== 'type'
346
+ const defaultSpecifier = importSpecifiers . find ( ( [ imported ] ) => {
347
+ return imported === 'default'
379
348
} )
380
-
381
- if ( valueSpecifiers . length === 0 ) {
382
- return
383
- }
384
-
385
- const specifiers = valueSpecifiers . map ( ( [ imported , local ] ) => {
349
+ const specifiers = namedSpecifiers . map ( ( [ imported , local , type ] ) => {
350
+ const prefix = type === 'type' ? 'type ' : ''
386
351
if ( imported !== local ) {
387
- return `${ imported } as ${ local } `
352
+ return `${ prefix } ${ imported } as ${ local } `
388
353
}
389
- return imported
354
+ return ` ${ prefix } ${ imported } `
390
355
} )
391
- yield fixer . replaceText ( node , `import {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
392
356
393
- if ( typeSpecifiers . length > 0 ) {
394
- const specifiers = typeSpecifiers . map ( ( [ imported , local ] ) => {
395
- if ( imported !== local ) {
396
- return `${ imported } as ${ local } `
397
- }
398
- return imported
399
- } )
400
- yield fixer . insertTextAfter ( node , `\nimport type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
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 } '` )
401
368
}
402
369
}
403
370
} ,
0 commit comments