Repository: import-js/eslint-plugin-import (JavaScript)

Issue:
no-duplicates autofix causing multiple "from"s
This code fails "import/no-duplicates" rule:

``` javascript
import reducer from './glassSupplement';
import { glassDefaultState } from './glassSupplement';
```

Running an autofix turns the code into this (a syntax error):

``` javascript
import { glassDefaultState } from reducer from './glassSupplement';
```

Expected this instead:

``` javascript
import reducer, { glassDefaultState } from './glassSupplement';
```

.eslintrc.js:
``` javascript
module.exports = {
    "extends": [ "eslint:recommended", "plugin:import/errors", "plugin:import/warnings" ],
    "parser": "babel-eslint",
};
```

devDependencies:
    "babel-eslint": "^9.0.0",
    "eslint": "^5.16.0",
    "eslint-plugin-import": "^2.18.0",

autofix do not work well with named import and default import
So I have

```jsx
import Utils from '../../lib/utils'
import { selectActiveCurrencyPrice } from '../../lib/utils'
```

And if then it runs `eslint --fix` I get:

```jsx
import { selectActiveCurrencyPrice } from Utils from '../../lib/utils'
```

Thanks!
Autofix for import/no-duplicates gives wrong output
Fixers for import/no-duplicates give wrong output when both default import and named import are present.
 
**Code:**
```js
import aaa from 'foo'; // import/no-duplicates warns
import {bbb} from 'foo'; // import/no-duplicates warns
```

**Expected output:**
```js
import aaa, {bbb} from 'foo';
```

**Actual output:**
```js
import {bbb} from aaa from 'foo';
```



Discussion:


What version of eslint-plugin-import do you have?

See also, #1431 and #1403 - this seems like a duplicate of #1403?





Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3876057976..e8fbdb5f75 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 69e5a23a02..ce586cd674 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))


Test patch:
diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js
index 468c7ab982..917d0e400e 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'; ",