Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add afterHashbangComment option to lines-around-comment rule #16920

Merged
merged 2 commits into from Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/src/rules/lines-around-comment.md
Expand Up @@ -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

Expand Down Expand Up @@ -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 }] */
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
```

:::

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.
11 changes: 11 additions & 0 deletions lib/rules/lines-around-comment.js
Expand Up @@ -113,6 +113,10 @@ module.exports = {
},
applyDefaultIgnorePatterns: {
type: "boolean"
},
afterHashbangComment: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand Down Expand Up @@ -449,6 +453,13 @@ module.exports = {
before: options.beforeBlockComment
});
}
} else if (token.type === "Shebang") {
if (options.afterHashbangComment) {
checkForEmptyLine(token, {
after: options.afterHashbangComment,
before: false
});
}
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/lines-around-comment.js
Expand Up @@ -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 }]
}
sosukesuzuki marked this conversation as resolved.
Show resolved Hide resolved
],

Expand Down Expand Up @@ -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" }]
}
]

Expand Down