Skip to content

Commit ffc695e

Browse files
committed
1 parent efaf8a0 commit ffc695e

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
Fix `newline-after-import`'s `considerComments` options when linting `require`, backports https://github.com/import-js/eslint-plugin-import/pull/2952

src/rules/newline-after-import.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export = createRule<[Options?], MessageId>({
177177
function commentAfterImport(
178178
node: TSESTree.Node,
179179
nextComment: TSESTree.Comment,
180+
type: 'import' | 'require',
180181
) {
181182
const lineDifference = getLineDifference(node, nextComment)
182183
const EXPECTED_LINE_DIFFERENCE = options.count + 1
@@ -197,7 +198,7 @@ export = createRule<[Options?], MessageId>({
197198
data: {
198199
count: options.count,
199200
lineSuffix: options.count > 1 ? 's' : '',
200-
type: 'import',
201+
type,
201202
},
202203
fix:
203204
options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference
@@ -253,7 +254,7 @@ export = createRule<[Options?], MessageId>({
253254
}
254255

255256
if (nextComment) {
256-
commentAfterImport(node, nextComment)
257+
commentAfterImport(node, nextComment, 'import')
257258
} else if (
258259
nextNode &&
259260
nextNode.type !== 'ImportDeclaration' &&
@@ -301,7 +302,33 @@ export = createRule<[Options?], MessageId>({
301302
(!nextRequireCall ||
302303
!containsNodeOrEqual(nextStatement, nextRequireCall))
303304
) {
304-
checkForNewLine(statementWithRequireCall, nextStatement, 'require')
305+
let nextComment
306+
if (
307+
'comments' in statementWithRequireCall.parent &&
308+
statementWithRequireCall.parent.comments !== undefined &&
309+
options.considerComments
310+
) {
311+
const endLine = node.loc.end.line
312+
nextComment = statementWithRequireCall.parent.comments.find(
313+
o =>
314+
o.loc.start.line >= endLine &&
315+
o.loc.start.line <= endLine + options.count + 1,
316+
)
317+
}
318+
319+
if (nextComment && nextComment !== undefined) {
320+
commentAfterImport(
321+
statementWithRequireCall,
322+
nextComment,
323+
'require',
324+
)
325+
} else {
326+
checkForNewLine(
327+
statementWithRequireCall,
328+
nextStatement,
329+
'require',
330+
)
331+
}
305332
}
306333
}
307334
},

test/rules/newline-after-import.spec.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ ruleTester.run('newline-after-import', rule, {
260260
options: [{ count: 4, exactCount: true }],
261261
},
262262
{
263-
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
263+
code: `var foo = require('foo-module');\n\n\n\n\n// Some random comment\nvar foo = 'bar';`,
264264
languageOptions: {
265265
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
266266
},
@@ -479,6 +479,21 @@ ruleTester.run('newline-after-import', rule, {
479479
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
480480
},
481481
},
482+
{
483+
code: `var foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
484+
options: [{ count: 2, considerComments: true }],
485+
},
486+
{
487+
code: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
488+
options: [{ count: 2, considerComments: true }],
489+
},
490+
{
491+
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
492+
options: [{ count: 2, exactCount: true, considerComments: true }],
493+
languageOptions: {
494+
parserOptions: { ecmaVersion: 2015 },
495+
},
496+
},
482497
],
483498

484499
invalid: [
@@ -1054,17 +1069,39 @@ ruleTester.run('newline-after-import', rule, {
10541069
},
10551070
},
10561071
{
1057-
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
1058-
output: null,
1059-
options: [{ count: 2, exactCount: true, considerComments: true }],
1072+
code: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n// Some random comment\nvar foo = 'bar';`,
1073+
output: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
1074+
errors: [
1075+
{
1076+
line: 2,
1077+
column: 1,
1078+
messageId: `newline`,
1079+
},
1080+
],
1081+
languageOptions: {
1082+
parserOptions: {
1083+
ecmaVersion: 2015,
1084+
sourceType: 'module',
1085+
},
1086+
},
1087+
options: [{ considerComments: true, count: 2 }],
1088+
},
1089+
{
1090+
code: `var foo = require('foo-module');\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
1091+
output: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
10601092
errors: [
10611093
{
10621094
line: 1,
10631095
column: 1,
1064-
...getRequireError(2),
1096+
messageId: `newline`,
10651097
},
10661098
],
1067-
languageOptions: { parserOptions: { ecmaVersion: 2015 } },
1099+
languageOptions: {
1100+
parserOptions: {
1101+
ecmaVersion: 2015,
1102+
},
1103+
},
1104+
options: [{ considerComments: true, count: 2 }],
10681105
},
10691106
],
10701107
})

0 commit comments

Comments
 (0)