Skip to content

Commit

Permalink
feat: add afterHashbangComment option to lines-around-comment rule (
Browse files Browse the repository at this point in the history
#16920)

* feat: add `afterHashbangComment` option

* test: add more tests

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
sosukesuzuki and mdjermanovic committed Feb 24, 2023
1 parent c8c0c71 commit 8e34a04
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
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 }] */
```

:::

Examples of **correct** code for this rule with the `{ "afterHashbangComment": true }` option:

This comment has been minimized.

Copy link
@Romick2005

Romick2005 Feb 27, 2023

What is the difference between correct and incorrect use cases?


::: 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
27 changes: 27 additions & 0 deletions tests/lib/rules/lines-around-comment.js
Expand Up @@ -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 }]
}
],

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

Expand Down

0 comments on commit 8e34a04

Please sign in to comment.