From b6242b02548037223720657db4597f46b534dc87 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Fri, 21 Feb 2020 12:18:22 +0800 Subject: [PATCH] [fix] `no-duplicates`: fix fixer on cases with default import --- CHANGELOG.md | 2 ++ src/rules/no-duplicates.js | 9 +++++++-- tests/src/rules/no-duplicates.js | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 387605797..e8fbdb5f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`order`]: fix `isExternalModule` detect on windows ([#1651], thanks [@fisker]) - [`order`]: recognize ".." as a "parent" path ([#1658], thanks [@golopot]) +- [`no-duplicates`]: fix fixer on cases with default import ([#1666], thanks [@golopot]) ## [2.20.1] - 2020-02-01 ### Fixed @@ -655,6 +656,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#1666]: https://github.com/benmosher/eslint-plugin-import/pull/1666 [#1658]: https://github.com/benmosher/eslint-plugin-import/pull/1658 [#1651]: https://github.com/benmosher/eslint-plugin-import/pull/1651 [#1635]: https://github.com/benmosher/eslint-plugin-import/issues/1635 diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index 69e5a23a0..ce586cd67 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -136,8 +136,13 @@ function getFix(first, rest, sourceCode) { fixes.push(fixer.insertTextBefore(closeBrace, specifiersText)) } } else if (!shouldAddDefault && openBrace == null && shouldAddSpecifiers) { - // `import './foo'` → `import {...} from './foo'` - fixes.push(fixer.insertTextAfter(firstToken, ` {${specifiersText}} from`)) + if (first.specifiers.length === 0) { + // `import './foo'` → `import {...} from './foo'` + fixes.push(fixer.insertTextAfter(firstToken, ` {${specifiersText}} from`)) + } else { + // `import def from './foo'` → `import def, {...} from './foo'` + fixes.push(fixer.insertTextAfter(first.specifiers[0], `, {${specifiersText}}`)) + } } else if (!shouldAddDefault && openBrace != null && closeBrace != null) { // `import {...} './foo'` → `import {..., ...} from './foo'` fixes.push(fixer.insertTextBefore(closeBrace, specifiersText)) diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index 468c7ab98..917d0e400 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -168,6 +168,12 @@ ruleTester.run('no-duplicates', rule, { errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], }), + test({ + code: "import def from './foo'; import {x} from './foo'", + output: "import def, {x} from './foo'; ", + errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'], + }), + test({ code: "import {x} from './foo'; import def from './foo'", output: "import def, {x} from './foo'; ",