Skip to content

Commit 65941ac

Browse files
authored
feat: ES2023 no-hashbang-comment (#29)
1 parent 30f7398 commit 65941ac

6 files changed

Lines changed: 68 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ for configuration. Here's some examples:
153153
- [no-dynamic-imports](./docs/no-dynamic-imports.md)
154154
- [no-edge-destructure-bug](./docs/no-edge-destructure-bug.md)
155155
- [no-exponentiation-operator](./docs/no-exponentiation-operator.md)
156+
- [no-hashbang-comment](./docs/no-hashbang-comment.md)
156157
- [no-logical-assignment-operator](./docs/no-logical-assignment-operator.md)
157158
- [no-nullish-coalescing](./docs/no-nullish-coalescing.md)
158159
- [no-numeric-separators](./docs/no-numeric-separators.md)

docs/no-hashbang-comment.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# no-hashbang-comment
2+
3+
This prevents use of ES2023 hashang comments:
4+
5+
```js
6+
#!/usr/bin/env node
7+
```
8+
9+
These will not be allowed because they are not supported in the following browsers:
10+
11+
- Edge < 79
12+
- Safari < 13.1
13+
- Firefox < 67
14+
- Chrome < 74
15+
16+
17+
## What is the Fix?
18+
19+
You have to omit the comment.

lib/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ createRule(
211211
{ ts: 2022 }
212212
);
213213

214+
// ES2023
215+
createRule(
216+
"no-hashbang-comment",
217+
"edge < 79, safari < 13.1, firefox < 67, chrome < 74",
218+
"disallow hashbang comments",
219+
{ ts: 2022 }
220+
);
221+
214222
// Proposals...
215223
createRule(
216224
"no-do-expression",
@@ -230,7 +238,7 @@ createRule(
230238

231239
module.exports.configs.recommended = {
232240
plugins: ["escompat"],
233-
parserOptions: { ecmaVersion: 2020 },
241+
parserOptions: { ecmaVersion: 2023 },
234242
rules: Object.keys(module.exports.rules).reduce(
235243
(o, r) => ((o["escompat/" + r] = ["error"]), o),
236244
{}
@@ -242,7 +250,7 @@ module.exports.configs["flat/recommended"] = {
242250
escompat: module.exports
243251
},
244252
languageOptions: {
245-
ecmaVersion: 2020
253+
ecmaVersion: 2023
246254
},
247255
rules: Object.keys(module.exports.rules).reduce(
248256
(o, r) => ((o["escompat/" + r] = ["error"]), o),

lib/rules/no-hashbang-comment.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports = (context, badBrowser) => {
4+
const { sourceCode = context.getSourceCode() } = context;
5+
return {
6+
'Program:exit' (node) {
7+
const [comment] = sourceCode.getAllComments();
8+
if (comment.type === 'Shebang') {
9+
context.report(node, `Hashbang comments are not supported in ${badBrowser}`)
10+
}
11+
}
12+
}
13+
}

test/check-rules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('documentation', () => {
9999
let consume = true
100100
const headings = contents.filter(line => {
101101
// Discard lines that aren't headers or thumbs
102-
if (!(line.startsWith('#') || line.startsWith('\ud83d'))) return false
102+
if (!(line.startsWith('#') || line.startsWith('\ud83d')) || line.startsWith('#!')) return false
103103
// Ignore all sub headings/thumbs between `### Options` and `## When Not To Use It`
104104
if (line === '### Options') {
105105
consume = false

test/no-hashbang-comment.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const rule = require('../lib/index').rules['no-hashbang-comment']
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2018}})
7+
8+
ruleTester.run('no-hashbang-comment', rule, {
9+
valid: [
10+
{code: '// Regular comment'},
11+
{code: '/* Regular comment */'},
12+
],
13+
invalid: [
14+
{
15+
code: '#!/usr/bin/env node',
16+
errors: [
17+
{
18+
message:
19+
'Hashbang comments are not supported in undefined'
20+
}
21+
]
22+
}
23+
]
24+
})

0 commit comments

Comments
 (0)