Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: suggest to add comments inside empty blocks #16096

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/_data/rules.json
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
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
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 @@ -51,6 +53,7 @@ module.exports = {

return {
BlockStatement(node) {
const range = [node.range[0] + 1, node.range[1] - 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this into fix(fixer), since it's needed only for the fix, and at this point it isn't clear what the range represents.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can move.


// if the body is not empty, we can just return immediately
if (node.body.length !== 0) {
Expand All @@ -71,13 +74,42 @@ 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 closingCurly = sourceCode.getLastToken(node);
const openingCurly = sourceCode.getTokenBefore(node.cases.length ? node.cases[0] : closingCurly);
const range = [openingCurly.range[0] + 1, closingCurly.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: "switch" },
fix(fixer) {
return fixer.replaceTextRange(range, " /* empty */ ");
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
}
]
Comment on lines +103 to +111
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation is correct, but adding a comment inside an empty SwitchStatement doesn't really help, because it's still considered an error by this rule (playground link) so it seems we should provide these suggestions only for BlockStatement nodes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, this rule shouldn't provide suggestion to add a comment inside an empty switch statement, because that won't fix the lint problem reported by this rule.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update.

});
}
}
};
Expand Down