@@ -37,79 +37,308 @@ ruleTester.run("no-case-declarations", rule, {
37
37
languageOptions : { ecmaVersion : 6 }
38
38
} ,
39
39
`
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 : `
40
54
switch (a) {
41
55
case 1:
42
- case 2: {}
56
+ {}
57
+ function f() {}
58
+ break;
43
59
}
44
60
` ,
45
- `
61
+ errors : [ {
62
+ messageId : "unexpected" ,
63
+ type : "FunctionDeclaration" ,
64
+ suggestions : [
65
+ {
66
+ messageId : "addBrackets" ,
67
+ output : `
46
68
switch (a) {
47
- case 1: var x;
69
+ case 1:
70
+ { {}
71
+ function f() {}
72
+ break; }
48
73
}
49
74
`
50
- ] ,
51
- invalid : [
75
+ }
76
+ ]
77
+ } ]
78
+ } ,
52
79
{
53
80
code : `
54
81
switch (a) {
55
82
case 1:
56
- {}
57
- function f() {}
58
- break;
83
+ case 2:
84
+ let x;
59
85
}
60
86
` ,
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
+ } ]
62
104
} ,
63
105
{
64
106
code : `
65
107
switch (a) {
66
108
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; }
67
126
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:
68
142
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;
69
159
}
70
160
` ,
71
161
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
+ ]
73
198
} ,
74
199
{
75
200
code : "switch (a) { case 1: let x = 1; break; }" ,
76
201
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
+ } ]
78
212
} ,
79
213
{
80
214
code : "switch (a) { default: let x = 2; break; }" ,
81
215
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
+ } ]
83
226
} ,
84
227
{
85
228
code : "switch (a) { case 1: const x = 1; break; }" ,
86
229
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
+ } ]
88
240
} ,
89
241
{
90
242
code : "switch (a) { default: const x = 2; break; }" ,
91
243
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
+ } ]
93
254
} ,
94
255
{
95
256
code : "switch (a) { case 1: function f() {} break; }" ,
96
257
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
+ } ]
98
268
} ,
99
269
{
100
270
code : "switch (a) { default: function f() {} break; }" ,
101
271
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
+ } ]
103
282
} ,
104
283
{
105
284
code : "switch (a) { case 1: class C {} break; }" ,
106
285
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
+ } ]
108
296
} ,
109
297
{
110
298
code : "switch (a) { default: class C {} break; }" ,
111
299
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
+ } ]
113
342
}
114
343
]
115
344
} ) ;
0 commit comments