From 14464577a907497d2910a14d3853cdb62ece2835 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 22 Jul 2022 09:15:46 +0530 Subject: [PATCH 1/5] fix: apply `allowBlockStart` for switch statements too --- lib/rules/lines-around-comment.js | 3 ++- tests/lib/rules/lines-around-comment.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 974de21ed1d..1c1a174c407 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -264,7 +264,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..ded745d2315 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -364,6 +364,27 @@ 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 + }] + }, + // check for block end comments { code: "var a,\n// line\n\nb;", From 9347635dcd4e065ebace74f7c3e19bf0eb8a1735 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 22 Jul 2022 09:22:29 +0530 Subject: [PATCH 2/5] docs: add switch statement examples for lines-around-comment-rule --- docs/src/rules/lines-around-comment.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index e78f34b072a..4b5b362eff6 100644 --- a/docs/src/rules/lines-around-comment.md +++ b/docs/src/rules/lines-around-comment.md @@ -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 */ +} ``` ::: From e0d072a09b2ef3fc76a46183447ea2ba939ebf6b Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 23 Jul 2022 16:47:46 +0530 Subject: [PATCH 3/5] fix: check if comment is after the `{` token --- lib/rules/lines-around-comment.js | 12 ++++-- tests/lib/rules/lines-around-comment.js | 50 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 1c1a174c407..2c1b2f40199 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.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; } diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index ded745d2315..9fa1c559bbb 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -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 { @@ -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" }] } ] From 90dead924e9539a3d27a5f8618fcca899fefdc88 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 24 Jul 2022 06:49:16 +0530 Subject: [PATCH 4/5] fix: check for the correct opening brace --- lib/rules/lines-around-comment.js | 4 ++-- tests/lib/rules/lines-around-comment.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 2c1b2f40199..bd7d1cd2662 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -236,8 +236,8 @@ module.exports = { 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 === "{" + parentStartNodeOrToken = sourceCode.getTokenAfter(parent.discriminant, { + filter: astUtils.isOpeningBraceToken }); // opening brace of the switch statement } diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index 9fa1c559bbb..379698549e4 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -401,6 +401,22 @@ ruleTester.run("lines-around-comment", rule, { 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 { From df449cd2eb602ff1473e681aec33df0443f569c8 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 24 Jul 2022 06:51:08 +0530 Subject: [PATCH 5/5] docs: add switch statements for allowBlockEnd and allowBlockStart (lines-around-comment) --- docs/src/rules/lines-around-comment.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index 4b5b362eff6..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