forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcheck-unsupported-builtins.js
169 lines (148 loc) · 5.01 KB
/
check-unsupported-builtins.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
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
const { rsort } = require("semver")
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
const getConfiguredNodeVersion = require("./get-configured-node-version")
const getSemverRange = require("./get-semver-range")
const unprefixNodeColon = require("./unprefix-node-colon")
const semverRangeSubset = require("semver/ranges/subset")
const { getScope } = require("../util/eslint-compat")
/**
* Parses the options.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @returns {Readonly<{
* version: import('semver').Range;
* ignores: Set<string>;
* allowExperimental: boolean;
* }>} Parsed value.
*/
function parseOptions(context) {
const raw = context.options[0] || {}
const version = getConfiguredNodeVersion(context)
const ignores = new Set(raw.ignores || [])
const allowExperimental = raw.allowExperimental ?? false
return Object.freeze({ version, ignores, allowExperimental })
}
/**
* Check if it has been supported.
* @param {string[] | undefined} featureRange The target features supported range
* @param {import('semver').Range} requestedRange The configured version range.
* @returns {boolean}
*/
function isInRange(featureRange, requestedRange) {
if (featureRange == null || featureRange.length === 0) {
return false
}
const [latest] = rsort(featureRange)
const range = getSemverRange(
[...featureRange.map(version => `^${version}`), `>= ${latest}`].join(
"||"
)
)
if (range == null) {
return false
}
return semverRangeSubset(requestedRange, range)
}
/**
* Get the formatted text of a given supported version.
* @param {string[] | undefined} versions The support info.
* @returns {string | undefined}
*/
function versionsToString(versions) {
if (versions == null) {
return
}
const [latest, ...backported] = rsort(versions)
if (backported.length === 0) {
return latest
}
const backportString = backported.map(version => `^${version}`).join(", ")
return `${latest} (backported: ${backportString})`
}
/**
* Verify the code to report unsupported APIs.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @param {import('../unsupported-features/types.js').SupportVersionBuiltins} traceMap The map for APIs to report.
* @returns {void}
*/
module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(
context,
traceMap
) {
const options = parseOptions(context)
const scope = getScope(context)
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(traceMap.modules ?? {}),
...tracker.iterateEsmReferences(traceMap.modules ?? {}),
...tracker.iterateGlobalReferences(traceMap.globals ?? {}),
]
for (const { node, path, info } of references) {
const name = unprefixNodeColon(path.join("."))
if (options.ignores.has(name)) {
continue
}
if (options.allowExperimental) {
if (isInRange(info.experimental, options.version)) {
continue
}
const experimentalVersion = versionsToString(info.experimental)
if (experimentalVersion) {
context.report({
node,
messageId: "not-experimental-till",
data: {
name: name,
experimental: experimentalVersion,
version: options.version.raw,
},
})
continue
}
}
if (isInRange(info.supported, options.version)) {
continue
}
const supportedVersion = versionsToString(info.supported)
if (supportedVersion) {
context.report({
node,
messageId: "not-supported-till",
data: {
name: name,
supported: supportedVersion,
version: options.version.raw,
},
})
continue
}
context.report({
node,
messageId: "not-supported-yet",
data: {
name: name,
version: options.version.raw,
},
})
}
}
exports.messages = {
"not-experimental-till": [
"The '{{name}}' is not an experimental feature",
"until Node.js {{experimental}}.",
"The configured version range is '{{version}}'.",
].join(" "),
"not-supported-till": [
"The '{{name}}' is still an experimental feature",
"and is not supported until Node.js {{supported}}.",
"The configured version range is '{{version}}'.",
].join(" "),
"not-supported-yet": [
"The '{{name}}' is still an experimental feature",
"The configured version range is '{{version}}'.",
].join(" "),
}