Skip to content

Commit

Permalink
fix: check if comment is after the { token
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jul 23, 2022
1 parent 9347635 commit e0d072a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
12 changes: 9 additions & 3 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
}

return token.loc.start.line - parentStartNodeOrToken.loc.start.line === 1;
}
Expand Down
50 changes: 50 additions & 0 deletions tests/lib/rules/lines-around-comment.js
Expand Up @@ -384,6 +384,23 @@ ruleTester.run("lines-around-comment", rule, {
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
{
Expand Down Expand Up @@ -2127,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

0 comments on commit e0d072a

Please sign in to comment.