Skip to content

Commit 0eb60fd

Browse files
authored
Add no-hashbang rule (#33)
1 parent 7f3a71c commit 0eb60fd

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

docs/rules/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ This plugin provides the following rules.
44

55
- 🔧 mark means that the `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by the rule.
66

7+
## ES2023
8+
9+
There is a config that enables the rules in this category: `plugin:es-x/no-new-in-esnext`
10+
11+
| Rule ID | Description | |
12+
|:--------|:------------|:--:|
13+
| [es-x/no-hashbang](./no-hashbang.md) | disallow Hashbang comments. | |
14+
715
## ES2022
816

917
There are multiple configs that enable all rules in this category: `plugin:es-x/no-new-in-es2022`, `plugin:es-x/restrict-to-es3`, `plugin:es-x/restrict-to-es5`, `plugin:es-x/restrict-to-es2015`, `plugin:es-x/restrict-to-es2016`, `plugin:es-x/restrict-to-es2017`, `plugin:es-x/restrict-to-es2018`, `plugin:es-x/restrict-to-es2019`, `plugin:es-x/restrict-to-es2020`, and `plugin:es-x/restrict-to-es2021`

docs/rules/no-hashbang.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "es-x/no-hashbang"
3+
description: "disallow Hashbang comments"
4+
---
5+
6+
# es-x/no-hashbang
7+
> disallow Hashbang comments
8+
9+
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
10+
- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-esnext`
11+
12+
This rule reports ES2023 Hashbang comment as errors.
13+
14+
## 💡 Examples
15+
16+
⛔ Examples of **incorrect** code for this rule:
17+
18+
<eslint-playground type="bad">
19+
20+
```js
21+
#!/usr/bin/env node
22+
/*eslint es-x/no-hashbang: error */
23+
```
24+
25+
</eslint-playground>
26+
27+
## 📚 References
28+
29+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/lib/rules/no-hashbang.js)
30+
- [Test source](https://github.com/ota-meshi/eslint-plugin-es-x/blob/master/tests/lib/rules/no-hashbang.js)

lib/configs/no-new-in-esnext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
*/
55
"use strict"
66

7-
module.exports = { plugins: ["es-x"], rules: {} }
7+
module.exports = { plugins: ["es-x"], rules: { "es-x/no-hashbang": "error" } }

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module.exports = {
103103
"no-function-prototype-bind": require("./rules/no-function-prototype-bind"),
104104
"no-generators": require("./rules/no-generators"),
105105
"no-global-this": require("./rules/no-global-this"),
106+
"no-hashbang": require("./rules/no-hashbang"),
106107
"no-import-meta": require("./rules/no-import-meta"),
107108
"no-initializers-in-for-in": require("./rules/no-initializers-in-for-in"),
108109
"no-json": require("./rules/no-json"),

lib/rules/no-hashbang.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict"
2+
3+
module.exports = {
4+
meta: {
5+
docs: {
6+
description: "disallow Hashbang comments.",
7+
category: "ES2023",
8+
recommended: false,
9+
url: "http://ota-meshi.github.io/eslint-plugin-es-x/rules/no-hashbang.html",
10+
},
11+
fixable: null,
12+
messages: {
13+
forbidden: "ES2023 Hashbang comments are forbidden.",
14+
},
15+
schema: [],
16+
type: "problem",
17+
},
18+
create(context) {
19+
return {
20+
Program() {
21+
const firstComment = context.getSourceCode().ast.comments[0]
22+
if (firstComment && firstComment.type === "Shebang") {
23+
context.report({
24+
node: firstComment,
25+
messageId: "forbidden",
26+
})
27+
}
28+
},
29+
}
30+
},
31+
}

tests/lib/rules/no-hashbang.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use strict"
2+
3+
const RuleTester = require("../../tester")
4+
const rule = require("../../../lib/rules/no-hashbang.js")
5+
6+
if (!RuleTester.isSupported(2023)) {
7+
//eslint-disable-next-line no-console
8+
console.log("Skip the tests of no-hashbang.")
9+
return
10+
}
11+
12+
new RuleTester().run("no-hashbang", rule, {
13+
valid: [
14+
"/* comment */",
15+
"/** comment */",
16+
"// comment",
17+
`call() /** comment */
18+
// comment
19+
`,
20+
],
21+
invalid: [
22+
{
23+
code: `#!/usr/bin/env node
24+
// in the Script Goal
25+
'use strict';
26+
console.log(1);
27+
`,
28+
errors: ["ES2023 Hashbang comments are forbidden."],
29+
},
30+
{
31+
code: `#!/usr/bin/env node
32+
// in the Module Goal
33+
export {};
34+
console.log(1);
35+
`,
36+
parserOptions: { sourceType: "module" },
37+
errors: ["ES2023 Hashbang comments are forbidden."],
38+
},
39+
],
40+
})

tests/tester.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const semver = require("semver")
1111
const eslintVersion = new Linter().version
1212
const ecmaVersion =
1313
/*eslint-disable prettier/prettier */
14+
semver.gte(eslintVersion, "8.23.0") ? 2023 :
1415
semver.gte(eslintVersion, "8.0.0") ? 2022 :
1516
semver.gte(eslintVersion, "7.8.0") ? 2021 :
1617
semver.gte(eslintVersion, "6.2.0") ? 2020 :

0 commit comments

Comments
 (0)