Skip to content

Commit

Permalink
feat: fix considerComments option when require
Browse files Browse the repository at this point in the history
  • Loading branch information
developer-bandi committed Jan 12, 2024
1 parent 7a21f7e commit a705312
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
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}.`,
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

0 comments on commit a705312

Please sign in to comment.