Skip to content

Commit

Permalink
feat: add afterHashbangComment option
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Feb 22, 2023
1 parent 0700d1b commit 14688e2
Show file tree
Hide file tree
Showing 3 changed files with 55 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:

::: 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 }]
}
],

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

0 comments on commit 14688e2

Please sign in to comment.