5
5
"use strict"
6
6
7
7
const path = require ( "path" )
8
- const matcher = require ( "ignore" )
8
+ const matcher = require ( "ignore" ) . default
9
9
10
10
const getConvertPath = require ( "../util/get-convert-path" )
11
11
const getPackageJson = require ( "../util/get-package-json" )
@@ -16,6 +16,11 @@ const SHEBANG_PATTERN = /^(#!.+?)?(\r)?\n/u
16
16
const NODE_SHEBANG_PATTERN =
17
17
/ ^ # ! \/ u s r \/ b i n \/ e n v (?: - \S + ) * (?: [ ^ \s = - ] + = \S + ) * n o d e (?: [ ^ \r \n ] + ?) ? \n / u
18
18
19
+ /**
20
+ * @param {string } filePath
21
+ * @param {string } binField
22
+ * @returns {boolean }
23
+ */
19
24
function simulateNodeResolutionAlgorithm ( filePath , binField ) {
20
25
const possibilities = [ filePath ]
21
26
let newFilePath = filePath . replace ( / \. j s $ / u, "" )
@@ -29,32 +34,42 @@ function simulateNodeResolutionAlgorithm(filePath, binField) {
29
34
* Checks whether or not a given path is a `bin` file.
30
35
*
31
36
* @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`.
33
38
* @param {string } basedir - A directory path that `package.json` exists.
34
39
* @returns {boolean } `true` if the file is a `bin` file.
35
40
*/
36
41
function isBinFile ( filePath , binField , basedir ) {
37
42
if ( ! binField ) {
38
43
return false
39
44
}
45
+
40
46
if ( typeof binField === "string" ) {
41
47
return simulateNodeResolutionAlgorithm (
42
48
filePath ,
43
49
path . resolve ( basedir , binField )
44
50
)
45
51
}
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
52
67
}
53
68
54
69
/**
55
70
* Gets the shebang line (includes a line ending) from a given code.
56
71
*
57
- * @param {SourceCode } sourceCode - A source code object to check.
72
+ * @param {import('eslint'). SourceCode } sourceCode - A source code object to check.
58
73
* @returns {{length: number, bom: boolean, shebang: string, cr: boolean} }
59
74
* shebang's information.
60
75
* `retv.shebang` is an empty string if shebang doesn't exist.
@@ -128,6 +143,7 @@ module.exports = {
128
143
convertedRelativePath
129
144
)
130
145
146
+ /** @type {{ additionalExecutables: string[] } } */
131
147
const { additionalExecutables = [ ] } = context . options ?. [ 0 ] ?? { }
132
148
133
149
const executable = matcher ( )
@@ -155,7 +171,10 @@ module.exports = {
155
171
Program ( ) {
156
172
const loc = {
157
173
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
+ } ,
159
178
}
160
179
161
180
if (
0 commit comments