Skip to content

Commit b6242b0

Browse files
golopotljharb
authored andcommitted
[fix] no-duplicates: fix fixer on cases with default import
1 parent 41aaa18 commit b6242b0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Fixed
99
- [`order`]: fix `isExternalModule` detect on windows ([#1651], thanks [@fisker])
1010
- [`order`]: recognize ".." as a "parent" path ([#1658], thanks [@golopot])
11+
- [`no-duplicates`]: fix fixer on cases with default import ([#1666], thanks [@golopot])
1112

1213
## [2.20.1] - 2020-02-01
1314
### Fixed
@@ -655,6 +656,7 @@ for info on changes for earlier releases.
655656

656657
[`memo-parser`]: ./memo-parser/README.md
657658

659+
[#1666]: https://github.com/benmosher/eslint-plugin-import/pull/1666
658660
[#1658]: https://github.com/benmosher/eslint-plugin-import/pull/1658
659661
[#1651]: https://github.com/benmosher/eslint-plugin-import/pull/1651
660662
[#1635]: https://github.com/benmosher/eslint-plugin-import/issues/1635

Diff for: src/rules/no-duplicates.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ function getFix(first, rest, sourceCode) {
136136
fixes.push(fixer.insertTextBefore(closeBrace, specifiersText))
137137
}
138138
} else if (!shouldAddDefault && openBrace == null && shouldAddSpecifiers) {
139-
// `import './foo'` → `import {...} from './foo'`
140-
fixes.push(fixer.insertTextAfter(firstToken, ` {${specifiersText}} from`))
139+
if (first.specifiers.length === 0) {
140+
// `import './foo'` → `import {...} from './foo'`
141+
fixes.push(fixer.insertTextAfter(firstToken, ` {${specifiersText}} from`))
142+
} else {
143+
// `import def from './foo'` → `import def, {...} from './foo'`
144+
fixes.push(fixer.insertTextAfter(first.specifiers[0], `, {${specifiersText}}`))
145+
}
141146
} else if (!shouldAddDefault && openBrace != null && closeBrace != null) {
142147
// `import {...} './foo'` → `import {..., ...} from './foo'`
143148
fixes.push(fixer.insertTextBefore(closeBrace, specifiersText))

Diff for: tests/src/rules/no-duplicates.js

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ ruleTester.run('no-duplicates', rule, {
168168
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
169169
}),
170170

171+
test({
172+
code: "import def from './foo'; import {x} from './foo'",
173+
output: "import def, {x} from './foo'; ",
174+
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
175+
}),
176+
171177
test({
172178
code: "import {x} from './foo'; import def from './foo'",
173179
output: "import def, {x} from './foo'; ",

0 commit comments

Comments
 (0)