@@ -37,79 +37,308 @@ ruleTester.run("no-case-declarations", rule, {
3737 languageOptions : { ecmaVersion : 6 }
3838 } ,
3939 `
40+ switch (a) {
41+ case 1:
42+ case 2: {}
43+ }
44+ ` ,
45+ `
46+ switch (a) {
47+ case 1: var x;
48+ }
49+ `
50+ ] ,
51+ invalid : [
52+ {
53+ code : `
4054 switch (a) {
4155 case 1:
42- case 2: {}
56+ {}
57+ function f() {}
58+ break;
4359 }
4460 ` ,
45- `
61+ errors : [ {
62+ messageId : "unexpected" ,
63+ type : "FunctionDeclaration" ,
64+ suggestions : [
65+ {
66+ messageId : "addBrackets" ,
67+ output : `
4668 switch (a) {
47- case 1: var x;
69+ case 1:
70+ { {}
71+ function f() {}
72+ break; }
4873 }
4974 `
50- ] ,
51- invalid : [
75+ }
76+ ]
77+ } ]
78+ } ,
5279 {
5380 code : `
5481 switch (a) {
5582 case 1:
56- {}
57- function f() {}
58- break;
83+ case 2:
84+ let x;
5985 }
6086 ` ,
61- errors : [ { messageId : "unexpected" , type : "FunctionDeclaration" } ]
87+ languageOptions : { ecmaVersion : 6 } ,
88+ errors : [ {
89+ messageId : "unexpected" ,
90+ type : "VariableDeclaration" ,
91+ suggestions : [
92+ {
93+ messageId : "addBrackets" ,
94+ output : `
95+ switch (a) {
96+ case 1:
97+ case 2:
98+ { let x; }
99+ }
100+ `
101+ }
102+ ]
103+ } ]
62104 } ,
63105 {
64106 code : `
65107 switch (a) {
66108 case 1:
109+ let x;
110+ case 2:
111+ let y;
112+ }
113+ ` ,
114+ languageOptions : { ecmaVersion : 6 } ,
115+ errors : [
116+ {
117+ messageId : "unexpected" ,
118+ type : "VariableDeclaration" ,
119+ suggestions : [
120+ {
121+ messageId : "addBrackets" ,
122+ output : `
123+ switch (a) {
124+ case 1:
125+ { let x; }
67126 case 2:
127+ let y;
128+ }
129+ `
130+ }
131+ ]
132+ } ,
133+ {
134+ messageId : "unexpected" ,
135+ type : "VariableDeclaration" ,
136+ suggestions : [
137+ {
138+ messageId : "addBrackets" ,
139+ output : `
140+ switch (a) {
141+ case 1:
68142 let x;
143+ case 2:
144+ { let y; }
145+ }
146+ `
147+ }
148+ ]
149+ }
150+ ]
151+ } ,
152+ {
153+ code : `
154+ switch (a) {
155+ case 1:
156+ let x;
157+ default:
158+ let y;
69159 }
70160 ` ,
71161 languageOptions : { ecmaVersion : 6 } ,
72- errors : [ { messageId : "unexpected" , type : "VariableDeclaration" } ]
162+ errors : [
163+ {
164+ messageId : "unexpected" ,
165+ type : "VariableDeclaration" ,
166+ suggestions : [
167+ {
168+ messageId : "addBrackets" ,
169+ output : `
170+ switch (a) {
171+ case 1:
172+ { let x; }
173+ default:
174+ let y;
175+ }
176+ `
177+ }
178+ ]
179+ } ,
180+ {
181+ messageId : "unexpected" ,
182+ type : "VariableDeclaration" ,
183+ suggestions : [
184+ {
185+ messageId : "addBrackets" ,
186+ output : `
187+ switch (a) {
188+ case 1:
189+ let x;
190+ default:
191+ { let y; }
192+ }
193+ `
194+ }
195+ ]
196+ }
197+ ]
73198 } ,
74199 {
75200 code : "switch (a) { case 1: let x = 1; break; }" ,
76201 languageOptions : { ecmaVersion : 6 } ,
77- errors : [ { messageId : "unexpected" , type : "VariableDeclaration" } ]
202+ errors : [ {
203+ messageId : "unexpected" ,
204+ type : "VariableDeclaration" ,
205+ suggestions : [
206+ {
207+ messageId : "addBrackets" ,
208+ output : "switch (a) { case 1: { let x = 1; break; } }"
209+ }
210+ ]
211+ } ]
78212 } ,
79213 {
80214 code : "switch (a) { default: let x = 2; break; }" ,
81215 languageOptions : { ecmaVersion : 6 } ,
82- errors : [ { messageId : "unexpected" , type : "VariableDeclaration" } ]
216+ errors : [ {
217+ messageId : "unexpected" ,
218+ type : "VariableDeclaration" ,
219+ suggestions : [
220+ {
221+ messageId : "addBrackets" ,
222+ output : "switch (a) { default: { let x = 2; break; } }"
223+ }
224+ ]
225+ } ]
83226 } ,
84227 {
85228 code : "switch (a) { case 1: const x = 1; break; }" ,
86229 languageOptions : { ecmaVersion : 6 } ,
87- errors : [ { messageId : "unexpected" , type : "VariableDeclaration" } ]
230+ errors : [ {
231+ messageId : "unexpected" ,
232+ type : "VariableDeclaration" ,
233+ suggestions : [
234+ {
235+ messageId : "addBrackets" ,
236+ output : "switch (a) { case 1: { const x = 1; break; } }"
237+ }
238+ ]
239+ } ]
88240 } ,
89241 {
90242 code : "switch (a) { default: const x = 2; break; }" ,
91243 languageOptions : { ecmaVersion : 6 } ,
92- errors : [ { messageId : "unexpected" , type : "VariableDeclaration" } ]
244+ errors : [ {
245+ messageId : "unexpected" ,
246+ type : "VariableDeclaration" ,
247+ suggestions : [
248+ {
249+ messageId : "addBrackets" ,
250+ output : "switch (a) { default: { const x = 2; break; } }"
251+ }
252+ ]
253+ } ]
93254 } ,
94255 {
95256 code : "switch (a) { case 1: function f() {} break; }" ,
96257 languageOptions : { ecmaVersion : 6 } ,
97- errors : [ { messageId : "unexpected" , type : "FunctionDeclaration" } ]
258+ errors : [ {
259+ messageId : "unexpected" ,
260+ type : "FunctionDeclaration" ,
261+ suggestions : [
262+ {
263+ messageId : "addBrackets" ,
264+ output : "switch (a) { case 1: { function f() {} break; } }"
265+ }
266+ ]
267+ } ]
98268 } ,
99269 {
100270 code : "switch (a) { default: function f() {} break; }" ,
101271 languageOptions : { ecmaVersion : 6 } ,
102- errors : [ { messageId : "unexpected" , type : "FunctionDeclaration" } ]
272+ errors : [ {
273+ messageId : "unexpected" ,
274+ type : "FunctionDeclaration" ,
275+ suggestions : [
276+ {
277+ messageId : "addBrackets" ,
278+ output : "switch (a) { default: { function f() {} break; } }"
279+ }
280+ ]
281+ } ]
103282 } ,
104283 {
105284 code : "switch (a) { case 1: class C {} break; }" ,
106285 languageOptions : { ecmaVersion : 6 } ,
107- errors : [ { messageId : "unexpected" , type : "ClassDeclaration" } ]
286+ errors : [ {
287+ messageId : "unexpected" ,
288+ type : "ClassDeclaration" ,
289+ suggestions : [
290+ {
291+ messageId : "addBrackets" ,
292+ output : "switch (a) { case 1: { class C {} break; } }"
293+ }
294+ ]
295+ } ]
108296 } ,
109297 {
110298 code : "switch (a) { default: class C {} break; }" ,
111299 languageOptions : { ecmaVersion : 6 } ,
112- errors : [ { messageId : "unexpected" , type : "ClassDeclaration" } ]
300+ errors : [ {
301+ messageId : "unexpected" ,
302+ type : "ClassDeclaration" ,
303+ suggestions : [
304+ {
305+ messageId : "addBrackets" ,
306+ output : "switch (a) { default: { class C {} break; } }"
307+ }
308+ ]
309+ } ]
310+ } ,
311+
312+ // https://github.com/eslint/eslint/pull/18388#issuecomment-2075356456
313+ {
314+ code : `
315+ switch ("foo") {
316+ case "bar":
317+ function baz() { }
318+ break;
319+ default:
320+ baz();
321+ }
322+ ` ,
323+ languageOptions : { ecmaVersion : "latest" } ,
324+ errors : [ {
325+ messageId : "unexpected" ,
326+ type : "FunctionDeclaration" ,
327+ suggestions : [
328+ {
329+ messageId : "addBrackets" ,
330+ output : `
331+ switch ("foo") {
332+ case "bar":
333+ { function baz() { }
334+ break; }
335+ default:
336+ baz();
337+ }
338+ `
339+ }
340+ ]
341+ } ]
113342 }
114343 ]
115344} ) ;
0 commit comments