Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/newline after import #2952

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/rules/newline-after-import.js
Expand Up @@ -124,7 +124,7 @@ module.exports = {
}
}

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

Expand All @@ -140,7 +140,7 @@ module.exports = {
line: node.loc.end.line,
column,
},
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after import statement not followed by another import.`,
message: `Expected ${options.count} empty line${options.count > 1 ? 's' : ''} after ${type} statement not followed by another ${type}.`,
ljharb marked this conversation as resolved.
Show resolved Hide resolved
fix: options.exactCount && EXPECTED_LINE_DIFFERENCE < lineDifference ? undefined : (fixer) => fixer.insertTextAfter(
node,
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference),
Expand Down Expand Up @@ -178,7 +178,7 @@ module.exports = {
}

if (nextComment && typeof nextComment !== 'undefined') {
commentAfterImport(node, nextComment);
commentAfterImport(node, nextComment, 'import');
} else if (nextNode && nextNode.type !== 'ImportDeclaration' && (nextNode.type !== 'TSImportEqualsDeclaration' || nextNode.isExport)) {
checkForNewLine(node, nextNode, 'import');
}
Expand All @@ -201,6 +201,7 @@ module.exports = {
const nodePosition = findNodeIndexInScopeBody(scopeBody, node);
log('node position in scope:', nodePosition);

const endLine = node.loc.end.line;
const statementWithRequireCall = scopeBody[nodePosition];
const nextStatement = scopeBody[nodePosition + 1];
const nextRequireCall = requireCalls[index + 1];
Expand All @@ -215,8 +216,17 @@ module.exports = {
|| !containsNodeOrEqual(nextStatement, nextRequireCall)
)
) {
let nextComment;
if (typeof statementWithRequireCall.parent.comments !== 'undefined' && options.considerComments) {
nextComment = statementWithRequireCall.parent.comments.find((o) => o.loc.start.line >= endLine && o.loc.start.line <= endLine + options.count + 1);
}

if (nextComment && typeof nextComment !== 'undefined') {

checkForNewLine(statementWithRequireCall, nextStatement, 'require');
commentAfterImport(statementWithRequireCall, nextComment, 'require');
} else {
checkForNewLine(statementWithRequireCall, nextStatement, 'require');
}
}
});
},
Expand Down
33 changes: 27 additions & 6 deletions tests/src/rules/newline-after-import.js
Expand Up @@ -8,6 +8,7 @@ import { getTSParsers, parsers, testVersion } from '../utils';
const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.';
const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => `Expected ${count} empty lines after import statement not followed by another import.`;
const REQUIRE_ERROR_MESSAGE = 'Expected 1 empty line after require statement not followed by another require.';
const REQUIRE_ERROR_MESSAGE_MULTIPLE = (count) => `Expected ${count} empty lines after require statement not followed by another require.`;

const ruleTester = new RuleTester();

Expand Down Expand Up @@ -202,7 +203,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
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';`,
ljharb marked this conversation as resolved.
Show resolved Hide resolved
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
options: [{ count: 4, exactCount: true, considerComments: true }],
},
Expand Down Expand Up @@ -394,6 +395,14 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
`,
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 }],
},
),

invalid: [].concat(
Expand Down Expand Up @@ -825,7 +834,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
errors: [{
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015 },
},
Expand All @@ -836,7 +845,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
errors: [{
line: 1,
column: 1,
message: 'Expected 2 empty lines after require statement not followed by another require.',
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015 },
},
Expand All @@ -852,14 +861,26 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
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 }],
Comment on lines -855 to -856
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep this existing test case, to ensure it's not a breaking change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is valid, so if we want to keep it, I think the test case should be included in the valid array. Is it okay to do this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's fine - moving the test shows it's a bug fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I applied it.

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,
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
}],
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,
message: 'Expected 2 empty lines after require statement not followed by another require.',
message: REQUIRE_ERROR_MESSAGE_MULTIPLE(2),
}],
parserOptions: { ecmaVersion: 2015 },
options: [{ considerComments: true, count: 2 }],
},
),
});