Skip to content

Commit

Permalink
feat: no-empty suggest to add comment in empty BlockStatement (#16470)
Browse files Browse the repository at this point in the history
* feat: suggest to add comment in empty BlockStatements

* test: update inavlid test cases with suggestions

* test: more strict assertions

* test: assert for suggestions

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
snitin315 and mdjermanovic committed Nov 2, 2022
1 parent 324db1a commit 69216ee
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 16 deletions.
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
177 changes: 164 additions & 13 deletions tests/lib/rules/no-empty.js
Expand Up @@ -44,36 +44,187 @@ 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",
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",
suggestions: null
}]
},
{
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

0 comments on commit 69216ee

Please sign in to comment.