-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.cjs
More file actions
343 lines (308 loc) · 8.99 KB
/
Copy pathindex.cjs
File metadata and controls
343 lines (308 loc) · 8.99 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict';
const estraverse = require('estraverse');
/**
* unassert
* Encourages programming with assertions by providing tools to compile them away.
*
* https://github.com/unassert-js/unassert
*
* Copyright (c) 2015-2022 Takuto Wada
* Licensed under the MIT license.
* https://github.com/unassert-js/unassert/blob/master/LICENSE
*/
function isLiteral (node) {
return node && node.type === 'Literal';
}
function isIdentifier (node) {
return node && node.type === 'Identifier';
}
function isObjectPattern (node) {
return node && node.type === 'ObjectPattern';
}
function isMemberExpression (node) {
return node && node.type === 'MemberExpression';
}
function isCallExpression (node) {
return node && node.type === 'CallExpression';
}
function isExpressionStatement (node) {
return node && node.type === 'ExpressionStatement';
}
function isIfStatement (node) {
return node && node.type === 'IfStatement';
}
function isImportDeclaration (node) {
return node && node.type === 'ImportDeclaration';
}
function isBodyOfNodeHavingNonBlockStatementAsBody (node, key) {
if (!node) {
return false;
}
if (key !== 'body') {
return false;
}
switch (node.type) {
case 'DoWhileStatement':
case 'ForInStatement':
case 'ForOfStatement':
case 'ForStatement':
case 'LabeledStatement':
case 'WithStatement':
case 'WhileStatement':
return true;
}
return false;
}
function isBodyOfIfStatement (node, key) {
return isIfStatement(node) && (key === 'consequent' || key === 'alternate');
}
function isNonBlockChildOfParentNode (currentNode, parentNode, key) {
return isExpressionStatement(currentNode) && isCallExpression(currentNode.expression) &&
(isBodyOfIfStatement(parentNode, key) || isBodyOfNodeHavingNonBlockStatementAsBody(parentNode, key));
}
function createVisitor (options) {
const config = Object.assign(defaultOptions(), options);
const targetModules = new Set(config.modules);
const targetVariables = new Set(config.variables);
const nodeUpdates = new WeakMap();
function isAssertionModuleName (lit) {
return isLiteral(lit) && targetModules.has(lit.value);
}
function isAssertionVariableName (id) {
return isIdentifier(id) && targetVariables.has(id.name);
}
function isAssertionMethod (callee) {
if (!isMemberExpression(callee)) {
return false;
}
const obj = callee.object;
if (isMemberExpression(obj)) {
return isAssertionMethod(obj);
} else {
return isAssertionVariableName(obj);
}
}
function isAssertionFunction (callee) {
return isAssertionVariableName(callee);
}
function isConsoleAssert (callee) {
if (!isMemberExpression(callee)) {
return false;
}
const { object: obj, property: prop } = callee;
return isIdentifier(obj) && obj.name === 'console' &&
isIdentifier(prop) && prop.name === 'assert';
}
function registerIdentifierAsAssertionVariable (id) {
if (isIdentifier(id)) {
targetVariables.add(id.name);
}
}
function handleDestructuredAssertionAssignment (objectPattern) {
for (const { value } of objectPattern.properties) {
registerIdentifierAsAssertionVariable(value);
}
}
function handleImportSpecifiers (importDeclaration) {
for (const { local } of importDeclaration.specifiers) {
registerIdentifierAsAssertionVariable(local);
}
}
function registerAssertionVariables (node) {
if (isIdentifier(node)) {
registerIdentifierAsAssertionVariable(node);
} else if (isObjectPattern(node)) {
handleDestructuredAssertionAssignment(node);
} else if (isImportDeclaration(node)) {
handleImportSpecifiers(node);
}
}
function isRequireAssert (id, init) {
if (!isCallExpression(init)) {
return false;
}
const callee = init.callee;
if (!isIdentifier(callee) || callee.name !== 'require') {
return false;
}
const arg = init.arguments[0];
if (!isLiteral(arg) || !isAssertionModuleName(arg)) {
return false;
}
return isIdentifier(id) || isObjectPattern(id);
}
function isRequireAssertDotStrict (id, init) {
if (!isMemberExpression(init)) {
return false;
}
if (!isRequireAssert(id, init.object)) {
return false;
}
const prop = init.property;
if (!isIdentifier(prop)) {
return false;
}
return prop.name === 'strict';
}
function isRemovalTargetRequire (id, init) {
return isRequireAssert(id, init) || isRequireAssertDotStrict(id, init);
}
function isRemovalTargetAssertion (callee) {
return isAssertionFunction(callee) || isAssertionMethod(callee) || isConsoleAssert(callee);
}
function removeNode (node) {
nodeUpdates.set(node, null);
}
function replaceNode (node, replacement) {
nodeUpdates.set(node, replacement);
}
function createNoopExpression () {
return {
type: 'UnaryExpression',
operator: 'void',
prefix: true,
argument: {
type: 'Literal',
value: 0,
raw: '0'
}
};
}
function createNoopStatement () {
return {
type: 'BlockStatement',
body: []
};
}
function unassertImportDeclaration (currentNode, parentNode) {
const source = currentNode.source;
if (!(isAssertionModuleName(source))) {
return;
}
// remove current ImportDeclaration
removeNode(currentNode);
this.skip();
// register local identifier(s) as assertion variable
registerAssertionVariables(currentNode);
}
function unassertVariableDeclarator (currentNode, parentNode) {
if (isRemovalTargetRequire(currentNode.id, currentNode.init)) {
if (parentNode.declarations.length === 1) {
// remove parent VariableDeclaration
removeNode(parentNode);
} else {
// single var pattern
// remove current VariableDeclarator
removeNode(currentNode);
}
this.skip();
// register local identifier(s) as assertion variable
registerAssertionVariables(currentNode.id);
}
}
function unassertAssignmentExpression (currentNode, parentNode) {
if (currentNode.operator !== '=') {
return;
}
if (!isExpressionStatement(parentNode)) {
return;
}
if (isRemovalTargetRequire(currentNode.left, currentNode.right)) {
// remove parent ExpressionStatement
removeNode(parentNode);
this.skip();
// register local identifier(s) as assertion variable
registerAssertionVariables(currentNode.left);
}
}
function unassertCallExpression (currentNode, parentNode) {
const callee = currentNode.callee;
if (!isRemovalTargetAssertion(callee)) {
return;
}
switch (parentNode.type) {
case 'ExpressionStatement': {
// remove parent ExpressionStatement
removeNode(parentNode);
this.skip();
break;
}
case 'SequenceExpression': {
// replace the asserstion with essentially nothing
replaceNode(currentNode, createNoopExpression());
break;
}
}
}
function unassertAwaitExpression (currentNode, parentNode) {
const childNode = currentNode.argument;
if (isExpressionStatement(parentNode) && isCallExpression(childNode)) {
const callee = childNode.callee;
if (isRemovalTargetAssertion(callee)) {
// remove parent ExpressionStatement
removeNode(parentNode);
this.skip();
}
}
}
return {
enter: function (currentNode, parentNode) {
switch (currentNode.type) {
case 'ImportDeclaration': {
unassertImportDeclaration.bind(this)(currentNode, parentNode);
break;
}
case 'VariableDeclarator': {
unassertVariableDeclarator.bind(this)(currentNode, parentNode);
break;
}
case 'AssignmentExpression': {
unassertAssignmentExpression.bind(this)(currentNode, parentNode);
break;
}
case 'CallExpression': {
unassertCallExpression.bind(this)(currentNode, parentNode);
break;
}
case 'AwaitExpression': {
unassertAwaitExpression.bind(this)(currentNode, parentNode);
break;
}
}
},
leave: function (currentNode, parentNode) {
const update = nodeUpdates.get(currentNode);
if (update === undefined) {
return undefined;
}
if (update === null) {
if (isExpressionStatement(currentNode)) {
const path = this.path();
const key = path[path.length - 1];
if (isNonBlockChildOfParentNode(currentNode, parentNode, key)) {
return createNoopStatement();
}
}
this.remove();
return undefined;
}
return update;
}
};
}
function unassertAst (ast, options) {
return estraverse.replace(ast, createVisitor(options));
}
function defaultOptions () {
return {
modules: [
'assert',
'assert/strict',
'node:assert',
'node:assert/strict'
]
};
}
exports.createVisitor = createVisitor;
exports.defaultOptions = defaultOptions;
exports.unassertAst = unassertAst;