Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lines-around-comment apply allowBlockStart for switch statements #16153

Merged
merged 5 commits into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/src/rules/lines-around-comment.md
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
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")
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
87 changes: 87 additions & 0 deletions tests/lib/rules/lines-around-comment.js
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