Skip to content

Commit

Permalink
v1.0.95
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Jamin committed Aug 29, 2024
1 parent 220a0a4 commit 8c49d80
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
"multiline-comment-end-backslash": require("./rules/multiline-comment-end-backslash"),
"newline-after-switch-case": require("./rules/newline-after-switch-case"),
"no-async": require("./rules/no-async"),
"no-extra-line-within-function": require("./rules/no-extra-line-within-function"),
"no-var-in-blocks": require("./rules/no-var-in-blocks"),
"no-space-in-optional-arguments": require("./rules/no-space-in-optional-arguments"),
"no-useless-template-literals": require("./rules/no-useless-template-literals"),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-crisp",
"version": "1.0.94",
"version": "1.0.95",
"description": "Custom ESLint Rules for Crisp",
"author": "Crisp IM SAS",
"main": "index.js",
Expand Down
1 change: 1 addition & 0 deletions recommended-vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ module.exports = {
"crisp/multiline-comment-end-backslash": "error",
"crisp/newline-after-switch-case": "error",
"crisp/no-async": "error",
"crisp/no-extra-line-within-function": "error",
"crisp/no-short-parameters": [
"error",

Expand Down
1 change: 1 addition & 0 deletions recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ module.exports = {
"crisp/multiline-comment-end-backslash": "error",
"crisp/newline-after-switch-case": "error",
"crisp/no-async": "error",
"crisp/no-extra-line-within-function": "error",
"crisp/no-var-in-blocks": "error",
"crisp/no-short-parameters": [
"error",
Expand Down
61 changes: 61 additions & 0 deletions rules/no-extra-line-within-function.js
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);
}
},
};
},
};

0 comments on commit 8c49d80

Please sign in to comment.