Skip to content

Commit

Permalink
[Fix] no-duplicates: ensure autofix avoids excessive newlines
Browse files Browse the repository at this point in the history
Fixes #2027
  • Loading branch information
ertrzyiks authored and ljharb committed Apr 13, 2021
1 parent 7c1e8e4 commit ebb3f91
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`newline-after-import`]: respect decorator annotations ([#1985], thanks [@lilling])
- [`no-restricted-paths`]: enhance performance for zones with `except` paths ([#2022], thanks [@malykhinvi])
- [`no-unresolved`]: check import() ([#2026], thanks [@aladdin-add])
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])

### Changed
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
Expand Down Expand Up @@ -770,6 +771,7 @@ for info on changes for earlier releases.
[`memo-parser`]: ./memo-parser/README.md

[#2034]: https://github.com/benmosher/eslint-plugin-import/pull/2034
[#2028]: https://github.com/benmosher/eslint-plugin-import/pull/2028
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
Expand Down Expand Up @@ -1368,4 +1370,5 @@ for info on changes for earlier releases.
[@aladdin-add]: https://github.com/aladdin-add
[@davidbonnet]: https://github.com/davidbonnet
[@hayes]: https://github.com/hayes
[@edemaine]: https://github.com/edemaine
[@edemaine]: https://github.com/edemaine
[@ertrzyiks]: https://github.com/ertrzyiks
15 changes: 14 additions & 1 deletion src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,27 @@ function getFix(first, rest, sourceCode) {

// Remove imports whose specifiers have been moved into the first import.
for (const specifier of specifiers) {
fixes.push(fixer.remove(specifier.importNode));
const importNode = specifier.importNode;
fixes.push(fixer.remove(importNode));

const charAfterImportRange = [importNode.range[1], importNode.range[1] + 1]
const charAfterImport = sourceCode.text.substring(charAfterImportRange[0], charAfterImportRange[1]);
if (charAfterImport === '\n') {
fixes.push(fixer.removeRange(charAfterImportRange));
}
}

// Remove imports whose default import has been moved to the first import,
// and side-effect-only imports that are unnecessary due to the first
// import.
for (const node of unnecessaryImports) {
fixes.push(fixer.remove(node));

const charAfterImportRange = [node.range[1], node.range[1] + 1];
const charAfterImport = sourceCode.text.substring(charAfterImportRange[0], charAfterImportRange[1]);
if (charAfterImport === '\n') {
fixes.push(fixer.removeRange(charAfterImportRange));
}
}

return fixes;
Expand Down
16 changes: 15 additions & 1 deletion tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,21 @@ ruleTester.run('no-duplicates', rule, {
`,
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),


// #2027 long import list generate empty lines
test({
code: "import { Foo } from './foo';\nimport { Bar } from './foo';\nexport const value = {}",
output: "import { Foo , Bar } from './foo';\nexport const value = {}",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),

// #2027 long import list generate empty lines
test({
code: "import { Foo } from './foo';\nimport Bar from './foo';\nexport const value = {}",
output: "import Bar, { Foo } from './foo';\nexport const value = {}",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),
],
});

Expand Down Expand Up @@ -430,4 +445,3 @@ context('TypeScript', function() {
});
});
});

0 comments on commit ebb3f91

Please sign in to comment.