Skip to content

Commit

Permalink
fix: backports import-js#2952
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 4, 2024
1 parent efaf8a0 commit ffc695e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-pandas-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

Fix `newline-after-import`'s `considerComments` options when linting `require`, backports https://github.com/import-js/eslint-plugin-import/pull/2952
33 changes: 30 additions & 3 deletions src/rules/newline-after-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export = createRule<[Options?], MessageId>({
function commentAfterImport(
node: TSESTree.Node,
nextComment: TSESTree.Comment,
type: 'import' | 'require',
) {
const lineDifference = getLineDifference(node, nextComment)
const EXPECTED_LINE_DIFFERENCE = options.count + 1
Expand All @@ -197,7 +198,7 @@ export = createRule<[Options?], MessageId>({
data: {
count: options.count,
lineSuffix: options.count > 1 ? 's' : '',
type: 'import',
type,
},
fix:
options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference
Expand Down Expand Up @@ -253,7 +254,7 @@ export = createRule<[Options?], MessageId>({
}

if (nextComment) {
commentAfterImport(node, nextComment)
commentAfterImport(node, nextComment, 'import')
} else if (
nextNode &&
nextNode.type !== 'ImportDeclaration' &&
Expand Down Expand Up @@ -301,7 +302,33 @@ export = createRule<[Options?], MessageId>({
(!nextRequireCall ||
!containsNodeOrEqual(nextStatement, nextRequireCall))
) {
checkForNewLine(statementWithRequireCall, nextStatement, 'require')
let nextComment
if (
'comments' in statementWithRequireCall.parent &&
statementWithRequireCall.parent.comments !== undefined &&
options.considerComments
) {
const endLine = node.loc.end.line
nextComment = statementWithRequireCall.parent.comments.find(
o =>
o.loc.start.line >= endLine &&
o.loc.start.line <= endLine + options.count + 1,
)
}

if (nextComment && nextComment !== undefined) {
commentAfterImport(
statementWithRequireCall,
nextComment,
'require',
)
} else {
checkForNewLine(
statementWithRequireCall,
nextStatement,
'require',
)
}
}
}
},
Expand Down
49 changes: 43 additions & 6 deletions test/rules/newline-after-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ ruleTester.run('newline-after-import', rule, {
options: [{ count: 4, exactCount: true }],
},
{
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
code: `var foo = require('foo-module');\n\n\n\n\n// Some random comment\nvar foo = 'bar';`,
languageOptions: {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
Expand Down Expand Up @@ -479,6 +479,21 @@ ruleTester.run('newline-after-import', rule, {
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
},
{
code: `var foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
options: [{ count: 2, considerComments: true }],
},
{
code: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
options: [{ count: 2, considerComments: true }],
},
{
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
options: [{ count: 2, exactCount: true, considerComments: true }],
languageOptions: {
parserOptions: { ecmaVersion: 2015 },
},
},
],

invalid: [
Expand Down Expand Up @@ -1054,17 +1069,39 @@ ruleTester.run('newline-after-import', rule, {
},
},
{
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
output: null,
options: [{ count: 2, exactCount: true, considerComments: true }],
code: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n// Some random comment\nvar foo = 'bar';`,
output: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
errors: [
{
line: 2,
column: 1,
messageId: `newline`,
},
],
languageOptions: {
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
},
},
options: [{ considerComments: true, count: 2 }],
},
{
code: `var foo = require('foo-module');\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
output: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
errors: [
{
line: 1,
column: 1,
...getRequireError(2),
messageId: `newline`,
},
],
languageOptions: { parserOptions: { ecmaVersion: 2015 } },
languageOptions: {
parserOptions: {
ecmaVersion: 2015,
},
},
options: [{ considerComments: true, count: 2 }],
},
],
})

0 comments on commit ffc695e

Please sign in to comment.