es-check 8.0.0 → 8.0.1-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants.js +500 -0
- package/detectFeatures.js +83 -0
- package/package.json +5 -2
- package/utils.js +164 -0
package/constants.js
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map of ES features (by name) → earliest ES minVersion + AST detection hints
|
|
3
|
+
* ES6 (2015) = 6, ES7 (2016) = 7, ES8 (2017) = 8, ES9 (2018) = 9,
|
|
4
|
+
* ES10 (2019) = 10, ES11 (2020) = 11, ES12 (2021) = 12,
|
|
5
|
+
* ES13 (2022) = 13, ES14 (2023) = 14, etc.
|
|
6
|
+
*/
|
|
7
|
+
const ES_FEATURES = {
|
|
8
|
+
// ----------------------------------------------------------
|
|
9
|
+
// ES6 / ES2015
|
|
10
|
+
// ----------------------------------------------------------
|
|
11
|
+
ArraySpread: {
|
|
12
|
+
minVersion: 6,
|
|
13
|
+
example: '[...arr]',
|
|
14
|
+
astInfo: {
|
|
15
|
+
nodeType: 'ArrayExpression',
|
|
16
|
+
childType: 'SpreadElement',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
let: {
|
|
20
|
+
minVersion: 6,
|
|
21
|
+
example: 'let x = 10;',
|
|
22
|
+
astInfo: {
|
|
23
|
+
nodeType: 'VariableDeclaration',
|
|
24
|
+
kind: 'let',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
const: {
|
|
28
|
+
minVersion: 6,
|
|
29
|
+
example: 'const x = 10;',
|
|
30
|
+
astInfo: {
|
|
31
|
+
nodeType: 'VariableDeclaration',
|
|
32
|
+
kind: 'const',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
class: {
|
|
36
|
+
minVersion: 6,
|
|
37
|
+
example: 'class MyClass {}',
|
|
38
|
+
astInfo: {
|
|
39
|
+
nodeType: 'ClassDeclaration',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
extends: {
|
|
43
|
+
minVersion: 6,
|
|
44
|
+
example: 'class MyClass extends OtherClass {}',
|
|
45
|
+
astInfo: {
|
|
46
|
+
nodeType: 'ClassDeclaration',
|
|
47
|
+
property: 'superClass',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
import: {
|
|
51
|
+
minVersion: 6,
|
|
52
|
+
example: 'import * as mod from "mod";',
|
|
53
|
+
astInfo: {
|
|
54
|
+
nodeType: 'ImportDeclaration',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
export: {
|
|
58
|
+
minVersion: 6,
|
|
59
|
+
example: 'export default x;',
|
|
60
|
+
astInfo: {
|
|
61
|
+
nodeType: 'ExportDeclaration',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
ArrowFunctions: {
|
|
65
|
+
minVersion: 6,
|
|
66
|
+
example: 'const fn = () => {};',
|
|
67
|
+
astInfo: {
|
|
68
|
+
nodeType: 'ArrowFunctionExpression',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
TemplateLiterals: {
|
|
72
|
+
minVersion: 6,
|
|
73
|
+
example: 'const str = `Hello, ${name}!`;',
|
|
74
|
+
astInfo: {
|
|
75
|
+
nodeType: 'TemplateLiteral',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
Destructuring: {
|
|
79
|
+
minVersion: 6,
|
|
80
|
+
example: 'const { x } = obj;',
|
|
81
|
+
astInfo: {
|
|
82
|
+
nodeType: 'ObjectPattern',
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
DefaultParams: {
|
|
86
|
+
minVersion: 6,
|
|
87
|
+
example: 'function foo(x=10) {}',
|
|
88
|
+
astInfo: {
|
|
89
|
+
nodeType: 'AssignmentPattern',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
RestSpread: {
|
|
93
|
+
minVersion: 6,
|
|
94
|
+
example: 'function(...args) {}',
|
|
95
|
+
astInfo: {
|
|
96
|
+
nodeType: 'RestElement',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
ForOf: {
|
|
100
|
+
minVersion: 6,
|
|
101
|
+
example: 'for (const x of iterable) {}',
|
|
102
|
+
astInfo: {
|
|
103
|
+
nodeType: 'ForOfStatement',
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
Map: {
|
|
107
|
+
minVersion: 6,
|
|
108
|
+
example: 'new Map()',
|
|
109
|
+
astInfo: {
|
|
110
|
+
nodeType: 'NewExpression',
|
|
111
|
+
callee: 'Map',
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
Set: {
|
|
115
|
+
minVersion: 6,
|
|
116
|
+
example: 'new Set()',
|
|
117
|
+
astInfo: {
|
|
118
|
+
nodeType: 'NewExpression',
|
|
119
|
+
callee: 'Set',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
WeakMap: {
|
|
123
|
+
minVersion: 6,
|
|
124
|
+
example: 'new WeakMap()',
|
|
125
|
+
astInfo: {
|
|
126
|
+
nodeType: 'NewExpression',
|
|
127
|
+
callee: 'WeakMap',
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
WeakSet: {
|
|
131
|
+
minVersion: 6,
|
|
132
|
+
example: 'new WeakSet()',
|
|
133
|
+
astInfo: {
|
|
134
|
+
nodeType: 'NewExpression',
|
|
135
|
+
callee: 'WeakSet',
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
Promise: {
|
|
139
|
+
minVersion: 6,
|
|
140
|
+
example: 'new Promise((resolve, reject) => {})',
|
|
141
|
+
astInfo: {
|
|
142
|
+
nodeType: 'NewExpression',
|
|
143
|
+
callee: 'Promise',
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
Symbol: {
|
|
147
|
+
minVersion: 6,
|
|
148
|
+
example: 'Symbol("desc")',
|
|
149
|
+
astInfo: {
|
|
150
|
+
nodeType: 'CallExpression',
|
|
151
|
+
callee: 'Symbol',
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
// ----------------------------------------------------------
|
|
156
|
+
// ES7 / ES2016
|
|
157
|
+
// ----------------------------------------------------------
|
|
158
|
+
ExponentOperator: {
|
|
159
|
+
minVersion: 7,
|
|
160
|
+
example: 'a ** b',
|
|
161
|
+
astInfo: {
|
|
162
|
+
nodeType: 'BinaryExpression',
|
|
163
|
+
operator: '**',
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
ArrayPrototypeIncludes: {
|
|
167
|
+
minVersion: 7,
|
|
168
|
+
example: 'arr.includes(x)',
|
|
169
|
+
astInfo: {
|
|
170
|
+
nodeType: 'CallExpression',
|
|
171
|
+
property: 'includes',
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
// ----------------------------------------------------------
|
|
176
|
+
// ES8 / ES2017
|
|
177
|
+
// ----------------------------------------------------------
|
|
178
|
+
AsyncAwait: {
|
|
179
|
+
minVersion: 8,
|
|
180
|
+
example: 'async function foo() { await bar(); }',
|
|
181
|
+
astInfo: {
|
|
182
|
+
nodeType: 'AwaitExpression',
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
ObjectValues: {
|
|
186
|
+
minVersion: 8,
|
|
187
|
+
example: 'Object.values(obj)',
|
|
188
|
+
astInfo: {
|
|
189
|
+
nodeType: 'CallExpression',
|
|
190
|
+
object: 'Object',
|
|
191
|
+
property: 'values',
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
ObjectEntries: {
|
|
195
|
+
minVersion: 8,
|
|
196
|
+
example: 'Object.entries(obj)',
|
|
197
|
+
astInfo: {
|
|
198
|
+
nodeType: 'CallExpression',
|
|
199
|
+
object: 'Object',
|
|
200
|
+
property: 'entries',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
StringPadStart: {
|
|
204
|
+
minVersion: 8,
|
|
205
|
+
example: 'str.padStart(10)',
|
|
206
|
+
astInfo: {
|
|
207
|
+
nodeType: 'CallExpression',
|
|
208
|
+
property: 'padStart',
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
StringPadEnd: {
|
|
212
|
+
minVersion: 8,
|
|
213
|
+
example: 'str.padEnd(10)',
|
|
214
|
+
astInfo: {
|
|
215
|
+
nodeType: 'CallExpression',
|
|
216
|
+
property: 'padEnd',
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
// ----------------------------------------------------------
|
|
221
|
+
// ES9 / ES2018
|
|
222
|
+
// ----------------------------------------------------------
|
|
223
|
+
ObjectSpread: {
|
|
224
|
+
minVersion: 9,
|
|
225
|
+
example: 'const obj2 = { ...obj};',
|
|
226
|
+
astInfo: {
|
|
227
|
+
nodeType: 'ObjectExpression',
|
|
228
|
+
childType: 'SpreadElement',
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
AsyncIteration: {
|
|
232
|
+
minVersion: 9,
|
|
233
|
+
example: 'for await (const x of asyncIterable) {}',
|
|
234
|
+
astInfo: {
|
|
235
|
+
nodeType: 'ForAwaitStatement',
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
PromiseFinally: {
|
|
239
|
+
minVersion: 9,
|
|
240
|
+
example: 'promise.finally(() => {})',
|
|
241
|
+
astInfo: {
|
|
242
|
+
nodeType: 'CallExpression',
|
|
243
|
+
property: 'finally',
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
// ----------------------------------------------------------
|
|
248
|
+
// ES10 / ES2019
|
|
249
|
+
// ----------------------------------------------------------
|
|
250
|
+
ArrayFlat: {
|
|
251
|
+
minVersion: 10,
|
|
252
|
+
example: 'arr.flat()',
|
|
253
|
+
astInfo: {
|
|
254
|
+
nodeType: 'CallExpression',
|
|
255
|
+
property: 'flat',
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
ArrayFlatMap: {
|
|
259
|
+
minVersion: 10,
|
|
260
|
+
example: 'arr.flatMap(x => x)',
|
|
261
|
+
astInfo: {
|
|
262
|
+
nodeType: 'CallExpression',
|
|
263
|
+
property: 'flatMap',
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
ObjectFromEntries: {
|
|
267
|
+
minVersion: 10,
|
|
268
|
+
example: 'Object.fromEntries(entries)',
|
|
269
|
+
astInfo: {
|
|
270
|
+
nodeType: 'CallExpression',
|
|
271
|
+
object: 'Object',
|
|
272
|
+
property: 'fromEntries',
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
OptionalCatchBinding: {
|
|
276
|
+
minVersion: 10,
|
|
277
|
+
example: 'try { ... } catch { ... }',
|
|
278
|
+
astInfo: {
|
|
279
|
+
nodeType: 'CatchClause',
|
|
280
|
+
noParam: true,
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
// ----------------------------------------------------------
|
|
285
|
+
// ES11 / ES2020
|
|
286
|
+
// ----------------------------------------------------------
|
|
287
|
+
BigInt: {
|
|
288
|
+
minVersion: 11,
|
|
289
|
+
example: '123n',
|
|
290
|
+
astInfo: {
|
|
291
|
+
nodeType: 'BigIntLiteral',
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
DynamicImport: {
|
|
295
|
+
minVersion: 11,
|
|
296
|
+
example: 'import("module.js")',
|
|
297
|
+
astInfo: {
|
|
298
|
+
nodeType: 'ImportExpression',
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
OptionalChaining: {
|
|
302
|
+
minVersion: 11,
|
|
303
|
+
example: 'obj?.prop',
|
|
304
|
+
astInfo: {
|
|
305
|
+
nodeType: 'ChainExpression',
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
NullishCoalescing: {
|
|
309
|
+
minVersion: 11,
|
|
310
|
+
example: 'a ?? b',
|
|
311
|
+
astInfo: {
|
|
312
|
+
nodeType: 'LogicalExpression',
|
|
313
|
+
operator: '??',
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
globalThis: {
|
|
317
|
+
minVersion: 11,
|
|
318
|
+
example: 'globalThis',
|
|
319
|
+
astInfo: {
|
|
320
|
+
nodeType: 'Identifier',
|
|
321
|
+
name: 'globalThis',
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
PromiseAllSettled: {
|
|
325
|
+
minVersion: 11,
|
|
326
|
+
example: 'Promise.allSettled(promises)',
|
|
327
|
+
astInfo: {
|
|
328
|
+
nodeType: 'CallExpression',
|
|
329
|
+
property: 'allSettled',
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
StringMatchAll: {
|
|
333
|
+
minVersion: 11,
|
|
334
|
+
example: 'str.matchAll(regex)',
|
|
335
|
+
astInfo: {
|
|
336
|
+
nodeType: 'CallExpression',
|
|
337
|
+
property: 'matchAll',
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
|
|
341
|
+
// ----------------------------------------------------------
|
|
342
|
+
// ES12 / ES2021
|
|
343
|
+
// ----------------------------------------------------------
|
|
344
|
+
LogicalAssignment: {
|
|
345
|
+
minVersion: 12,
|
|
346
|
+
example: 'x &&= y;',
|
|
347
|
+
astInfo: {
|
|
348
|
+
nodeType: 'AssignmentExpression',
|
|
349
|
+
operators: ['&&=', '||=', '??='],
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
NumericSeparators: {
|
|
353
|
+
minVersion: 12,
|
|
354
|
+
example: '1_000_000',
|
|
355
|
+
astInfo: {
|
|
356
|
+
nodeType: 'NumericLiteralWithSeparator',
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
StringReplaceAll: {
|
|
360
|
+
minVersion: 12,
|
|
361
|
+
example: 'str.replaceAll("a", "b")',
|
|
362
|
+
astInfo: {
|
|
363
|
+
nodeType: 'CallExpression',
|
|
364
|
+
property: 'replaceAll',
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
PromiseAny: {
|
|
368
|
+
minVersion: 12,
|
|
369
|
+
example: 'Promise.any(promises)',
|
|
370
|
+
astInfo: {
|
|
371
|
+
nodeType: 'CallExpression',
|
|
372
|
+
property: 'any',
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
WeakRef: {
|
|
376
|
+
minVersion: 12,
|
|
377
|
+
example: 'new WeakRef(obj)',
|
|
378
|
+
astInfo: {
|
|
379
|
+
nodeType: 'NewExpression',
|
|
380
|
+
callee: 'WeakRef',
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
FinalizationRegistry: {
|
|
384
|
+
minVersion: 12,
|
|
385
|
+
example: 'new FinalizationRegistry(cb)',
|
|
386
|
+
astInfo: {
|
|
387
|
+
nodeType: 'NewExpression',
|
|
388
|
+
callee: 'FinalizationRegistry',
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
// ----------------------------------------------------------
|
|
393
|
+
// ES13 / ES2022
|
|
394
|
+
// ----------------------------------------------------------
|
|
395
|
+
TopLevelAwait: {
|
|
396
|
+
minVersion: 13,
|
|
397
|
+
example: 'await foo()',
|
|
398
|
+
astInfo: {
|
|
399
|
+
nodeType: 'AwaitExpression',
|
|
400
|
+
topLevel: true,
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
PrivateClassFields: {
|
|
404
|
+
minVersion: 13,
|
|
405
|
+
example: 'class MyClass { #x = 1; }',
|
|
406
|
+
astInfo: {
|
|
407
|
+
nodeType: 'PropertyDefinition',
|
|
408
|
+
isPrivate: true,
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
ClassStaticBlocks: {
|
|
412
|
+
minVersion: 13,
|
|
413
|
+
example: 'class MyClass { static {} }',
|
|
414
|
+
astInfo: {
|
|
415
|
+
nodeType: 'StaticBlock',
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
ErgonomicBrandChecks: {
|
|
419
|
+
minVersion: 13,
|
|
420
|
+
example: '#field in obj',
|
|
421
|
+
astInfo: {
|
|
422
|
+
nodeType: 'BinaryExpression',
|
|
423
|
+
operator: 'in',
|
|
424
|
+
leftIsPrivate: true,
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
ErrorCause: {
|
|
428
|
+
minVersion: 13,
|
|
429
|
+
example: 'new Error("...", { cause: e })',
|
|
430
|
+
astInfo: {
|
|
431
|
+
nodeType: 'NewExpression',
|
|
432
|
+
callee: 'Error',
|
|
433
|
+
hasOptionsCause: true,
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
ArrayPrototypeAt: {
|
|
437
|
+
minVersion: 13,
|
|
438
|
+
example: 'arr.at(-1)',
|
|
439
|
+
astInfo: {
|
|
440
|
+
nodeType: 'CallExpression',
|
|
441
|
+
property: 'at',
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
// ----------------------------------------------------------
|
|
446
|
+
// ES14 / ES2023
|
|
447
|
+
// ----------------------------------------------------------
|
|
448
|
+
Hashbang: {
|
|
449
|
+
minVersion: 14,
|
|
450
|
+
example: '#!/usr/bin/env node',
|
|
451
|
+
astInfo: {
|
|
452
|
+
nodeType: 'Hashbang',
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
ArrayToReversed: {
|
|
456
|
+
minVersion: 14,
|
|
457
|
+
example: 'arr.toReversed()',
|
|
458
|
+
astInfo: {
|
|
459
|
+
nodeType: 'CallExpression',
|
|
460
|
+
property: 'toReversed',
|
|
461
|
+
},
|
|
462
|
+
},
|
|
463
|
+
ArrayToSorted: {
|
|
464
|
+
minVersion: 14,
|
|
465
|
+
example: 'arr.toSorted(compareFn)',
|
|
466
|
+
astInfo: {
|
|
467
|
+
nodeType: 'CallExpression',
|
|
468
|
+
property: 'toSorted',
|
|
469
|
+
},
|
|
470
|
+
},
|
|
471
|
+
ArrayToSpliced: {
|
|
472
|
+
minVersion: 14,
|
|
473
|
+
example: 'arr.toSpliced(start, deleteCount, ...)',
|
|
474
|
+
astInfo: {
|
|
475
|
+
nodeType: 'CallExpression',
|
|
476
|
+
property: 'toSpliced',
|
|
477
|
+
},
|
|
478
|
+
},
|
|
479
|
+
ArrayWith: {
|
|
480
|
+
minVersion: 14,
|
|
481
|
+
example: 'arr.with(index, value)',
|
|
482
|
+
astInfo: {
|
|
483
|
+
nodeType: 'CallExpression',
|
|
484
|
+
property: 'with',
|
|
485
|
+
},
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
const NODE_TYPES = {
|
|
490
|
+
VARIABLE_DECLARATION: 'VariableDeclaration',
|
|
491
|
+
ARROW_FUNCTION_EXPRESSION: 'ArrowFunctionExpression',
|
|
492
|
+
CHAIN_EXPRESSION: 'ChainExpression',
|
|
493
|
+
LOGICAL_EXPRESSION: 'LogicalExpression',
|
|
494
|
+
NEW_EXPRESSION: 'NewExpression',
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
module.exports = {
|
|
498
|
+
ES_FEATURES,
|
|
499
|
+
NODE_TYPES,
|
|
500
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const acorn = require('acorn');
|
|
2
|
+
const walk = require('acorn-walk');
|
|
3
|
+
const { ES_FEATURES } = require('./constants');
|
|
4
|
+
const { checkMap } = require('./utils');
|
|
5
|
+
|
|
6
|
+
const detectFeatures = (code, ecmaVersion, sourceType) => {
|
|
7
|
+
const ast = acorn.parse(code, {
|
|
8
|
+
ecmaVersion: 'latest',
|
|
9
|
+
sourceType,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @note Flatten all checks
|
|
14
|
+
*/
|
|
15
|
+
const allChecks = Object.entries(ES_FEATURES).map(([featureName, { astInfo }]) => ({
|
|
16
|
+
featureName,
|
|
17
|
+
nodeType: astInfo.nodeType,
|
|
18
|
+
astInfo,
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @note A universal visitor for any node type:
|
|
23
|
+
* - Filters checks that match the current node’s type
|
|
24
|
+
* - Calls the relevant checker function
|
|
25
|
+
* - If true => mark the feature as found
|
|
26
|
+
*/
|
|
27
|
+
const foundFeatures = Object.keys(ES_FEATURES).reduce((acc, f) => {
|
|
28
|
+
acc[f] = false;
|
|
29
|
+
return acc;
|
|
30
|
+
}, {});
|
|
31
|
+
|
|
32
|
+
const universalVisitor = (node) => {
|
|
33
|
+
allChecks
|
|
34
|
+
.filter(({ nodeType }) => nodeType === node.type)
|
|
35
|
+
.forEach(({ featureName, astInfo }) => {
|
|
36
|
+
const checker = checkMap[node.type] || checkMap.default;
|
|
37
|
+
if (checker(node, astInfo)) {
|
|
38
|
+
foundFeatures[featureName] = true;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @note Build the visitors object for acorn-walk.
|
|
45
|
+
* Each unique nodeType gets the same universalVisitor.
|
|
46
|
+
*/
|
|
47
|
+
const nodeTypes = [...new Set(allChecks.map((c) => c.nodeType))];
|
|
48
|
+
const visitors = nodeTypes.reduce((acc, nt) => {
|
|
49
|
+
acc[nt] = universalVisitor;
|
|
50
|
+
return acc;
|
|
51
|
+
}, {});
|
|
52
|
+
|
|
53
|
+
walk.simple(ast, visitors);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @note Check if any found feature requires a higher version than requested.
|
|
57
|
+
* We assume each entry in ES_FEATURES has a `minVersion` property.
|
|
58
|
+
*/
|
|
59
|
+
const unsupportedFeatures = Object.entries(ES_FEATURES).reduce((acc = [], [featureName, { minVersion }]) => {
|
|
60
|
+
// If feature is used but requires a newer version than ecmaVersion, it's unsupported
|
|
61
|
+
if (foundFeatures[featureName] && minVersion > ecmaVersion) {
|
|
62
|
+
acc.push(featureName);
|
|
63
|
+
}
|
|
64
|
+
return acc;
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @note Fail if any unsupported features were used.
|
|
69
|
+
*/
|
|
70
|
+
if (unsupportedFeatures.length > 0) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Unsupported features detected: ${unsupportedFeatures.join(', ')}. ` +
|
|
73
|
+
`These require a higher ES version than ${ecmaVersion}.`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
foundFeatures,
|
|
79
|
+
unsupportedFeatures,
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
module.exports = detectFeatures;
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-check",
|
|
3
|
-
"version": "8.0.0",
|
|
3
|
+
"version": "8.0.1-0",
|
|
4
4
|
"description": "Checks the ECMAScript version of .js glob against a specified version of ECMAScript with a shell command",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"files": [
|
|
8
|
-
"index.js"
|
|
8
|
+
"index.js",
|
|
9
|
+
"utils.js",
|
|
10
|
+
"detectFeatures.js",
|
|
11
|
+
"constants.js"
|
|
9
12
|
],
|
|
10
13
|
"bin": {
|
|
11
14
|
"es-check": "index.js"
|
package/utils.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @note Checks if node.kind === astInfo.kind (e.g., 'const', 'let').
|
|
3
|
+
*/
|
|
4
|
+
const checkVarKindMatch = (node, astInfo) => {
|
|
5
|
+
if (!astInfo.kind) return false;
|
|
6
|
+
return node.kind === astInfo.kind;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @note Checks if a NewExpression node's callee is an Identifier
|
|
11
|
+
* that matches astInfo.callee (e.g. "Promise", "WeakRef").
|
|
12
|
+
*/
|
|
13
|
+
const checkCalleeMatch = (node, astInfo) => {
|
|
14
|
+
if (!astInfo.callee) return false;
|
|
15
|
+
// e.g. node.callee.type === 'Identifier' && node.callee.name === 'Promise'
|
|
16
|
+
if (!node.callee || node.callee.type !== 'Identifier') return false;
|
|
17
|
+
return node.callee.name === astInfo.callee;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @note Checks if a LogicalExpression node's operator matches astInfo.operator (e.g., '??').
|
|
22
|
+
*/
|
|
23
|
+
const checkOperatorMatch = (node, astInfo) =>{
|
|
24
|
+
if (!astInfo.operator) return false;
|
|
25
|
+
return node.operator === astInfo.operator;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @note For simple presence-based checks (e.g., ArrowFunctionExpression).
|
|
30
|
+
*/
|
|
31
|
+
const checkDefault = () => {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @note A more "universal" check for a CallExpression, used for many ES features:
|
|
37
|
+
* - arrayMethod => property: 'flat', 'includes', 'at', etc.
|
|
38
|
+
* - objectMethod => object: 'Object', property: 'fromEntries', etc.
|
|
39
|
+
*/
|
|
40
|
+
const checkCallExpression = (node, astInfo) => {
|
|
41
|
+
// Must be `CallExpression`
|
|
42
|
+
if (node.type !== 'CallExpression') return false;
|
|
43
|
+
|
|
44
|
+
// We might check if node.callee is a MemberExpression, e.g. array.includes(...)
|
|
45
|
+
// or if node.callee is an Identifier, e.g. Symbol(...).
|
|
46
|
+
if (node.callee.type === 'MemberExpression') {
|
|
47
|
+
const { object, property } = astInfo;
|
|
48
|
+
// e.g. object: 'Object', property: 'entries'
|
|
49
|
+
// => node.callee.object.name === 'Object' && node.callee.property.name === 'entries'
|
|
50
|
+
if (object) {
|
|
51
|
+
if (
|
|
52
|
+
!node.callee.object ||
|
|
53
|
+
node.callee.object.type !== 'Identifier' ||
|
|
54
|
+
node.callee.object.name !== object
|
|
55
|
+
) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (property) {
|
|
60
|
+
// e.g. property: 'includes'
|
|
61
|
+
if (!node.callee.property || node.callee.property.name !== property) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
} else if (node.callee.type === 'Identifier') {
|
|
67
|
+
// e.g. Symbol("desc")
|
|
68
|
+
const { callee } = astInfo;
|
|
69
|
+
// If astInfo.callee is "Symbol", check node.callee.name
|
|
70
|
+
if (callee && node.callee.name === callee) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @note Check ObjectExpression for childType, e.g. 'SpreadElement'
|
|
80
|
+
*/
|
|
81
|
+
const checkObjectExpression = (node, astInfo) => {
|
|
82
|
+
// If we want to detect object spread, we might check if node.properties
|
|
83
|
+
// contain a SpreadElement
|
|
84
|
+
if (astInfo.childType === 'SpreadElement') {
|
|
85
|
+
return node.properties.some((p) => p.type === 'SpreadElement');
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @note Check ClassDeclaration presence or superClass usage
|
|
92
|
+
*/
|
|
93
|
+
const checkClassDeclaration = (node, astInfo) => {
|
|
94
|
+
// Just having a ClassDeclaration means classes are used.
|
|
95
|
+
// If astInfo has `property: 'superClass'`, it means "extends" usage
|
|
96
|
+
if (astInfo.property === 'superClass') {
|
|
97
|
+
return !!node.superClass; // if superClass is not null, "extends" is used
|
|
98
|
+
}
|
|
99
|
+
return true; // default: any ClassDeclaration means the feature is used
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @note Example check for BinaryExpression (e.g., exponent operator `**`).
|
|
104
|
+
*/
|
|
105
|
+
const checkBinaryExpression = (node, astInfo) => {
|
|
106
|
+
if (!astInfo.operator) return false;
|
|
107
|
+
return node.operator === astInfo.operator;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const checkForAwaitStatement = (node) => {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @note Example check for CatchClause with no param => optional catch binding
|
|
116
|
+
*/
|
|
117
|
+
const checkCatchClause = (node, astInfo) => {
|
|
118
|
+
if (astInfo.noParam) {
|
|
119
|
+
return !node.param;
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @note Example check for BigIntLiteral or numeric with underscore
|
|
126
|
+
*/
|
|
127
|
+
const checkBigIntLiteral = (node) =>{
|
|
128
|
+
if (typeof node.value === 'bigint') {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @note the "catch-all" object mapping node types to specialized checkers
|
|
136
|
+
*/
|
|
137
|
+
const checkMap = {
|
|
138
|
+
VariableDeclaration: (node, astInfo) => checkVarKindMatch(node, astInfo),
|
|
139
|
+
ArrowFunctionExpression: () => checkDefault(),
|
|
140
|
+
ChainExpression: () => checkDefault(),
|
|
141
|
+
LogicalExpression: (node, astInfo) => checkOperatorMatch(node, astInfo),
|
|
142
|
+
NewExpression: (node, astInfo) => checkCalleeMatch(node, astInfo),
|
|
143
|
+
CallExpression: (node, astInfo) => checkCallExpression(node, astInfo),
|
|
144
|
+
ObjectExpression: (node, astInfo) => checkObjectExpression(node, astInfo),
|
|
145
|
+
ClassDeclaration: (node, astInfo) => checkClassDeclaration(node, astInfo),
|
|
146
|
+
BinaryExpression: (node, astInfo) => checkBinaryExpression(node, astInfo),
|
|
147
|
+
ForAwaitStatement: (node) => checkForAwaitStatement(node),
|
|
148
|
+
CatchClause: (node, astInfo) => checkCatchClause(node, astInfo),
|
|
149
|
+
Literal: (node, astInfo) => {
|
|
150
|
+
if (astInfo.nodeType === 'BigIntLiteral') {
|
|
151
|
+
return checkBigIntLiteral(node);
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
},
|
|
155
|
+
default: () => false,
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
module.exports = {
|
|
159
|
+
checkVarKindMatch,
|
|
160
|
+
checkCalleeMatch,
|
|
161
|
+
checkOperatorMatch,
|
|
162
|
+
checkDefault,
|
|
163
|
+
checkMap,
|
|
164
|
+
};
|