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 committed Feb 21, 2020
1 parent 12971f5 commit 9eb6d7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/rules/no-duplicates.js
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
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 9eb6d7b

Please sign in to comment.