Skip to content

Commit 69216ee

Browse files
feat: no-empty suggest to add comment in empty BlockStatement (#16470)
* 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>
1 parent 324db1a commit 69216ee

4 files changed

Lines changed: 185 additions & 16 deletions

File tree

docs/src/_data/rules.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@
731731
"description": "Disallow empty block statements",
732732
"recommended": true,
733733
"fixable": false,
734-
"hasSuggestions": false
734+
"hasSuggestions": true
735735
},
736736
{
737737
"name": "no-empty-function",

docs/src/_data/rules_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@
938938
},
939939
"no-empty": {
940940
"type": "suggestion",
941+
"hasSuggestions": true,
941942
"docs": {
942943
"description": "Disallow empty block statements",
943944
"recommended": true,

lib/rules/no-empty.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const astUtils = require("./utils/ast-utils");
1717
/** @type {import('../shared/types').Rule} */
1818
module.exports = {
1919
meta: {
20+
hasSuggestions: true,
2021
type: "suggestion",
2122

2223
docs: {
@@ -39,7 +40,8 @@ module.exports = {
3940
],
4041

4142
messages: {
42-
unexpected: "Empty {{type}} statement."
43+
unexpected: "Empty {{type}} statement.",
44+
suggestComment: "Add comment inside empty {{type}} statement."
4345
}
4446
},
4547

@@ -71,7 +73,22 @@ module.exports = {
7173
return;
7274
}
7375

74-
context.report({ node, messageId: "unexpected", data: { type: "block" } });
76+
context.report({
77+
node,
78+
messageId: "unexpected",
79+
data: { type: "block" },
80+
suggest: [
81+
{
82+
messageId: "suggestComment",
83+
data: { type: "block" },
84+
fix(fixer) {
85+
const range = [node.range[0] + 1, node.range[1] - 1];
86+
87+
return fixer.replaceTextRange(range, " /* empty */ ");
88+
}
89+
}
90+
]
91+
});
7592
},
7693

7794
SwitchStatement(node) {

tests/lib/rules/no-empty.js

Lines changed: 164 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,187 @@ ruleTester.run("no-empty", rule, {
4444
{ code: "try { foo(); } catch (ex) {} finally { bar(); }", options: [{ allowEmptyCatch: true }] }
4545
],
4646
invalid: [
47-
{ code: "try {} catch (ex) {throw ex}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
48-
{ code: "try { foo() } catch (ex) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
49-
{ code: "try { foo() } catch (ex) {throw ex} finally {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
50-
{ code: "if (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
51-
{ code: "while (foo) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
52-
{ code: "for (;foo;) {}", errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }] },
53-
{ code: "switch(foo) {}", errors: [{ messageId: "unexpected", data: { type: "switch" }, type: "SwitchStatement" }] },
47+
{
48+
code: "try {} catch (ex) {throw ex}",
49+
errors: [{
50+
messageId: "unexpected",
51+
data: { type: "block" },
52+
type: "BlockStatement",
53+
suggestions: [{
54+
messageId: "suggestComment",
55+
data: { type: "block" },
56+
output: "try { /* empty */ } catch (ex) {throw ex}"
57+
}]
58+
}]
59+
},
60+
{
61+
code: "try { foo() } catch (ex) {}",
62+
errors: [{
63+
messageId: "unexpected",
64+
data: { type: "block" },
65+
type: "BlockStatement",
66+
suggestions: [{
67+
messageId: "suggestComment",
68+
data: { type: "block" },
69+
output: "try { foo() } catch (ex) { /* empty */ }"
70+
}]
71+
}]
72+
},
73+
{
74+
code: "try { foo() } catch (ex) {throw ex} finally {}",
75+
errors: [{
76+
messageId: "unexpected",
77+
data: { type: "block" },
78+
type: "BlockStatement",
79+
suggestions: [{
80+
messageId: "suggestComment",
81+
data: { type: "block" },
82+
output: "try { foo() } catch (ex) {throw ex} finally { /* empty */ }"
83+
}]
84+
}]
85+
},
86+
{
87+
code: "if (foo) {}",
88+
errors: [{
89+
messageId: "unexpected",
90+
data: { type: "block" },
91+
type: "BlockStatement",
92+
suggestions: [{
93+
messageId: "suggestComment",
94+
data: { type: "block" },
95+
output: "if (foo) { /* empty */ }"
96+
}]
97+
}]
98+
},
99+
{
100+
code: "while (foo) {}",
101+
errors: [{
102+
messageId: "unexpected",
103+
data: { type: "block" },
104+
type: "BlockStatement",
105+
suggestions: [{
106+
messageId: "suggestComment",
107+
data: { type: "block" },
108+
output: "while (foo) { /* empty */ }"
109+
}]
110+
}]
111+
},
112+
{
113+
code: "for (;foo;) {}",
114+
errors: [{
115+
messageId: "unexpected",
116+
data: { type: "block" },
117+
type: "BlockStatement",
118+
suggestions: [{
119+
messageId: "suggestComment",
120+
data: { type: "block" },
121+
output: "for (;foo;) { /* empty */ }"
122+
}]
123+
}]
124+
},
125+
{
126+
code: "switch(foo) {}",
127+
errors: [{
128+
messageId: "unexpected",
129+
data: { type: "switch" },
130+
type: "SwitchStatement",
131+
suggestions: null
132+
}]
133+
},
134+
{
135+
code: "switch (foo) { /* empty */ }",
136+
errors: [{
137+
messageId: "unexpected",
138+
data: { type: "switch" },
139+
type: "SwitchStatement",
140+
suggestions: null
141+
}]
142+
},
54143
{
55144
code: "try {} catch (ex) {}",
56145
options: [{ allowEmptyCatch: true }],
57-
errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }]
146+
errors: [{
147+
messageId: "unexpected",
148+
data: { type: "block" },
149+
type: "BlockStatement",
150+
suggestions: [{
151+
messageId: "suggestComment",
152+
data: { type: "block" },
153+
output: "try { /* empty */ } catch (ex) {}"
154+
}]
155+
}]
58156
},
59157
{
60158
code: "try { foo(); } catch (ex) {} finally {}",
61159
options: [{ allowEmptyCatch: true }],
62-
errors: [{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }]
160+
errors: [{
161+
messageId: "unexpected",
162+
data: { type: "block" },
163+
type: "BlockStatement",
164+
suggestions: [{
165+
messageId: "suggestComment",
166+
data: { type: "block" },
167+
output: "try { foo(); } catch (ex) {} finally { /* empty */ }"
168+
}]
169+
}]
63170
},
64171
{
65172
code: "try {} catch (ex) {} finally {}",
66173
options: [{ allowEmptyCatch: true }],
67174
errors: [
68-
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" },
69-
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }
175+
{
176+
messageId: "unexpected",
177+
data: { type: "block" },
178+
type: "BlockStatement",
179+
suggestions: [
180+
{
181+
messageId: "suggestComment",
182+
data: { type: "block" },
183+
output: "try { /* empty */ } catch (ex) {} finally {}"
184+
}
185+
]
186+
},
187+
{
188+
messageId: "unexpected",
189+
data: { type: "block" },
190+
type: "BlockStatement",
191+
suggestions: [
192+
{
193+
messageId: "suggestComment",
194+
data: { type: "block" },
195+
output: "try {} catch (ex) {} finally { /* empty */ }"
196+
}
197+
]
198+
}
70199
]
71200
},
72201
{
73202
code: "try { foo(); } catch (ex) {} finally {}",
74203
errors: [
75-
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" },
76-
{ messageId: "unexpected", data: { type: "block" }, type: "BlockStatement" }
204+
{
205+
messageId: "unexpected",
206+
data: { type: "block" },
207+
type: "BlockStatement",
208+
suggestions: [
209+
{
210+
messageId: "suggestComment",
211+
data: { type: "block" },
212+
output: "try { foo(); } catch (ex) { /* empty */ } finally {}"
213+
}
214+
]
215+
},
216+
{
217+
messageId: "unexpected",
218+
data: { type: "block" },
219+
type: "BlockStatement",
220+
suggestions: [
221+
{
222+
messageId: "suggestComment",
223+
data: { type: "block" },
224+
output: "try { foo(); } catch (ex) {} finally { /* empty */ }"
225+
}
226+
]
227+
}
77228
]
78229
}
79230
]

0 commit comments

Comments
 (0)