Skip to content

Commit

Permalink
feat: suggest to add comments inside empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
amareshsm committed Jul 3, 2022
1 parent 9615a42 commit ecc2bd2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/_data/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@
"description": "Disallow empty block statements",
"recommended": true,
"fixable": false,
"hasSuggestions": false
"hasSuggestions": true
},
{
"name": "no-empty-function",
Expand Down
1 change: 1 addition & 0 deletions docs/src/_data/rules_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@
"fixable": "code"
},
"no-empty": {
"hasSuggestions": true,
"type": "suggestion",
"docs": {
"description": "Disallow empty block statements",
Expand Down
38 changes: 35 additions & 3 deletions lib/rules/no-empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const astUtils = require("./utils/ast-utils");
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
hasSuggestions: true,
type: "suggestion",

docs: {
Expand All @@ -39,7 +40,8 @@ module.exports = {
],

messages: {
unexpected: "Empty {{type}} statement."
unexpected: "Empty {{type}} statement.",
suggestComment: "Add comment inside empty {{type}} statement."
}
},

Expand All @@ -52,6 +54,8 @@ module.exports = {
return {
BlockStatement(node) {

const range = [node.range[0] + 1, node.range[1] - 1];

// if the body is not empty, we can just return immediately
if (node.body.length !== 0) {
return;
Expand All @@ -71,13 +75,41 @@ module.exports = {
return;
}

context.report({ node, messageId: "unexpected", data: { type: "block" } });
context.report({
node,
messageId: "unexpected",
data: { type: "block" },
suggest: [
{
messageId: "suggestComment",
data: { type: "block" },
fix(fixer) {
return fixer.replaceTextRange(range, " /* empty */ ");
}
}
]
});
},

SwitchStatement(node) {

const range = [node.range[0] + 1, node.range[1] - 1];

if (typeof node.cases === "undefined" || node.cases.length === 0) {
context.report({ node, messageId: "unexpected", data: { type: "switch" } });
context.report({
node,
messageId: "unexpected",
data: { type: "switch" },
suggest: [
{
messageId: "suggestComment",
data: { type: "block" },
fix(fixer) {
return fixer.replaceTextRange(range, " /* empty */ ");
}
}
]
});
}
}
};
Expand Down

0 comments on commit ecc2bd2

Please sign in to comment.