forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
prefer-node-protocol.js
190 lines (170 loc) · 6.04 KB
/
prefer-node-protocol.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @author Yusuke Iinuma
* See LICENSE file in root directory for full license.
*/
"use strict"
const { Range } = require("semver")
const getConfiguredNodeVersion = require("../util/get-configured-node-version")
const visitImport = require("../util/visit-import")
const visitRequire = require("../util/visit-require")
const mergeVisitorsInPlace = require("../util/merge-visitors-in-place")
const {
NodeBuiltinModules,
} = require("../unsupported-features/node-builtins.js")
/**
* @param {string} name The name of the node module
* @returns {boolean}
*/
function isBuiltin(name) {
return Object.hasOwn(NodeBuiltinModules, name)
}
const messageId = "preferNodeProtocol"
const supportedRangeForEsm = new Range("^12.20.0 || >= 14.13.1")
const supportedRangeForCjs = new Range("^14.18.0 || >= 16.0.0")
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description:
"enforce using the `node:` protocol when importing Node.js builtin modules.",
recommended: false,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/prefer-node-protocol.md",
},
fixable: "code",
messages: {
[messageId]: "Prefer `node:{{moduleName}}` over `{{moduleName}}`.",
},
schema: [
{
type: "object",
properties: {
version: getConfiguredNodeVersion.schema,
},
additionalProperties: false,
},
],
type: "suggestion",
},
create(context) {
/**
* @param {import('estree').Node} node
* @param {object} options
* @param {string} options.name
* @param {number} options.argumentsLength
* @returns {node is import('estree').CallExpression}
*/
function isCallExpression(node, { name, argumentsLength }) {
if (node?.type !== "CallExpression") {
return false
}
if (node.optional) {
return false
}
if (node.arguments.length !== argumentsLength) {
return false
}
if (
node.callee.type !== "Identifier" ||
node.callee.name !== name
) {
return false
}
return true
}
/**
* @param {import('estree').Node} [node]
* @returns {node is import('estree').Literal}
*/
function isStringLiteral(node) {
return node?.type === "Literal" && typeof node.type === "string"
}
/**
* @param {import('estree').Node | undefined} node
* @returns {node is import('estree').CallExpression}
*/
function isStaticRequire(node) {
return (
node != null &&
isCallExpression(node, {
name: "require",
argumentsLength: 1,
}) &&
isStringLiteral(node.arguments[0])
)
}
/**
* @param {import('eslint').Rule.RuleContext} context
* @param {import('../util/import-target.js').ModuleStyle} moduleStyle
* @returns {boolean}
*/
function isEnablingThisRule(context, moduleStyle) {
const version = getConfiguredNodeVersion(context)
// Only check Node.js version because this rule is meaningless if configured Node.js version doesn't match semver range.
if (!version.intersects(supportedRangeForEsm)) {
return false
}
// Only check when using `require`
if (
moduleStyle === "require" &&
!version.intersects(supportedRangeForCjs)
) {
return false
}
return true
}
/** @type {import('../util/import-target.js')[]} */
const targets = []
return [
visitImport(context, { includeCore: true }, importTargets => {
targets.push(...importTargets)
}),
visitRequire(context, { includeCore: true }, requireTargets => {
targets.push(
...requireTargets.filter(target =>
isStaticRequire(target.node.parent)
)
)
}),
{
"Program:exit"() {
for (const { node, moduleStyle } of targets) {
if (!isEnablingThisRule(context, moduleStyle)) {
continue
}
if (node.type === "TemplateLiteral") {
continue
}
if (
!("value" in node) ||
typeof node.value !== "string" ||
node.value.startsWith("node:") ||
!isBuiltin(node.value) ||
!isBuiltin(`node:${node.value}`)
) {
continue
}
context.report({
node,
messageId,
data: {
moduleName: node.value,
},
fix(fixer) {
const firstCharacterIndex =
(node?.range?.[0] ?? 0) + 1
return fixer.replaceTextRange(
[firstCharacterIndex, firstCharacterIndex],
"node:"
)
},
})
}
},
},
].reduce(
(mergedVisitor, thisVisitor) =>
mergeVisitorsInPlace(mergedVisitor, thisVisitor),
{}
)
},
}