forked from dollarshaveclub/es-check
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.js
164 lines (150 loc) · 4.88 KB
/
utils.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
/**
* @note Checks if node.kind === astInfo.kind (e.g., 'const', 'let').
*/
const checkVarKindMatch = (node, astInfo) => {
if (!astInfo.kind) return false;
return node.kind === astInfo.kind;
}
/**
* @note Checks if a NewExpression node's callee is an Identifier
* that matches astInfo.callee (e.g. "Promise", "WeakRef").
*/
const checkCalleeMatch = (node, astInfo) => {
if (!astInfo.callee) return false;
// e.g. node.callee.type === 'Identifier' && node.callee.name === 'Promise'
if (!node.callee || node.callee.type !== 'Identifier') return false;
return node.callee.name === astInfo.callee;
}
/**
* @note Checks if a LogicalExpression node's operator matches astInfo.operator (e.g., '??').
*/
const checkOperatorMatch = (node, astInfo) =>{
if (!astInfo.operator) return false;
return node.operator === astInfo.operator;
}
/**
* @note For simple presence-based checks (e.g., ArrowFunctionExpression).
*/
const checkDefault = () => {
return true;
}
/**
* @note A more "universal" check for a CallExpression, used for many ES features:
* - arrayMethod => property: 'flat', 'includes', 'at', etc.
* - objectMethod => object: 'Object', property: 'fromEntries', etc.
*/
const checkCallExpression = (node, astInfo) => {
// Must be `CallExpression`
if (node.type !== 'CallExpression') return false;
// We might check if node.callee is a MemberExpression, e.g. array.includes(...)
// or if node.callee is an Identifier, e.g. Symbol(...).
if (node.callee.type === 'MemberExpression') {
const { object, property } = astInfo;
// e.g. object: 'Object', property: 'entries'
// => node.callee.object.name === 'Object' && node.callee.property.name === 'entries'
if (object) {
if (
!node.callee.object ||
node.callee.object.type !== 'Identifier' ||
node.callee.object.name !== object
) {
return false;
}
}
if (property) {
// e.g. property: 'includes'
if (!node.callee.property || node.callee.property.name !== property) {
return false;
}
}
return true;
} else if (node.callee.type === 'Identifier') {
// e.g. Symbol("desc")
const { callee } = astInfo;
// If astInfo.callee is "Symbol", check node.callee.name
if (callee && node.callee.name === callee) {
return true;
}
}
return false;
}
/**
* @note Check ObjectExpression for childType, e.g. 'SpreadElement'
*/
const checkObjectExpression = (node, astInfo) => {
// If we want to detect object spread, we might check if node.properties
// contain a SpreadElement
if (astInfo.childType === 'SpreadElement') {
return node.properties.some((p) => p.type === 'SpreadElement');
}
return false;
}
/**
* @note Check ClassDeclaration presence or superClass usage
*/
const checkClassDeclaration = (node, astInfo) => {
// Just having a ClassDeclaration means classes are used.
// If astInfo has `property: 'superClass'`, it means "extends" usage
if (astInfo.property === 'superClass') {
return !!node.superClass; // if superClass is not null, "extends" is used
}
return true; // default: any ClassDeclaration means the feature is used
}
/**
* @note Example check for BinaryExpression (e.g., exponent operator `**`).
*/
const checkBinaryExpression = (node, astInfo) => {
if (!astInfo.operator) return false;
return node.operator === astInfo.operator;
}
const checkForAwaitStatement = (node) => {
return true;
}
/**
* @note Example check for CatchClause with no param => optional catch binding
*/
const checkCatchClause = (node, astInfo) => {
if (astInfo.noParam) {
return !node.param;
}
return false;
}
/**
* @note Example check for BigIntLiteral or numeric with underscore
*/
const checkBigIntLiteral = (node) =>{
if (typeof node.value === 'bigint') {
return true;
}
return false;
}
/**
* @note the "catch-all" object mapping node types to specialized checkers
*/
const checkMap = {
VariableDeclaration: (node, astInfo) => checkVarKindMatch(node, astInfo),
ArrowFunctionExpression: () => checkDefault(),
ChainExpression: () => checkDefault(),
LogicalExpression: (node, astInfo) => checkOperatorMatch(node, astInfo),
NewExpression: (node, astInfo) => checkCalleeMatch(node, astInfo),
CallExpression: (node, astInfo) => checkCallExpression(node, astInfo),
ObjectExpression: (node, astInfo) => checkObjectExpression(node, astInfo),
ClassDeclaration: (node, astInfo) => checkClassDeclaration(node, astInfo),
BinaryExpression: (node, astInfo) => checkBinaryExpression(node, astInfo),
ForAwaitStatement: (node) => checkForAwaitStatement(node),
CatchClause: (node, astInfo) => checkCatchClause(node, astInfo),
Literal: (node, astInfo) => {
if (astInfo.nodeType === 'BigIntLiteral') {
return checkBigIntLiteral(node);
}
return false;
},
default: () => false,
};
module.exports = {
checkVarKindMatch,
checkCalleeMatch,
checkOperatorMatch,
checkDefault,
checkMap,
};