Skip to content

Commit

Permalink
[fix] no-duplicates: fix fixer on cases with default import
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot authored and ljharb committed Feb 21, 2020
1 parent 41aaa18 commit b6242b0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'; ",
Expand Down

0 comments on commit b6242b0

Please sign in to comment.