Skip to content

Commit 61f66c5

Browse files
committed
fix: update overlap when removing and replacing node
1 parent 197bca9 commit 61f66c5

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/rules/no-wildcard-imports.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,13 @@ module.exports = {
364364
node.type === 'ImportDeclaration' && node.source.value === entrypoint && node.importKind === 'type'
365365
)
366366
})
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+
})
389374
let defaultSpecifier = importSpecifiers.find(([imported, , type]) => {
390375
return imported === 'default' && type !== 'type'
391376
})
@@ -399,10 +384,6 @@ module.exports = {
399384
defaultTypeSpecifier = `type ${defaultTypeSpecifier[1]}`
400385
}
401386

402-
if (typeImportDeclaration || importDeclaration) {
403-
yield fixer.remove(node)
404-
}
405-
406387
// Reuse a type import if it exists
407388
if (typeImportDeclaration) {
408389
const firstSpecifier = typeImportDeclaration.specifiers[0]
@@ -412,13 +393,19 @@ module.exports = {
412393
const postfix =
413394
namedTypeSpecifiers.length > 0 || typeImportDeclaration.specifiers.length > 0 ? ', ' : ' '
414395
yield fixer.insertTextBeforeRange(
415-
[firstSpecifier.range[0] - 1, firstSpecifier.range[1]],
396+
[firstSpecifier.range[0] - 2, firstSpecifier.range[1]],
416397
`${defaultTypeSpecifier}${postfix}`,
417398
)
418399
}
419400

420401
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(', ')}`)
422409
}
423410
}
424411

@@ -430,23 +417,37 @@ module.exports = {
430417
if (defaultSpecifier) {
431418
const postfix = namedSpecifiers.length > 0 || importDeclaration.specifiers.length > 0 ? ', ' : ' '
432419
yield fixer.insertTextBeforeRange(
433-
[firstSpecifier.range[0] - 1, firstSpecifier.range[1]],
420+
[firstSpecifier.range[0] - 2, firstSpecifier.range[1]],
434421
`${defaultSpecifier}${postfix}`,
435422
)
436423
}
437424

438425
if (namedSpecifiers.length > 0 || (!typeImportDeclaration && namedTypeSpecifiers.length > 0)) {
439-
const specifiers = [...namedSpecifiers]
426+
let specifiers = [...namedSpecifiers]
440427
if (!typeImportDeclaration) {
441428
specifiers.push(...namedTypeSpecifiers)
442429
}
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+
})
443437
yield fixer.insertTextAfter(lastSpecifier, `, ${specifiers.join(', ')}`)
444438
}
445439
} else {
446-
const specifiers = [...namedSpecifiers]
440+
let specifiers = [...namedSpecifiers]
447441
if (!typeImportDeclaration) {
448442
specifiers.push(...namedTypeSpecifiers)
449443
}
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+
})
450451
let declaration = 'import '
451452

452453
if (defaultSpecifier) {
@@ -466,6 +467,11 @@ module.exports = {
466467

467468
declaration += ` from '${entrypoint}'`
468469
yield fixer.replaceText(node, declaration)
470+
originalImportReplaced = true
471+
}
472+
473+
if (!originalImportReplaced) {
474+
yield fixer.remove(node)
469475
}
470476
}
471477
},

0 commit comments

Comments
 (0)