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: no-empty suggest to add comment in empty BlockStatement #16470

Merged
merged 4 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -731,7 +731,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 @@ -938,6 +938,7 @@
},
"no-empty": {
"type": "suggestion",
"hasSuggestions": true,
"docs": {
"description": "Disallow empty block statements",
"recommended": true,
Expand Down
21 changes: 19 additions & 2 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 Down Expand Up @@ -71,7 +73,22 @@ 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) {
const range = [node.range[0] + 1, node.range[1] - 1];

return fixer.replaceTextRange(range, " /* empty */ ");
}
}
]
});
},

SwitchStatement(node) {
Expand Down
176 changes: 163 additions & 13 deletions tests/lib/rules/no-empty.js
Expand Up @@ -44,36 +44,186 @@ ruleTester.run("no-empty", rule, {
{ code: "try { foo(); } catch (ex) {} finally { bar(); }", options: [{ allowEmptyCatch: true }] }
],
invalid: [
{ code: "try {} catch (ex) {throw ex}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "try { foo() } catch (ex) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "try { foo() } catch (ex) {throw ex} finally {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "if (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "while (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "for (;foo;) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
{ code: "switch(foo) {}", errors: [{ messageId: "unexpected", data: { type: "switch" }, type: "SwitchStatement" }] },
{
code: "try {} catch (ex) {throw ex}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
data: { type: "block" },
output: "try { /* empty */ } catch (ex) {throw ex}"
}]
}]
},
{
code: "try { foo() } catch (ex) {}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "try { foo() } catch (ex) { /* empty */ }"
}]
}]
},
{
code: "try { foo() } catch (ex) {throw ex} finally {}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "try { foo() } catch (ex) {throw ex} finally { /* empty */ }"
}]
}]
},
{
code: "if (foo) {}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "if (foo) { /* empty */ }"
}]
}]
},
{
code: "while (foo) {}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "while (foo) { /* empty */ }"
}]
}]
},
{
code: "for (;foo;) {}",
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "for (;foo;) { /* empty */ }"
}]
}]
},
{
code: "switch(foo) {}",
errors: [{
messageId: "unexpected",
data: { type: "switch" },
type: "SwitchStatement"
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}]
},
{
code: "switch (foo) { /* empty */ }",
errors: [{
messageId: "unexpected",
data: { type: "switch" },
type: "SwitchStatement",
suggestions: null
}]
},
{
code: "try {} catch (ex) {}",
options: [{ allowEmptyCatch: true }],
errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }]
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "try { /* empty */ } catch (ex) {}"
}]
}]
},
{
code: "try { foo(); } catch (ex) {} finally {}",
options: [{ allowEmptyCatch: true }],
errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }]
errors: [{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [{
messageId: "suggestComment",
data: { type: "block" },
output: "try { foo(); } catch (ex) {} finally { /* empty */ }"
}]
}]
},
{
code: "try {} catch (ex) {} finally {}",
options: [{ allowEmptyCatch: true }],
errors: [
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" },
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }
{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [
{
messageId: "suggestComment",
data: { type: "block" },
output: "try { /* empty */ } catch (ex) {} finally {}"
}
]
},
{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [
{
messageId: "suggestComment",
data: { type: "block" },
output: "try {} catch (ex) {} finally { /* empty */ }"
}
]
}
]
},
{
code: "try { foo(); } catch (ex) {} finally {}",
errors: [
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" },
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }
{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [
{
messageId: "suggestComment",
data: { type: "block" },
output: "try { foo(); } catch (ex) { /* empty */ } finally {}"
}
]
},
{
messageId: "unexpected",
data: { type: "block" },
type: "BlockStatement",
suggestions: [
{
messageId: "suggestComment",
data: { type: "block" },
output: "try { foo(); } catch (ex) {} finally { /* empty */ }"
}
]
}
]
}
]
Expand Down