forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathes-syntax.js
230 lines (205 loc) · 7.21 KB
/
es-syntax.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
const { getInnermostScope } = require("@eslint-community/eslint-utils")
const { rules: esRules } = require("eslint-plugin-es-x")
const rangeSubset = require("semver/ranges/subset")
const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
const getSemverRange = require("../../util/get-semver-range")
const mergeVisitorsInPlace = require("../../util/merge-visitors-in-place")
const { getScope } = require("../../util/eslint-compat")
/** @type {Record<string, ESSyntax>} */
const features = require("./es-syntax.json")
/** @type {Set<string>} */
const ignoreKeys = new Set()
/**
* @typedef ESSyntax
* @property {string[]} [aliases]
* @property {string | null} supported
* @property {string} [strictMode]
* @property {string} [deprecated]
*/
/**
* @typedef RuleMap
* @property {string} ruleId
* @property {string} feature
* @property {string[]} ignoreNames
* @property {import("semver").Range} supported
* @property {import("semver").Range} [strictMode]
* @property {boolean} deprecated
*/
/**
* @param {string} _match The entire match
* @param {string} first The first regex group
* @returns {string}
*/
function firstMatchToUpper(_match, first) {
return first.toUpperCase()
}
/** @type {RuleMap[]} */
const ruleMap = Object.entries(features).map(([ruleId, meta]) => {
const ruleIdNegated = ruleId.replace(/^no-/, "")
const ruleIdCamel = ruleIdNegated.replace(/-(\w)/g, firstMatchToUpper)
meta.aliases ??= []
const ignoreNames = [ruleId, ruleIdNegated, ruleIdCamel, ...meta.aliases]
for (const ignoreName of ignoreNames) {
ignoreKeys.add(ignoreName)
}
const supported = getSemverRange(meta.supported ?? "<0")
if (supported == null) {
throw new Error(`Invalid semver Range: "${meta.supported}"`)
}
/** @type {RuleMap} */
const rule = {
ruleId: ruleId,
feature: ruleIdNegated,
ignoreNames: ignoreNames,
supported: supported,
deprecated: Boolean(meta.deprecated),
}
if (meta.strictMode) {
const range = getSemverRange(meta.strictMode)
if (range) {
rule.strictMode = range
}
}
return rule
})
/**
* Parses the options.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @returns {{version: import('semver').Range,ignores:Set<string>}} Parsed value.
*/
function parseOptions(context) {
/** @type {{ ignores?: string[] }} */
const raw = context.options[0] || {}
const version = getConfiguredNodeVersion(context)
const ignores = new Set(raw.ignores || [])
return Object.freeze({ version, ignores })
}
/**
* Find the scope that a given node belongs to.
* @param {import('eslint').Scope.Scope} initialScope The initial scope to find.
* @param {import('estree').Node} node The AST node.
* @returns {import('eslint').Scope.Scope} The scope that the node belongs to.
*/
function normalizeScope(initialScope, node) {
let scope = getInnermostScope(initialScope, node)
while (scope?.block === node && scope.upper) {
scope = scope.upper
}
return scope
}
/**
* @param {import('eslint').Rule.RuleContext} context
* @param {import('estree').Node} node
* @returns {boolean}
*/
function isStrict(context, node) {
const scope = getScope(context)
return normalizeScope(scope, node).isStrict
}
/**
* Define the visitor object as merging the rules of eslint-plugin-es-x.
* @param {import('eslint').Rule.RuleContext} context The rule context.
* @param {ReturnType<parseOptions>} options The options.
* @returns {object} The defined visitor.
*/
function defineVisitor(context, options) {
return ruleMap
.filter(
rule =>
rule.ignoreNames.every(
ignoreName => options.ignores.has(ignoreName) === false
) &&
rangeSubset(
options.version,
rule.strictMode ?? rule.supported
) === false
)
.map(rule => {
const esRule = /** @type {import('eslint').Rule.RuleModule} */ (
esRules[rule.ruleId]
)
/** @type {Partial<import('eslint').Rule.RuleContext>} */
const esContext = {
report(descriptor) {
delete descriptor.fix
if (descriptor.data == null) {
descriptor.data = {}
}
descriptor.data.featureName = rule.feature
descriptor.data.version = options.version.raw
descriptor.data.supported = rule.supported.raw
if (rule.strictMode != null) {
if (
isStrict(
context,
/** @type {{ node: import('estree').Node}} */ (
descriptor
).node
) === false
) {
descriptor.data.supported = rule.strictMode.raw
} else if (
rangeSubset(options.version, rule.supported)
) {
return
}
}
const messageId =
rule.supported.raw === "<0"
? "not-supported-yet"
: "not-supported-till"
super.report({ ...descriptor, messageId })
},
}
Object.setPrototypeOf(esContext, context)
return esRule.create(
/** @type {import('eslint').Rule.RuleContext} */ (esContext)
)
})
.reduce(mergeVisitorsInPlace, {})
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description:
"disallow unsupported ECMAScript syntax on the specified version",
recommended: true,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-unsupported-features/es-syntax.md",
},
type: "problem",
fixable: null,
schema: [
{
type: "object",
properties: {
version: getConfiguredNodeVersion.schema,
ignores: {
type: "array",
items: { enum: [...ignoreKeys] },
uniqueItems: true,
},
},
additionalProperties: false,
},
],
messages: {
"not-supported-till": [
"'{{featureName}}' is not supported until Node.js {{supported}}.",
"The configured version range is '{{version}}'.",
].join(" "),
"not-supported-yet": [
"'{{featureName}}' is not supported in Node.js.",
"The configured version range is '{{version}}'.",
].join(" "),
},
},
create(context) {
return defineVisitor(context, parseOptions(context))
},
}