+116
-6
lines changedFilter options
+116
-6
lines changed Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ This rule has an object option:
25
25
* `"afterBlockComment": true` requires an empty line after block comments
26
26
* `"beforeLineComment": true` requires an empty line before line comments
27
27
* `"afterLineComment": true` requires an empty line after line comments
28
-
* `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, and class static blocks
29
-
* `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, and class static blocks
28
+
* `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, switch statements, and class static blocks
29
+
* `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, switch statements, and class static blocks
30
30
* `"allowObjectStart": true` allows comments to appear at the start of object literals
31
31
* `"allowObjectEnd": true` allows comments to appear at the end of object literals
32
32
* `"allowArrayStart": true` allows comments to appear at the start of array literals
@@ -230,6 +230,14 @@ class C {
230
230
foo();
231
231
}
232
232
}
233
+
234
+
switch (foo) {
235
+
/* what a great and wonderful day */
236
+
237
+
case 1:
238
+
bar();
239
+
break;
240
+
}
233
241
```
234
242
235
243
:::
@@ -308,6 +316,14 @@ class C {
308
316
309
317
/* what a great and wonderful day */
310
318
}
319
+
320
+
switch (foo) {
321
+
case 1:
322
+
bar();
323
+
break;
324
+
325
+
/* what a great and wonderful day */
326
+
}
311
327
```
312
328
313
329
:::
Original file line number Diff line number Diff line change
@@ -231,9 +231,15 @@ module.exports = {
231
231
const parent = getParentNodeOfToken(token);
232
232
233
233
if (parent && isParentNodeType(parent, nodeType)) {
234
-
const parentStartNodeOrToken = parent.type === "StaticBlock"
235
-
? sourceCode.getFirstToken(parent, { skip: 1 }) // opening brace of the static block
236
-
: parent;
234
+
let parentStartNodeOrToken = parent;
235
+
236
+
if (parent.type === "StaticBlock") {
237
+
parentStartNodeOrToken = sourceCode.getFirstToken(parent, { skip: 1 }); // opening brace of the static block
238
+
} else if (parent.type === "SwitchStatement") {
239
+
parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, {
240
+
filter: astUtils.isOpeningBraceToken
241
+
}); // opening brace of the switch statement
242
+
}
237
243
238
244
return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
239
245
}
@@ -264,7 +270,8 @@ module.exports = {
264
270
isCommentAtParentStart(token, "ClassBody") ||
265
271
isCommentAtParentStart(token, "BlockStatement") ||
266
272
isCommentAtParentStart(token, "StaticBlock") ||
267
-
isCommentAtParentStart(token, "SwitchCase")
273
+
isCommentAtParentStart(token, "SwitchCase") ||
274
+
isCommentAtParentStart(token, "SwitchStatement")
268
275
);
269
276
}
270
277
Original file line number Diff line number Diff line change
@@ -364,6 +364,60 @@ ruleTester.run("lines-around-comment", rule, {
364
364
parserOptions: { ecmaVersion: 2022 }
365
365
},
366
366
367
+
// https://github.com/eslint/eslint/issues/16131
368
+
{
369
+
code: `
370
+
switch (foo) {
371
+
// this comment is allowed by allowBlockStart: true
372
+
373
+
case 1:
374
+
bar();
375
+
break;
376
+
377
+
// this comment is allowed by allowBlockEnd: true
378
+
}
379
+
`,
380
+
options: [{
381
+
allowBlockStart: true,
382
+
beforeLineComment: true,
383
+
afterLineComment: true,
384
+
allowBlockEnd: true
385
+
}]
386
+
},
387
+
{
388
+
code: `
389
+
switch (foo)
390
+
{
391
+
// this comment is allowed by allowBlockStart: true
392
+
393
+
case 1:
394
+
bar();
395
+
break;
396
+
}
397
+
`,
398
+
options: [{
399
+
allowBlockStart: true,
400
+
beforeLineComment: true,
401
+
afterLineComment: true
402
+
}]
403
+
},
404
+
{
405
+
code: `
406
+
switch (
407
+
function(){}()
408
+
)
409
+
{
410
+
// this comment is allowed by allowBlockStart: true
411
+
case foo:
412
+
break;
413
+
}
414
+
`,
415
+
options: [{
416
+
allowBlockStart: true,
417
+
beforeLineComment: true
418
+
}]
419
+
},
420
+
367
421
// check for block end comments
368
422
{
369
423
code: "var a,\n// line\n\nb;",
@@ -2106,6 +2160,39 @@ ruleTester.run("lines-around-comment", rule, {
2106
2160
output: "foo;\n\n/* fallthrough */",
2107
2161
options: [],
2108
2162
errors: [{ messageId: "before", type: "Block" }]
2163
+
},
2164
+
{
2165
+
code: `
2166
+
switch (
2167
+
// this comment is not allowed by allowBlockStart: true
2168
+
2169
+
foo
2170
+
)
2171
+
{
2172
+
case 1:
2173
+
bar();
2174
+
break;
2175
+
}
2176
+
`,
2177
+
output: `
2178
+
switch (
2179
+
2180
+
// this comment is not allowed by allowBlockStart: true
2181
+
2182
+
foo
2183
+
)
2184
+
{
2185
+
case 1:
2186
+
bar();
2187
+
break;
2188
+
}
2189
+
`,
2190
+
options: [{
2191
+
allowBlockStart: true,
2192
+
beforeLineComment: true,
2193
+
afterLineComment: true
2194
+
}],
2195
+
errors: [{ messageId: "before", type: "Line" }]
2109
2196
}
2110
2197
]
2111
2198
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4