hashbang
UnreleasedConfiguration
Require the correct hashbang for package executables.
The node plugin ports rules from eslint-plugin-n.
Rule Details
This rule finds the nearest package.json for each file. Files listed in its
bin field must start with #!/usr/bin/env node, without a Unicode BOM and with
an LF line ending. Other files must not have a hashbang. Files without a package
are left alone.
For a package with "bin": "./bin/cli.js", this is incorrect in bin/cli.js:
This is correct:
In a regular library file, omit the hashbang. The rule provides automatic fixes to insert, replace or remove the first line and to remove a BOM or CR character. It recognizes hashbangs terminated by LF, including CRLF. It does not change line endings in the rest of the file.
Options
Enable the bundled node plugin and configure any of these options:
ignoreUnpublisheddefaults tofalse. When enabled, usepackage.jsonfiles,.npmignoreand the applicable.gitignoreto skip unpublished files. Files matched byadditionalExecutablesare still checked.additionalExecutablesdefaults to[]. Its ordered Git ignore patterns select additional executable paths relative to the package directory and support!exclusions.executableMapmaps file extensions to interpreter names. Extensions without an entry usenode.convertPathmaps source paths to published paths before checkingbinand publication status. Each array entry hasinclude, optionalexclude, andreplace: [regularExpression, replacement]. The first matching entry wins; replacement uses JavaScript capture substitutions such as$1. An object mapping patterns to replacement pairs is also supported. Omit the option to usesettings.n.convertPath, thensettings.node.convertPath, or no conversion.
Differences from upstream
Compared with eslint-plugin-n, the following cases can change which files need
a hashbang, whether a file is checked, or the result of an automatic fix.
Conversion priority
If several object-form convertPath patterns match, rslint uses alphabetical
pattern order; upstream uses object property order. For example, ** wins over
src/** in rslint even if you wrote src/** first. The resulting path may match
a different bin entry or publication pattern. Use the array form of
convertPath when priority matters.
Corrections to file selection and fixes
- Checking published files. With
ignoreUnpublished: true, rslint keeps checking package-root files such asREADME.jswhen you lint from another directory. A name such as..hidden.jsdoes not put a file outside its package. WhenconvertPathpoints into a nested package, that package'sfilesentries determine publication. Upstream can incorrectly skip the hashbang check in these cases. - Excluding or escaping filename characters. With
additionalExecutables: ["[!b]oo.js"]or["[^b]oo.js"], rslint selectsfoo.jsas an executable and excludesboo.js; upstream missesfoo.js. With["cli\\*"], rslint selects the literal filenamecli*, while upstream also selectscli.js. Similarly,["cli\\?"]selectscli?, which upstream misses, and["a\\b.js"]selectsab.js, where upstream selectsa.js. The same matching differences affectfiles,.npmignoreand.gitignore. - Filenames containing whitespace. Each
filesoradditionalExecutablesentry is one pattern, including any newline, tab or non-breaking space in it. For example, with"files": ["lib/foo.js\nbar.js"]andignoreUnpublished: true, rslint skipslib/foo.js; upstream checks it. Put separate filenames in separate entries. Upstream can also ignore tabs and non-breaking spaces or match ordinary spaces instead. WithadditionalExecutables: ["**/cli.js"], rslint selectscli.jsinside a directory whose name contains a newline; upstream misses it. The directory name difference also affectsfiles,.npmignoreand.gitignore. - Fixing an incomplete first line. If an executable contains only
#!/usr/bin/env nodewithout a final newline, both tools report a missing hashbang. rslint fixes it to a single header ending in LF; upstream duplicates the header and produces invalid JavaScript. rslint also replaces an empty#!or a header ending in CR, U+2028 or U+2029 with a valid header ending in LF.
Invalid patterns and package metadata
- Unclosed brackets or reversed ranges. With
additionalExecutables: ["[cli.js"], rslint treats the literal filename[cli.jsas executable; upstream selects nothing. A reversed range such as[z-a]also matches its literal text in rslint; upstream drops the invalid range and can still match the remaining characters. Close brackets and use ascending ranges such as[a-z]to select the intended files. - A brace range with step zero. With
"files": ["lib/cli{1..3..0}.js"]andignoreUnpublished: true, rslint checkslib/cli1.js; upstream skips it. Use a positive step such as{1..3..1}or list the filenames separately. - An invalid
convertPathregular expression. An expression such as[causes rslint to skip this rule for the file without reporting or fixing a hashbang. Upstream stops with aSyntaxError. Correct the expression so the file is checked. - A non-string
binentry. For"bin": { "cli": false }, rslint ignores that entry; upstream stops with aTypeError. Use a string path so the entry identifies an executable.