From ffc695e6c5ec48736651ff39429daafdb405e002 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 5 Sep 2024 01:06:37 +0800 Subject: [PATCH] fix: backports https://github.com/import-js/eslint-plugin-import/pull/2952 --- .changeset/chilled-pandas-tickle.md | 5 +++ src/rules/newline-after-import.ts | 33 +++++++++++++++-- test/rules/newline-after-import.spec.ts | 49 ++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 .changeset/chilled-pandas-tickle.md diff --git a/.changeset/chilled-pandas-tickle.md b/.changeset/chilled-pandas-tickle.md new file mode 100644 index 000000000..a29d3076c --- /dev/null +++ b/.changeset/chilled-pandas-tickle.md @@ -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 diff --git a/src/rules/newline-after-import.ts b/src/rules/newline-after-import.ts index ecb7b0f70..197484bce 100644 --- a/src/rules/newline-after-import.ts +++ b/src/rules/newline-after-import.ts @@ -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 @@ -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 @@ -253,7 +254,7 @@ export = createRule<[Options?], MessageId>({ } if (nextComment) { - commentAfterImport(node, nextComment) + commentAfterImport(node, nextComment, 'import') } else if ( nextNode && nextNode.type !== 'ImportDeclaration' && @@ -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', + ) + } } } }, diff --git a/test/rules/newline-after-import.spec.ts b/test/rules/newline-after-import.spec.ts index 81efd8d97..a56f27a13 100644 --- a/test/rules/newline-after-import.spec.ts +++ b/test/rules/newline-after-import.spec.ts @@ -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' }, }, @@ -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: [ @@ -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 }], }, ], })