diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index 7042cec403..bae7ed71ca 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -171,7 +171,15 @@ function getFix(first, rest, sourceCode, context) { // Add *only* the new identifiers that don't already exist, and track any new identifiers so we don't add them again in the next loop const [specifierText, updatedExistingIdentifiers] = specifier.identifiers.reduce(([text, set], cur) => { const trimmed = cur.trim(); // Trim whitespace before/after to compare to our set of existing identifiers - const curWithType = trimmed.length > 0 && preferInline && isTypeSpecifier ? `type ${cur}` : cur; + const hasLineComment = (/\/\/[^\n]*$/).test(trimmed); + let curWithType; + if (trimmed.length > 0 && preferInline && isTypeSpecifier) { + curWithType = `type ${trimmed}`; + } else if (hasLineComment && trimmed.length > 0) { + curWithType = `${trimmed}\n`; + } else { + curWithType = trimmed; + } if (existingIdentifiers.has(trimmed)) { return [text, set]; } diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index a7b2ae86bd..fbc05a0c70 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -53,20 +53,20 @@ ruleTester.run('no-duplicates', rule, { invalid: [ test({ code: "import { x } from './foo'; import { y } from './foo'", - output: "import { x , y } from './foo'; ", + output: "import { x ,y} from './foo'; ", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), test({ code: "import {x} from './foo'; import {y} from './foo'; import { z } from './foo'", - output: "import {x,y, z } from './foo'; ", + output: "import {x,y,z} from './foo'; ", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), // ensure resolved path results in warnings test({ code: "import { x } from './bar'; import { y } from 'bar';", - output: "import { x , y } from './bar'; ", + output: "import { x ,y} from './bar'; ", settings: { 'import/resolve': { paths: [path.join(process.cwd(), 'tests', 'files')], @@ -107,7 +107,7 @@ ruleTester.run('no-duplicates', rule, { test({ code: "import type { x } from './foo'; import type { y } from './foo'", - output: "import type { x , y } from './foo'; ", + output: "import type { x ,y} from './foo'; ", parser: parsers.BABEL_OLD, errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), @@ -145,7 +145,7 @@ ruleTester.run('no-duplicates', rule, { // #2347: duplicate identifiers should be removed test({ code: "import {a,b} from './foo'; import { b, c } from './foo'; import {b,c,d} from './foo'", - output: "import {a,b, c ,d} from './foo'; ", + output: "import {a,b,c,d} from './foo'; ", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], parser, }), @@ -153,7 +153,7 @@ ruleTester.run('no-duplicates', rule, { // #2347: duplicate identifiers should be removed, but not if they are adjacent to comments test({ code: "import {a} from './foo'; import { a/*,b*/ } from './foo'", - output: "import {a, a/*,b*/ } from './foo'; ", + output: "import {a,a/*,b*/} from './foo'; ", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], parser, }), @@ -186,7 +186,8 @@ ruleTester.run('no-duplicates', rule, { test({ code: "import './foo'; import { /*x*/} from './foo'; import {//y\n} from './foo'; import {z} from './foo'", - output: "import { /*x*///y\nz} from './foo'; ", + output: "import {/*x*///y\nz} from './foo'; ", + errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), @@ -373,7 +374,7 @@ import {x,y} from './foo' // #2027 long import list generate empty lines test({ code: "import { Foo } from './foo';\nimport { Bar } from './foo';\nexport const value = {}", - output: "import { Foo , Bar } from './foo';\nexport const value = {}", + output: "import { Foo ,Bar} from './foo';\nexport const value = {}", errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), @@ -402,22 +403,7 @@ import {x,y} from './foo' ${''} export default TestComponent; `, - output: ` - import { - DEFAULT_FILTER_KEYS, - BULK_DISABLED, - ${''} - BULK_ACTIONS_ENABLED - } from '../constants'; - import React from 'react'; - ${''} - const TestComponent = () => { - return