Skip to content

Commit

Permalink
[Fix] newline-after-import: fix exactCount with `considerComments…
Browse files Browse the repository at this point in the history
…` false positive, when there is a leading comment

Fixes #2882.
  • Loading branch information
kinland authored and ljharb committed Sep 26, 2023
1 parent 8705121 commit 66cb10f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Added
- TypeScript config: add .cts and .mts extensions ([#2851], thanks [@Zamiell])
- [`newline-after-import`]: new option `exactCount` and docs update ([#1933], thanks [@anikethsaha] and [@reosarevok])
- [`newline-after-import`]: fix `exactCount` with `considerComments` false positive, when there is a leading comment ([#2884], thanks [@kinland])

## [2.28.1] - 2023-08-18

Expand Down Expand Up @@ -1091,6 +1092,7 @@ for info on changes for earlier releases.

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

[#2884]: https://github.com/import-js/eslint-plugin-import/pull/2884
[#2854]: https://github.com/import-js/eslint-plugin-import/pull/2854
[#2851]: https://github.com/import-js/eslint-plugin-import/pull/2851
[#2850]: https://github.com/import-js/eslint-plugin-import/pull/2850
Expand Down Expand Up @@ -1769,15 +1771,16 @@ for info on changes for earlier releases.
[@kentcdodds]: https://github.com/kentcdodds
[@kevin940726]: https://github.com/kevin940726
[@kgregory]: https://github.com/kgregory
[@kinland]: https://github.com/kinland
[@kirill-konshin]: https://github.com/kirill-konshin
[@kiwka]: https://github.com/kiwka
[@klimashkin]: https://github.com/klimashkin
[@kmui2]: https://github.com/kmui2
[@knpwrs]: https://github.com/knpwrs
[@KostyaZgara]: https://github.com/KostyaZgara
[@kylemh]: https://github.com/kylemh
[@laysent]: https://github.com/laysent
[@laurens-dg]: https://github.com/laurens-dg
[@laysent]: https://github.com/laysent
[@le0nik]: https://github.com/le0nik
[@leipert]: https://github.com/leipert
[@lemonmade]: https://github.com/lemonmade
Expand Down
2 changes: 1 addition & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module.exports = {
let nextComment;

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

// skip "export import"s
Expand Down
133 changes: 133 additions & 0 deletions tests/src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 1, exactCount: true }],
},
{
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 2, exactCount: true }],
},
{
code: `import foo from 'foo';\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 1, exactCount: true }],
},
{
code: `import foo from 'foo';\n\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 2, exactCount: true, considerComments: true }],
},
{
code: `import foo from 'foo';\n\n// Some random comment\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 1, exactCount: true, considerComments: true }],
},
{
code: `/**\n * A leading comment\n */\nimport foo from 'foo';\n\n// Some random comment\nexport {foo};`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 2, exactCount: true }],
},
{
code: `import foo from 'foo';\n\n\nvar bar = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand Down Expand Up @@ -171,6 +196,16 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true }],
},
{
code: `var foo = require('foo-module');\n\n// Some random comment\n\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true }],
},
{
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true, considerComments: true }],
},
{
code: `require('foo-module');\n\nvar foo = 'bar';`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
Expand Down Expand Up @@ -683,6 +718,72 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n// some random comment\n\n\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
output: `import foo from 'foo';\n// some random comment\n\n\n\nexport default function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n\n\n// some random comment\nexport default function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import foo from 'foo';
Expand Down Expand Up @@ -728,5 +829,37 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
}],
parserOptions: { ecmaVersion: 2015 },
},
{
code: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
output: `const foo = require('foo');\n\n\n\n// some random comment\nconst bar = function() {};`,
options: [{ count: 2, exactCount: true }],
errors: [{
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
}],
parserOptions: { ecmaVersion: 2015 },
},
{
code: `import foo from 'foo';// some random comment\nexport default function() {};`,
output: `import foo from 'foo';\n\n// some random comment\nexport default function() {};`,
options: [{ count: 1, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: IMPORT_ERROR_MESSAGE,
}],
parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' },
},
{
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
errors: [{
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
}],
parserOptions: { ecmaVersion: 2015 },
},
),
});

0 comments on commit 66cb10f

Please sign in to comment.