@@ -364,28 +364,13 @@ module.exports = {
364
364
node . type === 'ImportDeclaration' && node . source . value === entrypoint && node . importKind === 'type'
365
365
)
366
366
} )
367
- const namedSpecifiers = importSpecifiers
368
- . filter ( ( [ imported , , type ] ) => {
369
- return imported !== 'default' && type !== 'type'
370
- } )
371
- . map ( ( [ imported , local , type ] ) => {
372
- const prefix = type === 'type' ? 'type ' : ''
373
- if ( imported !== local ) {
374
- return `${ prefix } ${ imported } as ${ local } `
375
- }
376
- return `${ prefix } ${ imported } `
377
- } )
378
- const namedTypeSpecifiers = importSpecifiers
379
- . filter ( ( [ imported , , type ] ) => {
380
- return imported !== 'default' && type === 'type'
381
- } )
382
- . map ( ( [ imported , local , type ] ) => {
383
- const prefix = type === 'type' ? 'type ' : ''
384
- if ( imported !== local ) {
385
- return `${ prefix } ${ imported } as ${ local } `
386
- }
387
- return `${ prefix } ${ imported } `
388
- } )
367
+ let originalImportReplaced = false
368
+ const namedSpecifiers = importSpecifiers . filter ( ( [ imported , , type ] ) => {
369
+ return imported !== 'default' && type !== 'type'
370
+ } )
371
+ const namedTypeSpecifiers = importSpecifiers . filter ( ( [ imported , , type ] ) => {
372
+ return imported !== 'default' && type === 'type'
373
+ } )
389
374
let defaultSpecifier = importSpecifiers . find ( ( [ imported , , type ] ) => {
390
375
return imported === 'default' && type !== 'type'
391
376
} )
@@ -399,10 +384,6 @@ module.exports = {
399
384
defaultTypeSpecifier = `type ${ defaultTypeSpecifier [ 1 ] } `
400
385
}
401
386
402
- if ( typeImportDeclaration || importDeclaration ) {
403
- yield fixer . remove ( node )
404
- }
405
-
406
387
// Reuse a type import if it exists
407
388
if ( typeImportDeclaration ) {
408
389
const firstSpecifier = typeImportDeclaration . specifiers [ 0 ]
@@ -412,13 +393,19 @@ module.exports = {
412
393
const postfix =
413
394
namedTypeSpecifiers . length > 0 || typeImportDeclaration . specifiers . length > 0 ? ', ' : ' '
414
395
yield fixer . insertTextBeforeRange (
415
- [ firstSpecifier . range [ 0 ] - 1 , firstSpecifier . range [ 1 ] ] ,
396
+ [ firstSpecifier . range [ 0 ] - 2 , firstSpecifier . range [ 1 ] ] ,
416
397
`${ defaultTypeSpecifier } ${ postfix } ` ,
417
398
)
418
399
}
419
400
420
401
if ( namedTypeSpecifiers . length > 0 ) {
421
- yield fixer . insertTextAfter ( lastSpecifier , `, ${ namedTypeSpecifiers . join ( ', ' ) } ` )
402
+ const specifiers = namedTypeSpecifiers . map ( ( [ imported , local ] ) => {
403
+ if ( imported !== local ) {
404
+ return `${ imported } as ${ local } `
405
+ }
406
+ return imported
407
+ } )
408
+ yield fixer . insertTextAfter ( lastSpecifier , `, ${ specifiers . join ( ', ' ) } ` )
422
409
}
423
410
}
424
411
@@ -430,23 +417,37 @@ module.exports = {
430
417
if ( defaultSpecifier ) {
431
418
const postfix = namedSpecifiers . length > 0 || importDeclaration . specifiers . length > 0 ? ', ' : ' '
432
419
yield fixer . insertTextBeforeRange (
433
- [ firstSpecifier . range [ 0 ] - 1 , firstSpecifier . range [ 1 ] ] ,
420
+ [ firstSpecifier . range [ 0 ] - 2 , firstSpecifier . range [ 1 ] ] ,
434
421
`${ defaultSpecifier } ${ postfix } ` ,
435
422
)
436
423
}
437
424
438
425
if ( namedSpecifiers . length > 0 || ( ! typeImportDeclaration && namedTypeSpecifiers . length > 0 ) ) {
439
- const specifiers = [ ...namedSpecifiers ]
426
+ let specifiers = [ ...namedSpecifiers ]
440
427
if ( ! typeImportDeclaration ) {
441
428
specifiers . push ( ...namedTypeSpecifiers )
442
429
}
430
+ specifiers = specifiers . map ( ( [ imported , local , type ] ) => {
431
+ const prefix = type === 'type' ? 'type ' : ''
432
+ if ( imported !== local ) {
433
+ return `${ prefix } ${ imported } as ${ local } `
434
+ }
435
+ return `${ prefix } ${ imported } `
436
+ } )
443
437
yield fixer . insertTextAfter ( lastSpecifier , `, ${ specifiers . join ( ', ' ) } ` )
444
438
}
445
439
} else {
446
- const specifiers = [ ...namedSpecifiers ]
440
+ let specifiers = [ ...namedSpecifiers ]
447
441
if ( ! typeImportDeclaration ) {
448
442
specifiers . push ( ...namedTypeSpecifiers )
449
443
}
444
+ specifiers = specifiers . map ( ( [ imported , local , type ] ) => {
445
+ const prefix = type === 'type' ? 'type ' : ''
446
+ if ( imported !== local ) {
447
+ return `${ prefix } ${ imported } as ${ local } `
448
+ }
449
+ return `${ prefix } ${ imported } `
450
+ } )
450
451
let declaration = 'import '
451
452
452
453
if ( defaultSpecifier ) {
@@ -466,6 +467,11 @@ module.exports = {
466
467
467
468
declaration += ` from '${ entrypoint } '`
468
469
yield fixer . replaceText ( node , declaration )
470
+ originalImportReplaced = true
471
+ }
472
+
473
+ if ( ! originalImportReplaced ) {
474
+ yield fixer . remove ( node )
469
475
}
470
476
}
471
477
} ,
0 commit comments