Skip to content

Commit a408ba4

Browse files
committed
fix(hashbang): Add missing types
1 parent 8dadff5 commit a408ba4

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/rules/hashbang.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict"
66

77
const path = require("path")
8-
const matcher = require("ignore")
8+
const matcher = require("ignore").default
99

1010
const getConvertPath = require("../util/get-convert-path")
1111
const getPackageJson = require("../util/get-package-json")
@@ -16,6 +16,11 @@ const SHEBANG_PATTERN = /^(#!.+?)?(\r)?\n/u
1616
const NODE_SHEBANG_PATTERN =
1717
/^#!\/usr\/bin\/env(?: -\S+)*(?: [^\s=-]+=\S+)* node(?: [^\r\n]+?)?\n/u
1818

19+
/**
20+
* @param {string} filePath
21+
* @param {string} binField
22+
* @returns {boolean}
23+
*/
1924
function simulateNodeResolutionAlgorithm(filePath, binField) {
2025
const possibilities = [filePath]
2126
let newFilePath = filePath.replace(/\.js$/u, "")
@@ -29,32 +34,42 @@ function simulateNodeResolutionAlgorithm(filePath, binField) {
2934
* Checks whether or not a given path is a `bin` file.
3035
*
3136
* @param {string} filePath - A file path to check.
32-
* @param {string|object|undefined} binField - A value of the `bin` field of `package.json`.
37+
* @param {unknown} binField - A value of the `bin` field of `package.json`.
3338
* @param {string} basedir - A directory path that `package.json` exists.
3439
* @returns {boolean} `true` if the file is a `bin` file.
3540
*/
3641
function isBinFile(filePath, binField, basedir) {
3742
if (!binField) {
3843
return false
3944
}
45+
4046
if (typeof binField === "string") {
4147
return simulateNodeResolutionAlgorithm(
4248
filePath,
4349
path.resolve(basedir, binField)
4450
)
4551
}
46-
return Object.keys(binField).some(key =>
47-
simulateNodeResolutionAlgorithm(
48-
filePath,
49-
path.resolve(basedir, binField[key])
50-
)
51-
)
52+
53+
if (binField instanceof Object) {
54+
for (const [value] of Object.values(binField)) {
55+
if (typeof value !== "string") {
56+
continue
57+
}
58+
59+
const resolvedPath = path.resolve(basedir, value)
60+
if (simulateNodeResolutionAlgorithm(filePath, resolvedPath)) {
61+
return true
62+
}
63+
}
64+
}
65+
66+
return false
5267
}
5368

5469
/**
5570
* Gets the shebang line (includes a line ending) from a given code.
5671
*
57-
* @param {SourceCode} sourceCode - A source code object to check.
72+
* @param {import('eslint').SourceCode} sourceCode - A source code object to check.
5873
* @returns {{length: number, bom: boolean, shebang: string, cr: boolean}}
5974
* shebang's information.
6075
* `retv.shebang` is an empty string if shebang doesn't exist.
@@ -128,6 +143,7 @@ module.exports = {
128143
convertedRelativePath
129144
)
130145

146+
/** @type {{ additionalExecutables: string[] }} */
131147
const { additionalExecutables = [] } = context.options?.[0] ?? {}
132148

133149
const executable = matcher()
@@ -155,7 +171,10 @@ module.exports = {
155171
Program() {
156172
const loc = {
157173
start: { line: 1, column: 0 },
158-
end: { line: 1, column: sourceCode.lines.at(0).length },
174+
end: {
175+
line: 1,
176+
column: sourceCode.lines.at(0)?.length ?? 0,
177+
},
159178
}
160179

161180
if (

lib/rules/shebang.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
...hashbang.meta,
1313
deprecated: true,
1414
replacedBy: ["n/hashbang"],
15-
docs: { ...hashbang.meta.docs, recommended: false },
15+
docs: { ...hashbang.meta?.docs, recommended: false },
1616
},
1717
create: hashbang.create,
1818
}

0 commit comments

Comments
 (0)