Skip to content

Commit

Permalink
feat: ES2023 no-hashbang-comment (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jul 23, 2024
1 parent 30f7398 commit 65941ac
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ for configuration. Here's some examples:
- [no-dynamic-imports](./docs/no-dynamic-imports.md)
- [no-edge-destructure-bug](./docs/no-edge-destructure-bug.md)
- [no-exponentiation-operator](./docs/no-exponentiation-operator.md)
- [no-hashbang-comment](./docs/no-hashbang-comment.md)
- [no-logical-assignment-operator](./docs/no-logical-assignment-operator.md)
- [no-nullish-coalescing](./docs/no-nullish-coalescing.md)
- [no-numeric-separators](./docs/no-numeric-separators.md)
Expand Down
19 changes: 19 additions & 0 deletions docs/no-hashbang-comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# no-hashbang-comment

This prevents use of ES2023 hashang comments:

```js
#!/usr/bin/env node
```

These will not be allowed because they are not supported in the following browsers:

- Edge < 79
- Safari < 13.1
- Firefox < 67
- Chrome < 74


## What is the Fix?

You have to omit the comment.
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ createRule(
{ ts: 2022 }
);

// ES2023
createRule(
"no-hashbang-comment",
"edge < 79, safari < 13.1, firefox < 67, chrome < 74",
"disallow hashbang comments",
{ ts: 2022 }
);

// Proposals...
createRule(
"no-do-expression",
Expand All @@ -230,7 +238,7 @@ createRule(

module.exports.configs.recommended = {
plugins: ["escompat"],
parserOptions: { ecmaVersion: 2020 },
parserOptions: { ecmaVersion: 2023 },
rules: Object.keys(module.exports.rules).reduce(
(o, r) => ((o["escompat/" + r] = ["error"]), o),
{}
Expand All @@ -242,7 +250,7 @@ module.exports.configs["flat/recommended"] = {
escompat: module.exports
},
languageOptions: {
ecmaVersion: 2020
ecmaVersion: 2023
},
rules: Object.keys(module.exports.rules).reduce(
(o, r) => ((o["escompat/" + r] = ["error"]), o),
Expand Down
13 changes: 13 additions & 0 deletions lib/rules/no-hashbang-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

module.exports = (context, badBrowser) => {
const { sourceCode = context.getSourceCode() } = context;
return {
'Program:exit' (node) {
const [comment] = sourceCode.getAllComments();
if (comment.type === 'Shebang') {
context.report(node, `Hashbang comments are not supported in ${badBrowser}`)
}
}
}
}
2 changes: 1 addition & 1 deletion test/check-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('documentation', () => {
let consume = true
const headings = contents.filter(line => {
// Discard lines that aren't headers or thumbs
if (!(line.startsWith('#') || line.startsWith('\ud83d'))) return false
if (!(line.startsWith('#') || line.startsWith('\ud83d')) || line.startsWith('#!')) return false
// Ignore all sub headings/thumbs between `### Options` and `## When Not To Use It`
if (line === '### Options') {
consume = false
Expand Down
24 changes: 24 additions & 0 deletions test/no-hashbang-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const rule = require('../lib/index').rules['no-hashbang-comment']
const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester({languageOptions: {ecmaVersion: 2018}})

ruleTester.run('no-hashbang-comment', rule, {
valid: [
{code: '// Regular comment'},
{code: '/* Regular comment */'},
],
invalid: [
{
code: '#!/usr/bin/env node',
errors: [
{
message:
'Hashbang comments are not supported in undefined'
}
]
}
]
})

0 comments on commit 65941ac

Please sign in to comment.