Skip to content

Commit

Permalink
v1.0.83
Browse files Browse the repository at this point in the history
Add align-comments rule
  • Loading branch information
eliottvincent committed Jun 22, 2024
1 parent b60ce26 commit bf99f32
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Each item has emojis denoting:

| Name | Description | 🟠 | 🟢 |
| :- | :- | :- | :- |
| [crisp/align-comments](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/align-comments.js) | Enforces alignment of comments compared to the previous line (the `indent` rule doesn't check this case) | 🟢 |
| [crisp/align-consecutive-class-assignements](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/align-consecutive-class-assignements.js) | Enforces alignment of consecutive assignment statements in a class constructor | 🟠 |
| [crisp/align-one-var](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/align-one-var.js) | Enforces alignment of variables in 'one-var' statements | 🟠 |
| [crisp/align-requires](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/align-requires.js) | Enforces alignment of require statements | 🟠 |
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = {
"recommended-vue": require("./recommended-vue")
},
rules: {
"align-comments": require("./rules/align-comments"),
"align-consecutive-class-assignements": require("./rules/align-consecutive-class-assignements"),
"align-one-var": require("./rules/align-one-var"),
"align-requires": require("./rules/align-requires"),
"const": require("./rules/const"),
Expand All @@ -30,10 +32,8 @@ module.exports = {
"one-space-after-operator": require("./rules/one-space-after-operator"),
"regex-in-constructor": require("./rules/regex-in-constructor"),
"ternary-parenthesis": require("./rules/ternary-parenthesis"),
"multiline-comment-end-backslash": require("./rules/multiline-comment-end-backslash"),
"two-lines-between-class-members": require("./rules/two-lines-between-class-members"),
"variable-names": require("./rules/variable-names"),
"align-consecutive-class-assignements": require("./rules/align-consecutive-class-assignements"),
"vue-computed-order": require("./rules/vue-computed-order"),
"vue-emits-order": require("./rules/vue-emits-order"),
"vue-header-check": require("./rules/vue-header-check"),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.82",
"version": "1.0.83",
"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 @@ -141,6 +141,7 @@ module.exports = {
"space-infix-ops": "error",

// Crisp JS rules
"crisp/align-comments": "error",
"crisp/enforce-optional": "error",
"crisp/header-check": "error",
"crisp/header-comments-check": "error",
Expand Down
104 changes: 104 additions & 0 deletions rules/align-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
module.exports = {
meta: {
type: "layout",
docs: {
description: "enforce comments alignment with the next line",
category: "Stylistic Issues",
recommended: false,
},
fixable: "whitespace",
schema: [],
},

create(context) {
const sourceCode = context.getSourceCode();
const processedComments = new Set();

function checkNode(node) {
// Ignore nodes without location info
if (!node.loc) {
return
};

const comments = sourceCode.getCommentsBefore(node);

comments.forEach(comment => {
if (processedComments.has(comment)) {
return
};

const commentLine = comment.loc.start.line;
const commentEndLine = comment.loc.end.line;
const nodeLine = node.loc.start.line;
const commentIndent = comment.loc.start.column;
const nodeIndent = node.loc.start.column;

// Ignore comments on the same line as code
for (let line = commentLine; line <= commentEndLine; line++) {
const lineText = sourceCode.lines[line - 1].trim();

// Ignore if the line contains code
if (lineText && !lineText.startsWith("//")) {
return
};
}

if (commentIndent !== nodeIndent) {
processedComments.add(comment);

context.report({
node,
message: "Comment should be aligned with the next line.",

fix(fixer) {
const indent = " ".repeat(nodeIndent);
const commentText = sourceCode.getText(comment);
const fixedComment = `\n${indent}${commentText.trim()}`;
return fixer.replaceTextRange([comment.range[0], comment.range[1]], fixedComment);
},
});
}
});
}

return {
Program(node) {
node.body.forEach(statement => {
checkNode(statement);
});
},

BlockStatement(node) {
node.body.forEach(statement => {
checkNode(statement);
});
},

ArrowFunctionExpression(node) {
if (node.body.type === "BlockStatement") {
node.body.body.forEach(statement => {
checkNode(statement);
});
} else {
checkNode(node.body);
}
},

FunctionExpression(node) {
if (node.body.type === "BlockStatement") {
node.body.body.forEach(statement => {
checkNode(statement);
});
}
},

FunctionDeclaration(node) {
if (node.body.type === "BlockStatement") {
node.body.body.forEach(statement => {
checkNode(statement);
});
}
},
};
},
};

0 comments on commit bf99f32

Please sign in to comment.