diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index 36ce77bf73f..924ba66cf1a 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 64f7f6c5fea..ece43b1417e 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 379698549e4..a55d6fe2719 100644 --- a/tests/lib/rules/lines-around-comment.js +++ b/tests/lib/rules/lines-around-comment.js @@ -1051,6 +1051,25 @@ 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 }] + }, + "#!comment\nvar a = 1;", + { + code: "#!comment\nvar a = 1;", + options: [{}] + }, + { + code: "#!comment\nvar a = 1;", + options: [{ afterHashbangComment: false }] + }, + { + code: "#!comment\nvar a = 1;", + options: [{ afterLineComment: true, afterBlockComment: true }] } ], @@ -2193,6 +2212,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" }] } ]