-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Baptiste Jamin
committed
Aug 29, 2024
1 parent
220a0a4
commit 8c49d80
Showing
5 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module.exports = { | ||
meta: { | ||
type: "layout", | ||
docs: { | ||
description: "enforce maximum one line break between declarations within functions", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
}, | ||
fixable: "whitespace", | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
function checkFunctionBody(node) { | ||
const bodyNode = node.body; | ||
if (bodyNode.type !== 'BlockStatement') return; | ||
|
||
const lines = sourceCode.lines; | ||
|
||
for (let i = 1; i < bodyNode.body.length; i++) { | ||
const currentNode = bodyNode.body[i]; | ||
const previousNode = bodyNode.body[i - 1]; | ||
const lineOfCurrentNode = currentNode.loc.start.line; | ||
const lineOfPreviousNodeEnd = previousNode.loc.end.line; | ||
|
||
// Count the empty lines between the end of the previous node and the start of the current one | ||
let emptyLineCount = 0; | ||
for (let j = lineOfPreviousNodeEnd; j < lineOfCurrentNode - 1; j++) { | ||
if (lines[j].trim() === '') { | ||
emptyLineCount++; | ||
} | ||
} | ||
|
||
// Report an error if there are more than one empty line | ||
if (emptyLineCount > 1) { | ||
context.report({ | ||
node: currentNode, | ||
message: "Expected at most one line break between declarations within a function.", | ||
fix(fixer) { | ||
const range = [ | ||
sourceCode.getIndexFromLoc({ line: lineOfPreviousNodeEnd, column: 0 }), | ||
sourceCode.getIndexFromLoc({ line: lineOfCurrentNode, column: 0 }) | ||
]; | ||
return fixer.replaceTextRange(range, '\n\n'); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
FunctionDeclaration: checkFunctionBody, | ||
FunctionExpression: checkFunctionBody, | ||
ArrowFunctionExpression(node) { | ||
if (node.body.type === 'BlockStatement') { | ||
checkFunctionBody(node); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |