Skip to content

Commit 8e34a04

Browse files
feat: add afterHashbangComment option to lines-around-comment rule (#16920)
* feat: add `afterHashbangComment` option * test: add more tests Co-authored-by: Milos Djermanovic <[email protected]> --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent c8c0c71 commit 8e34a04

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/src/rules/lines-around-comment.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This rule has an object option:
3333
* `"allowClassEnd": true` allows comments to appear at the end of classes
3434
* `"applyDefaultIgnorePatterns"` enables or disables the default comment patterns to be ignored by the rule
3535
* `"ignorePattern"` custom patterns to be ignored by the rule
36+
* `"afterHashbangComment": true` requires an empty line after hashbang comments
3637

3738
### beforeBlockComment
3839

@@ -717,6 +718,35 @@ foo();
717718

718719
:::
719720

721+
### afterHashbangComment
722+
723+
Examples of **incorrect** code for this rule with the `{ "afterHashbangComment": true }` option:
724+
725+
::: incorrect
726+
727+
```js
728+
#!foo
729+
var day = "great"
730+
731+
/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */
732+
```
733+
734+
:::
735+
736+
Examples of **correct** code for this rule with the `{ "afterHashbangComment": true }` option:
737+
738+
::: correct
739+
740+
```js
741+
#!foo
742+
743+
var day = "great"
744+
745+
/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */
746+
```
747+
748+
:::
749+
720750
## When Not To Use It
721751

722752
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.

lib/rules/lines-around-comment.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ module.exports = {
113113
},
114114
applyDefaultIgnorePatterns: {
115115
type: "boolean"
116+
},
117+
afterHashbangComment: {
118+
type: "boolean",
119+
default: false
116120
}
117121
},
118122
additionalProperties: false
@@ -449,6 +453,13 @@ module.exports = {
449453
before: options.beforeBlockComment
450454
});
451455
}
456+
} else if (token.type === "Shebang") {
457+
if (options.afterHashbangComment) {
458+
checkForEmptyLine(token, {
459+
after: options.afterHashbangComment,
460+
before: false
461+
});
462+
}
452463
}
453464
});
454465
}

tests/lib/rules/lines-around-comment.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,25 @@ ruleTester.run("lines-around-comment", rule, {
10511051
{
10521052
code: "foo\n/* this is pragmatic */",
10531053
options: [{ applyDefaultIgnorePatterns: false, ignorePattern: "pragma" }]
1054+
},
1055+
1056+
// Hashbang comment
1057+
{
1058+
code: "#!comment\n\nvar a = 1;",
1059+
options: [{ afterHashbangComment: true }]
1060+
},
1061+
"#!comment\nvar a = 1;",
1062+
{
1063+
code: "#!comment\nvar a = 1;",
1064+
options: [{}]
1065+
},
1066+
{
1067+
code: "#!comment\nvar a = 1;",
1068+
options: [{ afterHashbangComment: false }]
1069+
},
1070+
{
1071+
code: "#!comment\nvar a = 1;",
1072+
options: [{ afterLineComment: true, afterBlockComment: true }]
10541073
}
10551074
],
10561075

@@ -2193,6 +2212,14 @@ ruleTester.run("lines-around-comment", rule, {
21932212
afterLineComment: true
21942213
}],
21952214
errors: [{ messageId: "before", type: "Line" }]
2215+
},
2216+
2217+
// Hashbang comment
2218+
{
2219+
code: "#!foo\nvar a = 1;",
2220+
output: "#!foo\n\nvar a = 1;",
2221+
options: [{ afterHashbangComment: true }],
2222+
errors: [{ messageId: "after", type: "Shebang" }]
21962223
}
21972224
]
21982225

0 commit comments

Comments
 (0)