@@ -13,6 +13,14 @@ const mergeVisitorsInPlace = require("../util/merge-visitors-in-place")
13
13
14
14
const messageId = "preferNodeProtocol"
15
15
16
+ const supportedRangeForEsm = /** @type {import('semver').Range } */ (
17
+ getSemverRange ( "^12.20.0 || >= 14.13.1" )
18
+ )
19
+ const supportedRangeForCjs = /** @type {import('semver').Range } */ (
20
+ getSemverRange ( "^14.18.0 || >= 16.0.0" )
21
+ )
22
+
23
+ /** @type {import('eslint').Rule.RuleModule } */
16
24
module . exports = {
17
25
meta : {
18
26
docs : {
@@ -37,6 +45,13 @@ module.exports = {
37
45
type : "suggestion" ,
38
46
} ,
39
47
create ( context ) {
48
+ /**
49
+ * @param {import('estree').Node } node
50
+ * @param {object } options
51
+ * @param {string } options.name
52
+ * @param {number } options.argumentsLength
53
+ * @returns {node is import('estree').CallExpression }
54
+ */
40
55
function isCallExpression ( node , { name, argumentsLength } ) {
41
56
if ( node ?. type !== "CallExpression" ) {
42
57
return false
@@ -60,40 +75,54 @@ module.exports = {
60
75
return true
61
76
}
62
77
78
+ /**
79
+ * @param {import('estree').Node } node
80
+ * @returns {node is import('estree').Literal }
81
+ */
63
82
function isStringLiteral ( node ) {
64
83
return node ?. type === "Literal" && typeof node . type === "string"
65
84
}
66
85
86
+ /**
87
+ * @param {import('estree').Node | undefined } node
88
+ * @returns {node is import('estree').CallExpression }
89
+ */
67
90
function isStaticRequire ( node ) {
68
91
return (
92
+ node != null &&
69
93
isCallExpression ( node , {
70
94
name : "require" ,
71
95
argumentsLength : 1 ,
72
- } ) && isStringLiteral ( node . arguments [ 0 ] )
96
+ } ) &&
97
+ isStringLiteral ( node . arguments [ 0 ] )
73
98
)
74
99
}
75
100
101
+ /**
102
+ * @param {import('eslint').Rule.RuleContext } context
103
+ * @param {import('../util/import-target.js').ModuleStyle } moduleStyle
104
+ * @returns {Boolean }
105
+ */
76
106
function isEnablingThisRule ( context , moduleStyle ) {
77
107
const version = getConfiguredNodeVersion ( context )
78
108
79
- const supportedVersionForEsm = "^12.20.0 || >= 14.13.1"
80
109
// Only check Node.js version because this rule is meaningless if configured Node.js version doesn't match semver range.
81
- if ( ! version . intersects ( getSemverRange ( supportedVersionForEsm ) ) ) {
110
+ if ( ! version . intersects ( supportedRangeForEsm ) ) {
82
111
return false
83
112
}
84
113
85
- const supportedVersionForCjs = "^14.18.0 || >= 16.0.0"
86
114
// Only check when using `require`
87
115
if (
88
116
moduleStyle === "require" &&
89
- ! version . intersects ( getSemverRange ( supportedVersionForCjs ) )
117
+ ! version . intersects ( supportedRangeForCjs )
90
118
) {
91
119
return false
92
120
}
93
121
94
122
return true
95
123
}
96
124
125
+ /** @type {import('../util/import-target.js')[] } */
97
126
const targets = [ ]
98
127
return [
99
128
visitImport ( context , { includeCore : true } , importTargets => {
@@ -117,7 +146,7 @@ module.exports = {
117
146
continue
118
147
}
119
148
120
- const { value } = node
149
+ const { value } = /** @type { { value: string } }*/ ( node )
121
150
if (
122
151
typeof value !== "string" ||
123
152
value . startsWith ( "node:" ) ||
@@ -134,7 +163,8 @@ module.exports = {
134
163
moduleName : value ,
135
164
} ,
136
165
fix ( fixer ) {
137
- const firstCharacterIndex = node . range [ 0 ] + 1
166
+ const firstCharacterIndex =
167
+ ( node ?. range ?. [ 0 ] ?? 0 ) + 1
138
168
return fixer . replaceTextRange (
139
169
[ firstCharacterIndex , firstCharacterIndex ] ,
140
170
"node:"
0 commit comments