Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfa4601

Browse files
authored
fix: ignore empty switch statements with comments in no-empty rule (#20045)
* fix: ignore empty switch statements with comments in no-empty rule * simplify range
1 parent dfd11de commit bfa4601

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

docs/src/rules/no-empty.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ while (foo) {
5555
/* empty */
5656
}
5757

58+
switch(foo) {
59+
/* empty */
60+
}
61+
5862
try {
5963
doSomething();
6064
} catch (ex) {

lib/rules/no-empty.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,47 @@ module.exports = {
104104
typeof node.cases === "undefined" ||
105105
node.cases.length === 0
106106
) {
107+
const openingBrace = sourceCode.getTokenAfter(
108+
node.discriminant,
109+
astUtils.isOpeningBraceToken,
110+
);
111+
112+
const closingBrace = sourceCode.getLastToken(node);
113+
114+
if (
115+
sourceCode.commentsExistBetween(
116+
openingBrace,
117+
closingBrace,
118+
)
119+
) {
120+
return;
121+
}
122+
107123
context.report({
108124
node,
125+
loc: {
126+
start: openingBrace.loc.start,
127+
end: closingBrace.loc.end,
128+
},
109129
messageId: "unexpected",
110130
data: { type: "switch" },
131+
suggest: [
132+
{
133+
messageId: "suggestComment",
134+
data: { type: "switch" },
135+
fix(fixer) {
136+
const range = [
137+
openingBrace.range[1],
138+
closingBrace.range[0],
139+
];
140+
141+
return fixer.replaceTextRange(
142+
range,
143+
" /* empty */ ",
144+
);
145+
},
146+
},
147+
],
111148
});
112149
}
113150
},

tests/lib/rules/no-empty.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ruleTester.run("no-empty", rule, {
3030
"function foo() { }",
3131
"if (foo) {/* empty */}",
3232
"while (foo) {/* empty */}",
33+
"switch (foo) {/* empty */}",
3334
"for (;foo;) {/* empty */}",
3435
"try { foo() } catch (ex) {/* empty */}",
3536
"try { foo() } catch (ex) {// empty\n}",
@@ -159,18 +160,38 @@ ruleTester.run("no-empty", rule, {
159160
messageId: "unexpected",
160161
data: { type: "switch" },
161162
type: "SwitchStatement",
162-
suggestions: null,
163+
line: 1,
164+
column: 13,
165+
endLine: 1,
166+
endColumn: 15,
167+
suggestions: [
168+
{
169+
messageId: "suggestComment",
170+
data: { type: "switch" },
171+
output: "switch(foo) { /* empty */ }",
172+
},
173+
],
163174
},
164175
],
165176
},
166177
{
167-
code: "switch (foo) { /* empty */ }",
178+
code: "switch /* empty */ (/* empty */ foo /* empty */) /* empty */ {} /* empty */",
168179
errors: [
169180
{
170181
messageId: "unexpected",
171182
data: { type: "switch" },
172183
type: "SwitchStatement",
173-
suggestions: null,
184+
line: 1,
185+
column: 62,
186+
endLine: 1,
187+
endColumn: 64,
188+
suggestions: [
189+
{
190+
messageId: "suggestComment",
191+
data: { type: "switch" },
192+
output: "switch /* empty */ (/* empty */ foo /* empty */) /* empty */ { /* empty */ } /* empty */",
193+
},
194+
],
174195
},
175196
],
176197
},

0 commit comments

Comments
 (0)