Skip to content

Commit 7aea664

Browse files
ertrzyiksljharb
authored andcommitted
[Fix] no-duplicates: ensure autofix avoids excessive newlines
Fixes #2027
1 parent 998c300 commit 7aea664

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
66

77
## [Unreleased]
88

9+
### Fixed
10+
- [`no-duplicates`]: ensure autofix avoids excessive newlines ([#2028], thanks [@ertrzyiks])
11+
912
## [2.23.4] - 2021-05-29
1013

1114
### Fixed
@@ -808,6 +811,7 @@ for info on changes for earlier releases.
808811
[#2075]: https://github.com/benmosher/eslint-plugin-import/pull/2075
809812
[#2071]: https://github.com/benmosher/eslint-plugin-import/pull/2071
810813
[#2034]: https://github.com/benmosher/eslint-plugin-import/pull/2034
814+
[#2028]: https://github.com/benmosher/eslint-plugin-import/pull/2028
811815
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
812816
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
813817
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
@@ -1273,6 +1277,7 @@ for info on changes for earlier releases.
12731277
[@ephys]: https://github.com/ephys
12741278
[@eps1lon]: https://github.com/eps1lon
12751279
[@ernestostifano]: https://github.com/ernestostifano
1280+
[@ertrzyiks]: https://github.com/ertrzyiks
12761281
[@fa93hws]: https://github.com/fa93hws
12771282
[@fengkfengk]: https://github.com/fengkfengk
12781283
[@fernandopasik]: https://github.com/fernandopasik
@@ -1412,4 +1417,4 @@ for info on changes for earlier releases.
14121417
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
14131418
[@xpl]: https://github.com/xpl
14141419
[@yordis]: https://github.com/yordis
1415-
[@zloirock]: https://github.com/zloirock
1420+
[@zloirock]: https://github.com/zloirock

src/rules/no-duplicates.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,27 @@ function getFix(first, rest, sourceCode) {
150150

151151
// Remove imports whose specifiers have been moved into the first import.
152152
for (const specifier of specifiers) {
153-
fixes.push(fixer.remove(specifier.importNode));
153+
const importNode = specifier.importNode;
154+
fixes.push(fixer.remove(importNode));
155+
156+
const charAfterImportRange = [importNode.range[1], importNode.range[1] + 1];
157+
const charAfterImport = sourceCode.text.substring(charAfterImportRange[0], charAfterImportRange[1]);
158+
if (charAfterImport === '\n') {
159+
fixes.push(fixer.removeRange(charAfterImportRange));
160+
}
154161
}
155162

156163
// Remove imports whose default import has been moved to the first import,
157164
// and side-effect-only imports that are unnecessary due to the first
158165
// import.
159166
for (const node of unnecessaryImports) {
160167
fixes.push(fixer.remove(node));
168+
169+
const charAfterImportRange = [node.range[1], node.range[1] + 1];
170+
const charAfterImport = sourceCode.text.substring(charAfterImportRange[0], charAfterImportRange[1]);
171+
if (charAfterImport === '\n') {
172+
fixes.push(fixer.removeRange(charAfterImportRange));
173+
}
161174
}
162175

163176
return fixes;

tests/src/rules/no-duplicates.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -302,32 +302,30 @@ ruleTester.run('no-duplicates', rule, {
302302

303303
test({
304304
code: `
305-
import {x} from './foo'
306-
import {y} from './foo'
307-
// some-tool-disable-next-line
305+
import {x} from './foo'
306+
import {y} from './foo'
307+
// some-tool-disable-next-line
308308
`,
309309
// Not autofix bail.
310310
output: `
311-
import {x,y} from './foo'
312-
313-
// some-tool-disable-next-line
311+
import {x,y} from './foo'
312+
// some-tool-disable-next-line
314313
`,
315314
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
316315
}),
317316

318317
test({
319318
code: `
320-
import {x} from './foo'
321-
// comment
319+
import {x} from './foo'
320+
// comment
322321
323-
import {y} from './foo'
322+
import {y} from './foo'
324323
`,
325324
// Not autofix bail.
326325
output: `
327-
import {x,y} from './foo'
328-
// comment
326+
import {x,y} from './foo'
327+
// comment
329328
330-
331329
`,
332330
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
333331
}),
@@ -400,6 +398,20 @@ ruleTester.run('no-duplicates', rule, {
400398
`,
401399
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
402400
}),
401+
402+
// #2027 long import list generate empty lines
403+
test({
404+
code: "import { Foo } from './foo';\nimport { Bar } from './foo';\nexport const value = {}",
405+
output: "import { Foo , Bar } from './foo';\nexport const value = {}",
406+
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
407+
}),
408+
409+
// #2027 long import list generate empty lines
410+
test({
411+
code: "import { Foo } from './foo';\nimport Bar from './foo';\nexport const value = {}",
412+
output: "import Bar, { Foo } from './foo';\nexport const value = {}",
413+
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
414+
}),
403415
],
404416
});
405417

@@ -430,4 +442,3 @@ context('TypeScript', function() {
430442
});
431443
});
432444
});
433-

0 commit comments

Comments
 (0)