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 3 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
16 changes: 16 additions & 0 deletions docs/src/rules/lines-around-comment.md
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.getFirstToken(parent, {
filter: t => t.type === "Punctuator" && t.value === "{"
}); // opening brace of the switch statement
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}

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
71 changes: 71 additions & 0 deletions tests/lib/rules/lines-around-comment.js
Expand Up @@ -364,6 +364,44 @@ 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
}]
},

// check for block end comments
{
code: "var a,\n// line\n\nb;",
Expand Down Expand Up @@ -2106,6 +2144,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