Skip to content

Commit 66cb10f

Browse files
kinlandljharb
authored andcommitted
[Fix] newline-after-import: fix exactCount with considerComments false positive, when there is a leading comment
Fixes #2882.
1 parent 8705121 commit 66cb10f

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
### Added
1010
- TypeScript config: add .cts and .mts extensions ([#2851], thanks [@Zamiell])
1111
- [`newline-after-import`]: new option `exactCount` and docs update ([#1933], thanks [@anikethsaha] and [@reosarevok])
12+
- [`newline-after-import`]: fix `exactCount` with `considerComments` false positive, when there is a leading comment ([#2884], thanks [@kinland])
1213

1314
## [2.28.1] - 2023-08-18
1415

@@ -1091,6 +1092,7 @@ for info on changes for earlier releases.
10911092

10921093
[`memo-parser`]: ./memo-parser/README.md
10931094

1095+
[#2884]: https://github.com/import-js/eslint-plugin-import/pull/2884
10941096
[#2854]: https://github.com/import-js/eslint-plugin-import/pull/2854
10951097
[#2851]: https://github.com/import-js/eslint-plugin-import/pull/2851
10961098
[#2850]: https://github.com/import-js/eslint-plugin-import/pull/2850
@@ -1769,15 +1771,16 @@ for info on changes for earlier releases.
17691771
[@kentcdodds]: https://github.com/kentcdodds
17701772
[@kevin940726]: https://github.com/kevin940726
17711773
[@kgregory]: https://github.com/kgregory
1774+
[@kinland]: https://github.com/kinland
17721775
[@kirill-konshin]: https://github.com/kirill-konshin
17731776
[@kiwka]: https://github.com/kiwka
17741777
[@klimashkin]: https://github.com/klimashkin
17751778
[@kmui2]: https://github.com/kmui2
17761779
[@knpwrs]: https://github.com/knpwrs
17771780
[@KostyaZgara]: https://github.com/KostyaZgara
17781781
[@kylemh]: https://github.com/kylemh
1779-
[@laysent]: https://github.com/laysent
17801782
[@laurens-dg]: https://github.com/laurens-dg
1783+
[@laysent]: https://github.com/laysent
17811784
[@le0nik]: https://github.com/le0nik
17821785
[@leipert]: https://github.com/leipert
17831786
[@lemonmade]: https://github.com/lemonmade

src/rules/newline-after-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module.exports = {
169169
let nextComment;
170170

171171
if (typeof parent.comments !== 'undefined' && options.considerComments) {
172-
nextComment = parent.comments.find((o) => o.loc.start.line === endLine + 1);
172+
nextComment = parent.comments.find((o) => o.loc.start.line >= endLine && o.loc.start.line <= endLine + options.count + 1);
173173
}
174174

175175
// skip "export import"s

tests/src/rules/newline-after-import.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
142142
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
143143
options: [{ count: 1, exactCount: true }],
144144
},
145+
{
146+
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
147+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
148+
options: [{ count: 2, exactCount: true }],
149+
},
150+
{
151+
code: `import foo from 'foo';\n// Some random comment\nvar bar = 'bar';`,
152+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
153+
options: [{ count: 1, exactCount: true }],
154+
},
155+
{
156+
code: `import foo from 'foo';\n\n\n// Some random comment\nvar bar = 'bar';`,
157+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
158+
options: [{ count: 2, exactCount: true, considerComments: true }],
159+
},
160+
{
161+
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
162+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
163+
options: [{ count: 1, exactCount: true, considerComments: true }],
164+
},
165+
{
166+
code: `/**\n * A leading comment\n */\nimport foo from 'foo';\n\n// Some random comment\nexport {foo};`,
167+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
168+
options: [{ count: 2, exactCount: true }],
169+
},
145170
{
146171
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
147172
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
@@ -171,6 +196,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
171196
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
172197
options: [{ count: 4, exactCount: true }],
173198
},
199+
{
200+
code: `var foo = require('foo-module');\n\n// Some random comment\n\n\nvar foo = 'bar';`,
201+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
202+
options: [{ count: 4, exactCount: true }],
203+
},
204+
{
205+
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
206+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
207+
options: [{ count: 4, exactCount: true, considerComments: true }],
208+
},
174209
{
175210
code: `require('foo-module');\n\nvar foo = 'bar';`,
176211
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
@@ -683,6 +718,72 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
683718
}],
684719
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
685720
},
721+
{
722+
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
723+
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
724+
options: [{ count: 2, exactCount: true }],
725+
errors: [{
726+
line: 1,
727+
column: 1,
728+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
729+
}],
730+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
731+
},
732+
{
733+
code: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
734+
output: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
735+
options: [{ count: 2, exactCount: true }],
736+
errors: [{
737+
line: 1,
738+
column: 1,
739+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
740+
}],
741+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
742+
},
743+
{
744+
code: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
745+
output: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
746+
options: [{ count: 2, exactCount: true }],
747+
errors: [{
748+
line: 1,
749+
column: 1,
750+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
751+
}],
752+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
753+
},
754+
{
755+
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
756+
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
757+
options: [{ count: 2, exactCount: true, considerComments: true }],
758+
errors: [{
759+
line: 1,
760+
column: 1,
761+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
762+
}],
763+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
764+
},
765+
{
766+
code: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
767+
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
768+
options: [{ count: 2, exactCount: true, considerComments: true }],
769+
errors: [{
770+
line: 1,
771+
column: 1,
772+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
773+
}],
774+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
775+
},
776+
{
777+
code: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
778+
output: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
779+
options: [{ count: 2, exactCount: true, considerComments: true }],
780+
errors: [{
781+
line: 1,
782+
column: 1,
783+
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
784+
}],
785+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
786+
},
686787
{
687788
code: `
688789
import foo from 'foo';
@@ -728,5 +829,37 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
728829
}],
729830
parserOptions: { ecmaVersion: 2015 },
730831
},
832+
{
833+
code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
834+
output: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
835+
options: [{ count: 2, exactCount: true }],
836+
errors: [{
837+
line: 1,
838+
column: 1,
839+
message: 'Expected 2 empty lines after require statement not followed by another require.',
840+
}],
841+
parserOptions: { ecmaVersion: 2015 },
842+
},
843+
{
844+
code: `import foo from 'foo';// some random comment\nexport default function() {};`,
845+
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
846+
options: [{ count: 1, exactCount: true, considerComments: true }],
847+
errors: [{
848+
line: 1,
849+
column: 1,
850+
message: IMPORT_ERROR_MESSAGE,
851+
}],
852+
parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' },
853+
},
854+
{
855+
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
856+
options: [{ count: 2, exactCount: true, considerComments: true }],
857+
errors: [{
858+
line: 1,
859+
column: 1,
860+
message: 'Expected 2 empty lines after require statement not followed by another require.',
861+
}],
862+
parserOptions: { ecmaVersion: 2015 },
863+
},
731864
),
732865
});

0 commit comments

Comments
 (0)