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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ const astUtils = require("./utils/ast-utils"); | |
/** @type {import('../shared/types').Rule} */ | ||
module.exports = { | ||
meta: { | ||
hasSuggestions: true, | ||
type: "suggestion", | ||
|
||
docs: { | ||
|
@@ -39,7 +40,8 @@ module.exports = { | |
], | ||
|
||
messages: { | ||
unexpected: "Empty {{type}} statement." | ||
unexpected: "Empty {{type}} statement.", | ||
suggestComment: "Add comment inside empty {{type}} statement." | ||
} | ||
}, | ||
|
||
|
@@ -51,6 +53,7 @@ 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) { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will update. |
||
}); | ||
} | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
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 therange
represents.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we can move.