diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index e78f34b072a..44051246faa 100644 --- a/docs/src/rules/lines-around-comment.md +++ b/docs/src/rules/lines-around-comment.md @@ -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 @@ -230,6 +230,14 @@ class C { foo(); } } + +switch (foo) { + /* what a great and wonderful day */ + + case 1: + bar(); + break; +} ``` ::: @@ -308,6 +316,14 @@ class C { /* what a great and wonderful day */ } + +switch (foo) { + case 1: + bar(); + break; + + /* what a great and wonderful day */ +} ``` ::: diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 974de21ed1d..bd7d1cd2662 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -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; } @@ -264,7 +270,8 @@ module.exports = { isCommentAtParentStart(token, "ClassBody") || isCommentAtParentStart(token, "BlockStatement") || isCommentAtParentStart(token, "StaticBlock") || - isCommentAtParentStart(token, "SwitchCase") + isCommentAtParentStart(token, "SwitchCase") || + isCommentAtParentStart(token, "SwitchStatement") ); } diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index 2687f1458aa..379698549e4 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -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;", @@ -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" }] } ]