1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# node/shebang
> suggest correct usage of shebang
> - ⭐️ This rule is included in `plugin:node/recommended` preset.
> - ✒️ 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 this rule.
When we make a CLI tool with Node.js, we add `bin` field to `package.json`, then we add a shebang the entry file.
This rule suggests correct usage of shebang.
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
## 📖 Rule Details
This rule looks up `package.json` file from each linting target file.
Starting from the directory of the target file, it goes up ancestor directories until found.
If `package.json` was not found, this rule does nothing.
This rule checks `bin` field of `package.json`, then if a target file matches one of `bin` files, it checks whether or not there is a correct shebang.
Otherwise it checks whether or not there is not a shebang.
The following patterns are considered problems for files in `bin` field of `package.json`:
```js
console.log("hello"); /*error This file needs shebang "#!/usr/bin/env node".*/
```
```js
#!/usr/bin/env node /*error This file must not have Unicode BOM.*/
console.log("hello");
// If this file has Unicode BOM.
```
```js
#!/usr/bin/env node /*error This file must have Unix linebreaks (LF).*/
console.log("hello");
// If this file has Windows' linebreaks (CRLF).
```
The following patterns are considered problems for other files:
```js
#!/usr/bin/env node /*error This file needs no shebang.*/
console.log("hello");
```
The following patterns are not considered problems for files in `bin` field of `package.json`:
```js
#!/usr/bin/env node
console.log("hello");
```
The following patterns are not considered problems for other files:
```js
console.log("hello");
```
### Options
```json
{
"node/shebang": ["error", {"convertPath": null}]
}
```
#### convertPath
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never handled as a bin file.
`convertPath` option tells to the rule, it needs to convert file paths.
For example:
```json
{
"rules": {
"node/shebang": ["error", {
"convertPath": {
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
}
}]
}
}
```
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]`
`targetFiles` is a glob pattern.
It converts paths which are matched to the pattern with the following way.
```js
path.replace(new RegExp(fromRegExp), toString);
```
So on this example, `src/a/foo.jsx` is handled as `lib/a/foo.js`.
The `convertPath` option can be an array as well.
For example:
```json
{
"rules": {
"node/shebang": ["error", {
"convertPath": [
{
"include": ["src/**/*.js"],
"exclude": ["**/*.spec.js"],
"replace": ["^src/(.+)$", "lib/$1"]
}
]
}]
}
}
```
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
In addition, we can specify glob patterns to exclude files.
### Shared Settings
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
Several rules have the same option, but we can set this option at once.
- `convertPath`
For Example:
```json
{
"settings": {
"node": {
"convertPath": {
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
}
}
},
"rules": {
"node/shebang": "error"
}
}
```
## 🔎 Implementation
- [Rule source](../../lib/rules/shebang.js)
- [Test source](../../tests/lib/rules/shebang.js)
|