From d592f12752f877a1c54120cc87084e322863c2d1 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 22 Feb 2023 12:54:11 +0900 Subject: [PATCH] feat: add `afterHahbangComment` option --- docs/src/rules/lines-around-comment.md | 30 +++++++++++++++++++++++++ lib/rules/lines-around-comment.js | 11 +++++++++ tests/lib/rules/lines-around-comment.js | 14 ++++++++++++ 3 files changed, 55 insertions(+) diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index 36ce77bf73f5..924ba66cf1a4 100644 --- a/docs/src/rules/lines-around-comment.md +++ b/docs/src/rules/lines-around-comment.md @@ -33,6 +33,7 @@ This rule has an object option: * `"allowClassEnd": true` allows comments to appear at the end of classes * `"applyDefaultIgnorePatterns"` enables or disables the default comment patterns to be ignored by the rule * `"ignorePattern"` custom patterns to be ignored by the rule +* `"afterHashbangComment": true` requires an empty line after hashbang comments ### beforeBlockComment @@ -717,6 +718,35 @@ foo(); ::: +### afterHashbangComment + +Examples of **incorrect** code for this rule with the `{ "afterHashbangComment": true }` option: + +::: incorrect + +```js +#!foo +var day = "great" + +/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */ +``` + +::: + +Examples of **correct** code for this rule with the `{ "afterHashbangComment": true }` option: + +::: correct + +```js +#!foo + +var day = "great" + +/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */ +``` + +::: + ## When Not To Use It Many people enjoy a terser code style and don't mind comments bumping up against code. If you fall into that category this rule is not for you. diff --git a/lib/rules/lines-around-comment.js b/lib/rules/lines-around-comment.js index 64f7f6c5fea7..ece43b1417e0 100644 --- a/lib/rules/lines-around-comment.js +++ b/lib/rules/lines-around-comment.js @@ -113,6 +113,10 @@ module.exports = { }, applyDefaultIgnorePatterns: { type: "boolean" + }, + afterHashbangComment: { + type: "boolean", + default: false } }, additionalProperties: false @@ -449,6 +453,13 @@ module.exports = { before: options.beforeBlockComment }); } + } else if (token.type === "Shebang") { + if (options.afterHashbangComment) { + checkForEmptyLine(token, { + after: options.afterHashbangComment, + before: false + }); + } } }); } diff --git a/tests/lib/rules/lines-around-comment.js b/tests/lib/rules/lines-around-comment.js index 379698549e44..2101e6cf1687 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -1051,6 +1051,12 @@ ruleTester.run("lines-around-comment", rule, { { code: "foo\n/* this is pragmatic */", options: [{ applyDefaultIgnorePatterns: false, ignorePattern: "pragma" }] + }, + + // Hashbang comment + { + code: "#!comment\n\nvar a = 1;", + options: [{ afterHashbangComment: true }] } ], @@ -2193,6 +2199,14 @@ ruleTester.run("lines-around-comment", rule, { afterLineComment: true }], errors: [{ messageId: "before", type: "Line" }] + }, + + // Hashbang comment + { + code: "#!foo\nvar a = 1;", + output: "#!foo\n\nvar a = 1;", + options: [{ afterHashbangComment: true }], + errors: [{ messageId: "after", type: "Shebang" }] } ]