Skip to content

Commit ee1ea02

Browse files
developer-bandiljharb
authored andcommitted
[Fix] newline-after-import: fix considerComments option when require
1 parent 806e3c2 commit ee1ea02

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
- [`no-extraneous-dependencies`]: allow wrong path ([#3012], thanks [@chabb])
1717
- [`no-cycle`]: use scc algorithm to optimize ([#2998], thanks [@soryy708])
1818
- [`no-duplicates`]: Removing duplicates breaks in TypeScript ([#3033], thanks [@yesl-kim])
19+
- [`newline-after-import`]: fix considerComments option when require ([#2952], thanks [@developer-bandi])
1920

2021
### Changed
2122
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
@@ -1136,6 +1137,7 @@ for info on changes for earlier releases.
11361137
[#2987]: https://github.com/import-js/eslint-plugin-import/pull/2987
11371138
[#2985]: https://github.com/import-js/eslint-plugin-import/pull/2985
11381139
[#2982]: https://github.com/import-js/eslint-plugin-import/pull/2982
1140+
[#2952]: https://github.com/import-js/eslint-plugin-import/pull/2952
11391141
[#2944]: https://github.com/import-js/eslint-plugin-import/pull/2944
11401142
[#2942]: https://github.com/import-js/eslint-plugin-import/pull/2942
11411143
[#2919]: https://github.com/import-js/eslint-plugin-import/pull/2919
@@ -1742,6 +1744,7 @@ for info on changes for earlier releases.
17421744
[@bicstone]: https://github.com/bicstone
17431745
[@Blasz]: https://github.com/Blasz
17441746
[@bmish]: https://github.com/bmish
1747+
[@developer-bandi]: https://github.com/developer-bandi
17451748
[@borisyankov]: https://github.com/borisyankov
17461749
[@bradennapier]: https://github.com/bradennapier
17471750
[@bradzacher]: https://github.com/bradzacher

src/rules/newline-after-import.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module.exports = {
124124
}
125125
}
126126

127-
function commentAfterImport(node, nextComment) {
127+
function commentAfterImport(node, nextComment, type) {
128128
const lineDifference = getLineDifference(node, nextComment);
129129
const EXPECTED_LINE_DIFFERENCE = options.count + 1;
130130

@@ -140,7 +140,7 @@ module.exports = {
140140
line: node.loc.end.line,
141141
column,
142142
},
143-
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after import statement not followed by another import.`,
143+
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after ${type} statement not followed by another ${type}.`,
144144
fix: options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference ? undefined : (fixer) => fixer.insertTextAfter(
145145
node,
146146
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference),
@@ -178,7 +178,7 @@ module.exports = {
178178
}
179179

180180
if (nextComment && typeof nextComment !== 'undefined') {
181-
commentAfterImport(node, nextComment);
181+
commentAfterImport(node, nextComment, 'import');
182182
} else if (nextNode && nextNode.type !== 'ImportDeclaration' && (nextNode.type !== 'TSImportEqualsDeclaration' || nextNode.isExport)) {
183183
checkForNewLine(node, nextNode, 'import');
184184
}
@@ -215,8 +215,18 @@ module.exports = {
215215
|| !containsNodeOrEqual(nextStatement, nextRequireCall)
216216
)
217217
) {
218-
219-
checkForNewLine(statementWithRequireCall, nextStatement, 'require');
218+
let nextComment;
219+
if (typeof statementWithRequireCall.parent.comments !== 'undefined' && options.considerComments) {
220+
const endLine = node.loc.end.line;
221+
nextComment = statementWithRequireCall.parent.comments.find((o) => o.loc.start.line >= endLine && o.loc.start.line <= endLine + options.count + 1);
222+
}
223+
224+
if (nextComment && typeof nextComment !== 'undefined') {
225+
226+
commentAfterImport(statementWithRequireCall, nextComment, 'require');
227+
} else {
228+
checkForNewLine(statementWithRequireCall, nextStatement, 'require');
229+
}
220230
}
221231
});
222232
},

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

+32-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getTSParsers, parsers, testVersion } from '../utils';
88
const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.';
99
const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => `Expected ${count} empty lines after import statement not followed by another import.`;
1010
const REQUIRE_ERROR_MESSAGE = 'Expected 1 empty line after require statement not followed by another require.';
11+
const REQUIRE_ERROR_MESSAGE_MULTIPLE = (count) => `Expected ${count} empty lines after require statement not followed by another require.`;
1112

1213
const ruleTester = new RuleTester();
1314

@@ -202,7 +203,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
202203
options: [{ count: 4, exactCount: true }],
203204
},
204205
{
205-
code: `var foo = require('foo-module');\n\n\n\n// Some random comment\nvar foo = 'bar';`,
206+
code: `var foo = require('foo-module');\n\n\n\n\n// Some random comment\nvar foo = 'bar';`,
206207
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
207208
options: [{ count: 4, exactCount: true, considerComments: true }],
208209
},
@@ -394,6 +395,19 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
394395
`,
395396
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
396397
},
398+
{
399+
code: `var foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
400+
options: [{ count: 2, considerComments: true }],
401+
},
402+
{
403+
code: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
404+
options: [{ count: 2, considerComments: true }],
405+
},
406+
{
407+
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
408+
options: [{ count: 2, exactCount: true, considerComments: true }],
409+
parserOptions: { ecmaVersion: 2015 },
410+
},
397411
),
398412

399413
invalid: [].concat(
@@ -825,7 +839,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
825839
errors: [{
826840
line: 1,
827841
column: 1,
828-
message: 'Expected 2 empty lines after require statement not followed by another require.',
842+
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
829843
}],
830844
parserOptions: { ecmaVersion: 2015 },
831845
},
@@ -836,7 +850,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
836850
errors: [{
837851
line: 1,
838852
column: 1,
839-
message: 'Expected 2 empty lines after require statement not followed by another require.',
853+
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
840854
}],
841855
parserOptions: { ecmaVersion: 2015 },
842856
},
@@ -852,14 +866,26 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
852866
parserOptions: { ecmaVersion: 2015, considerComments: true, sourceType: 'module' },
853867
},
854868
{
855-
code: `const foo = require('foo');\n\n\n// some random comment\nconst bar = function() {};`,
856-
options: [{ count: 2, exactCount: true, considerComments: true }],
869+
code: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n// Some random comment\nvar foo = 'bar';`,
870+
output: `var foo = require('foo-module');\nvar foo = require('foo-module');\n\n\n// Some random comment\nvar foo = 'bar';`,
871+
errors: [{
872+
line: 2,
873+
column: 1,
874+
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
875+
}],
876+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
877+
options: [{ considerComments: true, count: 2 }],
878+
},
879+
{
880+
code: `var foo = require('foo-module');\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
881+
output: `var foo = require('foo-module');\n\n\n/**\n * Test comment\n */\nvar foo = 'bar';`,
857882
errors: [{
858883
line: 1,
859884
column: 1,
860-
message: 'Expected 2 empty lines after require statement not followed by another require.',
885+
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
861886
}],
862887
parserOptions: { ecmaVersion: 2015 },
888+
options: [{ considerComments: true, count: 2 }],
863889
},
864890
),
865891
});

0 commit comments

Comments
 (0)