diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index c968d57c7..58263a943 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -107,22 +107,26 @@ function getInlineTypeFix(nodes, sourceCode) { const nodeClosingBrace = nodeTokens[nodeClosingBraceIndex]; const tokenBeforeClosingBrace = nodeTokens[nodeClosingBraceIndex - 1]; if (nodeClosingBrace) { - if (rest.length && isComma(tokenBeforeClosingBrace)) { - fixes.push(fixer.remove(tokenBeforeClosingBrace)); - } + const specifiers = []; rest.forEach((node) => { // these will be all Type imports, no Value specifiers // then add inline type specifiers to importKind === 'type' import node.specifiers.forEach((specifier) => { if (specifier.importKind === 'type') { - fixes.push(fixer.insertTextBefore(nodeClosingBrace, `, type ${specifier.local.name}`)); + specifiers.push(`type ${specifier.local.name}`); } else { - fixes.push(fixer.insertTextBefore(nodeClosingBrace, `, ${specifier.local.name}`)); + specifiers.push(specifier.local.name); } }); fixes.push(fixer.remove(node)); }); + + if (isComma(tokenBeforeClosingBrace)) { + fixes.push(fixer.insertTextBefore(nodeClosingBrace, ` ${specifiers.join(', ')}`)); + } else { + fixes.push(fixer.insertTextBefore(nodeClosingBrace, `, ${specifiers.join(', ')}`)); + } } else { // we have a default import only const defaultSpecifier = firstImport.specifiers.find((spec) => spec.type === 'ImportDefaultSpecifier'); diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index 0185af00d..ee5ee248b 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -1039,7 +1039,7 @@ context('TypeScript', function () { code: "import { type C, } from './foo';import {AValue, BValue, } from './foo';", ...parserConfig, options: [{ 'prefer-inline': true }], - output: "import { type C , AValue, BValue} from './foo';", + output: "import { type C, AValue, BValue} from './foo';", errors: [ { line: 1,