Skip to content

Commit 0396775

Browse files
authored
fix: lines-around-comment apply allowBlockStart for switch statements (#16153)
* fix: apply `allowBlockStart` for switch statements too * docs: add switch statement examples for lines-around-comment-rule * fix: check if comment is after the `{` token * fix: check for the correct opening brace * docs: add switch statements for allowBlockEnd and allowBlockStart (lines-around-comment)
1 parent 2aadc93 commit 0396775

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

docs/src/rules/lines-around-comment.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ This rule has an object option:
2525
* `"afterBlockComment": true` requires an empty line after block comments
2626
* `"beforeLineComment": true` requires an empty line before line comments
2727
* `"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
3030
* `"allowObjectStart": true` allows comments to appear at the start of object literals
3131
* `"allowObjectEnd": true` allows comments to appear at the end of object literals
3232
* `"allowArrayStart": true` allows comments to appear at the start of array literals
@@ -230,6 +230,14 @@ class C {
230230
foo();
231231
}
232232
}
233+
234+
switch (foo) {
235+
/* what a great and wonderful day */
236+
237+
case 1:
238+
bar();
239+
break;
240+
}
233241
```
234242

235243
:::
@@ -308,6 +316,14 @@ class C {
308316

309317
/* what a great and wonderful day */
310318
}
319+
320+
switch (foo) {
321+
case 1:
322+
bar();
323+
break;
324+
325+
/* what a great and wonderful day */
326+
}
311327
```
312328

313329
:::

lib/rules/lines-around-comment.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,15 @@ module.exports = {
231231
const parent = getParentNodeOfToken(token);
232232

233233
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+
}
237243

238244
return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
239245
}
@@ -264,7 +270,8 @@ module.exports = {
264270
isCommentAtParentStart(token, "ClassBody") ||
265271
isCommentAtParentStart(token, "BlockStatement") ||
266272
isCommentAtParentStart(token, "StaticBlock") ||
267-
isCommentAtParentStart(token, "SwitchCase")
273+
isCommentAtParentStart(token, "SwitchCase") ||
274+
isCommentAtParentStart(token, "SwitchStatement")
268275
);
269276
}
270277

tests/lib/rules/lines-around-comment.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,60 @@ ruleTester.run("lines-around-comment", rule, {
364364
parserOptions: { ecmaVersion: 2022 }
365365
},
366366

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+
367421
// check for block end comments
368422
{
369423
code: "var a,\n// line\n\nb;",
@@ -2106,6 +2160,39 @@ ruleTester.run("lines-around-comment", rule, {
21062160
output: "foo;\n\n/* fallthrough */",
21072161
options: [],
21082162
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" }]
21092196
}
21102197
]
21112198

0 commit comments

Comments
 (0)