Skip to content

Commit

Permalink
fix: lines-around-comment apply allowBlockStart for switch statemen…
Browse files Browse the repository at this point in the history
…ts (#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)
  • Loading branch information
snitin315 authored Jul 25, 2022
1 parent 2aadc93 commit 0396775
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
20 changes: 18 additions & 2 deletions docs/src/rules/lines-around-comment.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ This rule has an object option:
* `"afterBlockComment": true` requires an empty line after block comments
* `"beforeLineComment": true` requires an empty line before line comments
* `"afterLineComment": true` requires an empty line after line comments
* `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, and class static blocks
* `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, and class static blocks
* `"allowBlockStart": true` allows comments to appear at the start of block statements, function bodies, classes, switch statements, and class static blocks
* `"allowBlockEnd": true` allows comments to appear at the end of block statements, function bodies, classes, switch statements, and class static blocks
* `"allowObjectStart": true` allows comments to appear at the start of object literals
* `"allowObjectEnd": true` allows comments to appear at the end of object literals
* `"allowArrayStart": true` allows comments to appear at the start of array literals
Expand Down Expand Up @@ -230,6 +230,14 @@ class C {
foo();
}
}

switch (foo) {
/* what a great and wonderful day */

case 1:
bar();
break;
}
```

:::
Expand Down Expand Up @@ -308,6 +316,14 @@ class C {

/* what a great and wonderful day */
}

switch (foo) {
case 1:
bar();
break;

/* what a great and wonderful day */
}
```

:::
Expand Down
15 changes: 11 additions & 4 deletions lib/rules/lines-around-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,15 @@ module.exports = {
const parent = getParentNodeOfToken(token);

if (parent && isParentNodeType(parent, nodeType)) {
const parentStartNodeOrToken = parent.type === "StaticBlock"
? sourceCode.getFirstToken(parent, { skip: 1 }) // opening brace of the static block
: parent;
let parentStartNodeOrToken = parent;

if (parent.type === "StaticBlock") {
parentStartNodeOrToken = sourceCode.getFirstToken(parent, { skip: 1 }); // opening brace of the static block
} else if (parent.type === "SwitchStatement") {
parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, {
filter: astUtils.isOpeningBraceToken
}); // opening brace of the switch statement
}

return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
}
Expand Down Expand Up @@ -264,7 +270,8 @@ module.exports = {
isCommentAtParentStart(token, "ClassBody") ||
isCommentAtParentStart(token, "BlockStatement") ||
isCommentAtParentStart(token, "StaticBlock") ||
isCommentAtParentStart(token, "SwitchCase")
isCommentAtParentStart(token, "SwitchCase") ||
isCommentAtParentStart(token, "SwitchStatement")
);
}

Expand Down
87 changes: 87 additions & 0 deletions tests/lib/rules/lines-around-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,60 @@ ruleTester.run("lines-around-comment", rule, {
parserOptions: { ecmaVersion: 2022 }
},

// https://github.com/eslint/eslint/issues/16131
{
code: `
switch (foo) {
// this comment is allowed by allowBlockStart: true
case 1:
bar();
break;
// this comment is allowed by allowBlockEnd: true
}
`,
options: [{
allowBlockStart: true,
beforeLineComment: true,
afterLineComment: true,
allowBlockEnd: true
}]
},
{
code: `
switch (foo)
{
// this comment is allowed by allowBlockStart: true
case 1:
bar();
break;
}
`,
options: [{
allowBlockStart: true,
beforeLineComment: true,
afterLineComment: true
}]
},
{
code: `
switch (
function(){}()
)
{
// this comment is allowed by allowBlockStart: true
case foo:
break;
}
`,
options: [{
allowBlockStart: true,
beforeLineComment: true
}]
},

// check for block end comments
{
code: "var a,\n// line\n\nb;",
Expand Down Expand Up @@ -2106,6 +2160,39 @@ ruleTester.run("lines-around-comment", rule, {
output: "foo;\n\n/* fallthrough */",
options: [],
errors: [{ messageId: "before", type: "Block" }]
},
{
code: `
switch (
// this comment is not allowed by allowBlockStart: true
foo
)
{
case 1:
bar();
break;
}
`,
output: `
switch (
// this comment is not allowed by allowBlockStart: true
foo
)
{
case 1:
bar();
break;
}
`,
options: [{
allowBlockStart: true,
beforeLineComment: true,
afterLineComment: true
}],
errors: [{ messageId: "before", type: "Line" }]
}
]

Expand Down

0 comments on commit 0396775

Please sign in to comment.