@ant-yasa/uast-parser-java-js 0.1.15-beta → 0.1.19-beta.3
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/package.json +2 -2
- package/dist/package.json +0 -73
- package/dist/src/frontend/java/ASTBuilder.d.ts +0 -171
- package/dist/src/frontend/java/ASTBuilder.js +0 -2369
- package/dist/src/frontend/java/ASTBuilder.js.map +0 -1
- package/dist/src/frontend/java/ErrorListener.d.ts +0 -9
- package/dist/src/frontend/java/ErrorListener.js +0 -24
- package/dist/src/frontend/java/ErrorListener.js.map +0 -1
- package/dist/src/frontend/java/antlr/JavaLexer.d.ts +0 -154
- package/dist/src/frontend/java/antlr/JavaLexer.js +0 -812
- package/dist/src/frontend/java/antlr/JavaLexer.js.map +0 -1
- package/dist/src/frontend/java/antlr/JavaListener.d.ts +0 -1393
- package/dist/src/frontend/java/antlr/JavaListener.js +0 -4
- package/dist/src/frontend/java/antlr/JavaListener.js.map +0 -1
- package/dist/src/frontend/java/antlr/JavaParser.d.ts +0 -2065
- package/dist/src/frontend/java/antlr/JavaParser.js +0 -15279
- package/dist/src/frontend/java/antlr/JavaParser.js.map +0 -1
- package/dist/src/frontend/java/antlr/JavaVisitor.d.ts +0 -892
- package/dist/src/frontend/java/antlr/JavaVisitor.js +0 -4
- package/dist/src/frontend/java/antlr/JavaVisitor.js.map +0 -1
- package/dist/src/frontend/java/parser.d.ts +0 -20
- package/dist/src/frontend/java/parser.js +0 -93
- package/dist/src/frontend/java/parser.js.map +0 -1
- package/dist/src/frontend/java/testBuilder.d.ts +0 -146
- package/dist/src/frontend/java/testBuilder.js +0 -464
- package/dist/src/frontend/java/testBuilder.js.map +0 -1
- package/dist/src/frontend/java/types.d.ts +0 -29
- package/dist/src/frontend/java/types.js +0 -3
- package/dist/src/frontend/java/types.js.map +0 -1
- package/dist/src/frontend/javascript/parser.d.ts +0 -8
- package/dist/src/frontend/javascript/parser.js +0 -1121
- package/dist/src/frontend/javascript/parser.js.map +0 -1
- package/dist/src/index.d.ts +0 -14
- package/dist/src/index.js +0 -76
- package/dist/src/index.js.map +0 -1
|
@@ -1,1121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.parse = exports.appendLocMeta = exports.createTmpVariableDeclaration = exports.createTmpIdentifier = void 0;
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
const babelCore = __importStar(require("@babel/core"));
|
|
29
|
-
const babelParser = __importStar(require("@babel/parser"));
|
|
30
|
-
const AST = __importStar(require("@babel/types"));
|
|
31
|
-
const UAST = __importStar(require("@ant-yasa/uast-spec"));
|
|
32
|
-
const uast_spec_1 = require("@ant-yasa/uast-spec");
|
|
33
|
-
const visitor = (opts) => ({
|
|
34
|
-
EmptyStatement(node) {
|
|
35
|
-
return UAST.noop();
|
|
36
|
-
},
|
|
37
|
-
File(node) {
|
|
38
|
-
return visit(node.program, opts);
|
|
39
|
-
},
|
|
40
|
-
Program(node) {
|
|
41
|
-
const body = node.body.map((stmt) => {
|
|
42
|
-
return visit(stmt, opts);
|
|
43
|
-
});
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
const source = node.loc?.filename || '';
|
|
46
|
-
return UAST.compileUnit(body, 'javascript', null, source, uast_spec_1.version);
|
|
47
|
-
},
|
|
48
|
-
Identifier(node) {
|
|
49
|
-
return UAST.identifier(node.name);
|
|
50
|
-
},
|
|
51
|
-
PrivateName(node) {
|
|
52
|
-
return UAST.identifier(node.id.name);
|
|
53
|
-
},
|
|
54
|
-
RegExpLiteral(node) {
|
|
55
|
-
return UAST.literal(node.pattern, 'string');
|
|
56
|
-
},
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
58
|
-
NullLiteral(node) {
|
|
59
|
-
return UAST.literal(null, 'null');
|
|
60
|
-
},
|
|
61
|
-
StringLiteral(node) {
|
|
62
|
-
return UAST.literal(node.value, 'string');
|
|
63
|
-
},
|
|
64
|
-
BooleanLiteral(node) {
|
|
65
|
-
return UAST.literal(node.value, 'boolean');
|
|
66
|
-
},
|
|
67
|
-
NumericLiteral(node) {
|
|
68
|
-
return UAST.literal(node.value, 'number');
|
|
69
|
-
},
|
|
70
|
-
BigIntLiteral(node) {
|
|
71
|
-
return UAST.literal(node.value, 'number');
|
|
72
|
-
},
|
|
73
|
-
DecimalLiteral(node) {
|
|
74
|
-
return UAST.literal(node.value, 'string');
|
|
75
|
-
},
|
|
76
|
-
TemplateLiteral(node) {
|
|
77
|
-
if (node.quasis.length === 1) {
|
|
78
|
-
return UAST.literal(node.quasis[0].value.raw, 'string');
|
|
79
|
-
}
|
|
80
|
-
return buildBinaryExpression(node.quasis, node.expressions, false);
|
|
81
|
-
function buildBinaryExpression(quasis, expressions, exprProcess) {
|
|
82
|
-
if (!exprProcess) {
|
|
83
|
-
const quasi = quasis.shift();
|
|
84
|
-
if (!quasi) {
|
|
85
|
-
return UAST.literal("", 'string');
|
|
86
|
-
}
|
|
87
|
-
if (quasi.tail) {
|
|
88
|
-
const quasiLit = UAST.literal(quasi.value.raw, 'string');
|
|
89
|
-
quasiLit.loc = quasi.loc;
|
|
90
|
-
return quasiLit;
|
|
91
|
-
}
|
|
92
|
-
const binaryExpression = UAST.binaryExpression('+', UAST.literal(quasi.value.raw, 'string'), buildBinaryExpression(quasis, expressions, true));
|
|
93
|
-
binaryExpression.left.loc = quasi.loc;
|
|
94
|
-
return binaryExpression;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
const expr = expressions.shift();
|
|
98
|
-
if (!expr) {
|
|
99
|
-
throw new Error('expr cannot be empty');
|
|
100
|
-
}
|
|
101
|
-
if (expressions.length == 0 && quasis.length == 0) {
|
|
102
|
-
return visit(expr, opts);
|
|
103
|
-
}
|
|
104
|
-
return UAST.binaryExpression('+', visit(expr, opts), buildBinaryExpression(quasis, expressions, false));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
ExpressionStatement(node) {
|
|
109
|
-
return UAST.expressionStatement(visit(node.expression, opts));
|
|
110
|
-
},
|
|
111
|
-
BlockStatement(node) {
|
|
112
|
-
return UAST.scopedStatement(node.body.map((stmt) => {
|
|
113
|
-
return visit(stmt, opts);
|
|
114
|
-
}), null);
|
|
115
|
-
},
|
|
116
|
-
// with(x) { //code }
|
|
117
|
-
// to
|
|
118
|
-
// {
|
|
119
|
-
// ...x;
|
|
120
|
-
// //code
|
|
121
|
-
// }
|
|
122
|
-
WithStatement(node) {
|
|
123
|
-
const spreadElement = UAST.spreadElement(visit(node.object, opts));
|
|
124
|
-
const body = visit(node.body, opts);
|
|
125
|
-
return UAST.scopedStatement([
|
|
126
|
-
UAST.expressionStatement(spreadElement),
|
|
127
|
-
body
|
|
128
|
-
], null);
|
|
129
|
-
},
|
|
130
|
-
ReturnStatement(node) {
|
|
131
|
-
return UAST.returnStatement(visit(node.argument, opts));
|
|
132
|
-
},
|
|
133
|
-
LabeledStatement(node) {
|
|
134
|
-
return UAST.scopedStatement([visit(node.body, opts)], visit(node.label, opts));
|
|
135
|
-
},
|
|
136
|
-
BreakStatement(node) {
|
|
137
|
-
return UAST.breakStatement(visit(node.label, opts));
|
|
138
|
-
},
|
|
139
|
-
ContinueStatement(node) {
|
|
140
|
-
return UAST.continueStatement(visit(node.label, opts));
|
|
141
|
-
},
|
|
142
|
-
IfStatement(node) {
|
|
143
|
-
return UAST.ifStatement(visit(node.test, opts), visit(node.consequent, opts), visit(node.alternate, opts));
|
|
144
|
-
},
|
|
145
|
-
SwitchStatement(node) {
|
|
146
|
-
return UAST.switchStatement(visit(node.discriminant, opts), node.cases.map((c) => {
|
|
147
|
-
return visit(c, opts);
|
|
148
|
-
}));
|
|
149
|
-
},
|
|
150
|
-
SwitchCase(node) {
|
|
151
|
-
return UAST.caseClause(visit(node.test, opts), UAST.scopedStatement(node.consequent.map((stmt) => {
|
|
152
|
-
return visit(stmt, opts);
|
|
153
|
-
})));
|
|
154
|
-
},
|
|
155
|
-
ThrowStatement(node) {
|
|
156
|
-
return UAST.throwStatement(visit(node.argument, opts));
|
|
157
|
-
},
|
|
158
|
-
TryStatement(node) {
|
|
159
|
-
let handlers;
|
|
160
|
-
if (node.handler) {
|
|
161
|
-
handlers = [visit(node.handler, opts)];
|
|
162
|
-
}
|
|
163
|
-
return UAST.tryStatement(visit(node.block, opts), handlers, visit(node.finalizer, opts));
|
|
164
|
-
},
|
|
165
|
-
CatchClause(node) {
|
|
166
|
-
const body = visit(node.body, opts);
|
|
167
|
-
const parameter = [];
|
|
168
|
-
if (!node.param) {
|
|
169
|
-
return UAST.catchClause(parameter, body);
|
|
170
|
-
}
|
|
171
|
-
else if (AST.isIdentifier(node.param)) {
|
|
172
|
-
const visited = visit(node.param, opts);
|
|
173
|
-
const decl = UAST.variableDeclaration(visited, null, false, UAST.dynamicType());
|
|
174
|
-
decl.loc = visited.loc;
|
|
175
|
-
parameter.push(decl);
|
|
176
|
-
return UAST.catchClause(parameter, body);
|
|
177
|
-
}
|
|
178
|
-
else { //patternLike
|
|
179
|
-
const varDecl = createTmpVariableDeclaration();
|
|
180
|
-
const seq = _visitPatternLike(node.param, varDecl.id, opts);
|
|
181
|
-
seq.expressions.push(body);
|
|
182
|
-
parameter.push(varDecl);
|
|
183
|
-
return UAST.catchClause(parameter, seq);
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
WhileStatement(node) {
|
|
187
|
-
return UAST.whileStatement(visit(node.test, opts), visit(node.body, opts), false);
|
|
188
|
-
},
|
|
189
|
-
DoWhileStatement(node) {
|
|
190
|
-
return UAST.whileStatement(visit(node.test, opts), visit(node.body, opts), true);
|
|
191
|
-
},
|
|
192
|
-
ForStatement(node) {
|
|
193
|
-
return UAST.forStatement(visit(node.init, opts), visit(node.test, opts), visit(node.update, opts), visit(node.body, opts));
|
|
194
|
-
},
|
|
195
|
-
ForInStatement(node) {
|
|
196
|
-
return _visitRangeStatement(node, opts);
|
|
197
|
-
},
|
|
198
|
-
ForOfStatement(node) {
|
|
199
|
-
// TODO 对于Map的考虑 e.g. for( const [a, b] of map )
|
|
200
|
-
return _visitRangeStatement(node, opts);
|
|
201
|
-
},
|
|
202
|
-
FunctionDeclaration(node) {
|
|
203
|
-
const res = _visitFunctionLike(node, opts);
|
|
204
|
-
if (node.async) {
|
|
205
|
-
res._meta.async = true;
|
|
206
|
-
}
|
|
207
|
-
if (node.generator) {
|
|
208
|
-
res._meta.generator = true;
|
|
209
|
-
}
|
|
210
|
-
return res;
|
|
211
|
-
},
|
|
212
|
-
// VariableDeclaration 在两个场景下使用:
|
|
213
|
-
// 1. 作为Statement
|
|
214
|
-
// 2. 作为init存在于 For-like Statment
|
|
215
|
-
// visit 只处理情况1, 情况2放在For like 里单独处理
|
|
216
|
-
VariableDeclaration(node) {
|
|
217
|
-
const resList = [];
|
|
218
|
-
const attachList = [];
|
|
219
|
-
node.declarations.forEach((d) => {
|
|
220
|
-
attachList.push(visit(d, opts));
|
|
221
|
-
});
|
|
222
|
-
const seq = _flatSequence(attachList);
|
|
223
|
-
if (seq.expressions.length === 1) {
|
|
224
|
-
return seq.expressions[0];
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
return seq;
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
VariableDeclarator(node) {
|
|
231
|
-
if (AST.isIdentifier(node.id)) {
|
|
232
|
-
let decl;
|
|
233
|
-
// 新增 如果是ArrayExpression 则相应variable的vartype设置为arraytype
|
|
234
|
-
if (node?.init?.type === "ArrayExpression") {
|
|
235
|
-
decl = UAST.variableDeclaration(visit(node.id, opts), visit(node.init, opts), false, UAST.arrayType(UAST.identifier(node.id.name), UAST.dynamicType()));
|
|
236
|
-
return UAST.sequence([decl]);
|
|
237
|
-
}
|
|
238
|
-
decl = UAST.variableDeclaration(visit(node.id, opts), visit(node.init, opts), false, UAST.dynamicType());
|
|
239
|
-
decl.loc = node.loc;
|
|
240
|
-
return UAST.sequence([decl]);
|
|
241
|
-
}
|
|
242
|
-
if (AST.isObjectPattern(node.id) || AST.isArrayPattern(node.id)) {
|
|
243
|
-
if (!node.init) {
|
|
244
|
-
throw new Error('init is expected when id is pattern');
|
|
245
|
-
}
|
|
246
|
-
const tmpVarDecl = createTmpVariableDeclaration(visit(node.init, opts), false);
|
|
247
|
-
const sequence = _visitPatternLike(node.id, tmpVarDecl.id, opts);
|
|
248
|
-
sequence.expressions.unshift(tmpVarDecl);
|
|
249
|
-
return sequence;
|
|
250
|
-
}
|
|
251
|
-
// impossible
|
|
252
|
-
throw new Error('VariableDeclarator id can not be any thing but id or pattern');
|
|
253
|
-
},
|
|
254
|
-
// Expressions
|
|
255
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
256
|
-
Super(node) {
|
|
257
|
-
return UAST.superExpression();
|
|
258
|
-
},
|
|
259
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
260
|
-
ThisExpression(node) {
|
|
261
|
-
return UAST.thisExpression();
|
|
262
|
-
},
|
|
263
|
-
ArrowFunctionExpression(node) {
|
|
264
|
-
const res = _visitFunctionLike(node, opts);
|
|
265
|
-
if (node.async) {
|
|
266
|
-
res._meta.async = true;
|
|
267
|
-
}
|
|
268
|
-
if (node.generator) {
|
|
269
|
-
res._meta.generator = true;
|
|
270
|
-
}
|
|
271
|
-
return res;
|
|
272
|
-
},
|
|
273
|
-
YieldExpression(node) {
|
|
274
|
-
return UAST.returnStatement(visit(node.argument, opts), true);
|
|
275
|
-
},
|
|
276
|
-
AwaitExpression(node) {
|
|
277
|
-
return visit(node.argument, opts);
|
|
278
|
-
},
|
|
279
|
-
ArrayExpression(node) {
|
|
280
|
-
const objProperties = [];
|
|
281
|
-
node.elements.forEach((e, index) => {
|
|
282
|
-
if (AST.isSpreadElement(e)) {
|
|
283
|
-
objProperties.push(visit(e, opts));
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
if (e !== null) {
|
|
287
|
-
objProperties.push(UAST.objectProperty(UAST.literal(index, 'number'), visit(e, opts)));
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
objProperties.push(UAST.objectProperty(UAST.literal(index, 'number'), UAST.literal(null, 'null')));
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
const uastNode = UAST.objectExpression(objProperties);
|
|
295
|
-
uastNode._meta.isArray = true;
|
|
296
|
-
return uastNode;
|
|
297
|
-
},
|
|
298
|
-
ObjectExpression(node) {
|
|
299
|
-
const objProperties = [];
|
|
300
|
-
node.properties.forEach((p) => {
|
|
301
|
-
objProperties.push(visit(p, opts));
|
|
302
|
-
});
|
|
303
|
-
return UAST.objectExpression(objProperties);
|
|
304
|
-
},
|
|
305
|
-
ObjectProperty(node) {
|
|
306
|
-
return UAST.objectProperty(visit(node.key, opts), visit(node.value, opts));
|
|
307
|
-
},
|
|
308
|
-
ObjectMethod(node) {
|
|
309
|
-
const key = visit(node.key, opts);
|
|
310
|
-
const value = _visitFunctionLike(node, opts);
|
|
311
|
-
return UAST.objectProperty(key, value);
|
|
312
|
-
},
|
|
313
|
-
RecordExpression(node) {
|
|
314
|
-
const objProperties = [];
|
|
315
|
-
node.properties.forEach((p) => {
|
|
316
|
-
objProperties.push(visit(p, opts));
|
|
317
|
-
});
|
|
318
|
-
return UAST.objectExpression(objProperties);
|
|
319
|
-
},
|
|
320
|
-
TupleExpression(node) {
|
|
321
|
-
return UAST.tupleExpression(node.elements.map((e) => {
|
|
322
|
-
return visit(e, opts);
|
|
323
|
-
}));
|
|
324
|
-
},
|
|
325
|
-
FunctionExpression(node) {
|
|
326
|
-
return _visitFunctionLike(node, opts);
|
|
327
|
-
},
|
|
328
|
-
UnaryExpression(node) {
|
|
329
|
-
return UAST.unaryExpression(node.operator, visit(node.argument, opts), !node.prefix);
|
|
330
|
-
},
|
|
331
|
-
UpdateExpression(node) {
|
|
332
|
-
return UAST.unaryExpression(node.operator, visit(node.argument, opts), !node.prefix);
|
|
333
|
-
},
|
|
334
|
-
BinaryExpression(node) {
|
|
335
|
-
let operator = node.operator;
|
|
336
|
-
if (operator === '===') {
|
|
337
|
-
operator = '==';
|
|
338
|
-
}
|
|
339
|
-
return UAST.binaryExpression(node.operator, visit(node.left, opts), visit(node.right, opts));
|
|
340
|
-
},
|
|
341
|
-
AssignmentExpression(node) {
|
|
342
|
-
if (AST.isObjectPattern(node.left) || AST.isArrayPattern(node.left)) {
|
|
343
|
-
const varDecl = createTmpVariableDeclaration(visit(node.right, opts));
|
|
344
|
-
varDecl.loc = node.right.loc;
|
|
345
|
-
const seq = _visitPatternLike(node.left, varDecl.id, opts);
|
|
346
|
-
seq.expressions.unshift(varDecl);
|
|
347
|
-
return seq;
|
|
348
|
-
}
|
|
349
|
-
return UAST.assignmentExpression(visit(node.left, opts), visit(node.right, opts), node.operator, false);
|
|
350
|
-
},
|
|
351
|
-
LogicalExpression(node) {
|
|
352
|
-
return UAST.binaryExpression(node.operator, visit(node.left, opts), visit(node.right, opts));
|
|
353
|
-
},
|
|
354
|
-
SpreadElement(node) {
|
|
355
|
-
return UAST.spreadElement(visit(node.argument, opts));
|
|
356
|
-
},
|
|
357
|
-
RestElement(node) {
|
|
358
|
-
return UAST.spreadElement(visit(node.argument, opts));
|
|
359
|
-
},
|
|
360
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
361
|
-
ArgumentPlaceholder(node) {
|
|
362
|
-
return UAST.noop();
|
|
363
|
-
},
|
|
364
|
-
MemberExpression(node) {
|
|
365
|
-
return UAST.memberAccess(visit(node.object, opts), visit(node.property, opts), node.computed);
|
|
366
|
-
},
|
|
367
|
-
OptionalMemberExpression(node) {
|
|
368
|
-
return UAST.conditionalExpression(visit(node.object, opts), UAST.literal(null, 'null'), UAST.memberAccess(visit(node.object, opts), visit(node.property, opts), node.computed));
|
|
369
|
-
},
|
|
370
|
-
//TODO
|
|
371
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
372
|
-
BindExpression(node) {
|
|
373
|
-
throw new Error('BindExpression is not been implemented');
|
|
374
|
-
},
|
|
375
|
-
ConditionalExpression(node) {
|
|
376
|
-
return UAST.conditionalExpression(visit(node.test, opts), visit(node.consequent, opts), visit(node.alternate, opts));
|
|
377
|
-
},
|
|
378
|
-
CallExpression(node) {
|
|
379
|
-
return UAST.callExpression(visit(node.callee, opts), node.arguments.map((arg) => {
|
|
380
|
-
return visit(arg, opts);
|
|
381
|
-
}));
|
|
382
|
-
},
|
|
383
|
-
OptionalCallExpression(node) {
|
|
384
|
-
return UAST.conditionalExpression(visit(node.callee, opts), UAST.literal(null, 'null'), UAST.callExpression(visit(node.callee, opts), node.arguments.map((arg) => {
|
|
385
|
-
return visit(arg, opts);
|
|
386
|
-
})));
|
|
387
|
-
},
|
|
388
|
-
NewExpression(node) {
|
|
389
|
-
return UAST.newExpression(visit(node.callee, opts), node.arguments.map((arg) => {
|
|
390
|
-
return visit(arg, opts);
|
|
391
|
-
}));
|
|
392
|
-
},
|
|
393
|
-
SequenceExpression(node) {
|
|
394
|
-
return UAST.sequence(node.expressions.map((expr) => {
|
|
395
|
-
return visit(expr, opts);
|
|
396
|
-
}));
|
|
397
|
-
},
|
|
398
|
-
ParenthesizedExpression(node) {
|
|
399
|
-
return visit(node.expression, opts);
|
|
400
|
-
},
|
|
401
|
-
DoExpression(node) {
|
|
402
|
-
return visit(node.body, opts);
|
|
403
|
-
},
|
|
404
|
-
ModuleExpression(node) {
|
|
405
|
-
return UAST.scopedStatement(node.body.body.map((stmt) => {
|
|
406
|
-
return visit(stmt, opts);
|
|
407
|
-
}));
|
|
408
|
-
},
|
|
409
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
410
|
-
TopicReference(node) {
|
|
411
|
-
return UAST.noop();
|
|
412
|
-
},
|
|
413
|
-
// TODO
|
|
414
|
-
// TemplateLiteral
|
|
415
|
-
// TaggedTemplateExpression
|
|
416
|
-
// TemplateElement
|
|
417
|
-
ClassBody() {
|
|
418
|
-
throw new Error('ClassBody should not be visited');
|
|
419
|
-
},
|
|
420
|
-
ClassMethod(node) {
|
|
421
|
-
const res = _visitFunctionLike(node, opts);
|
|
422
|
-
if (node.kind === 'constructor') {
|
|
423
|
-
res._meta.isConstructor = true;
|
|
424
|
-
}
|
|
425
|
-
if (node.async) {
|
|
426
|
-
res._meta.async = true;
|
|
427
|
-
}
|
|
428
|
-
if (node.generator) {
|
|
429
|
-
res._meta.generator = true;
|
|
430
|
-
}
|
|
431
|
-
if (node.static) {
|
|
432
|
-
res._meta.static = true;
|
|
433
|
-
}
|
|
434
|
-
return res;
|
|
435
|
-
},
|
|
436
|
-
ClassPrivateMethod(node) {
|
|
437
|
-
return _visitFunctionLike(node, opts);
|
|
438
|
-
},
|
|
439
|
-
ClassProperty(node) {
|
|
440
|
-
if (node.typeAnnotation && node.typeAnnotation != null) {
|
|
441
|
-
if (node.typeAnnotation.type === "TSTypeAnnotation") {
|
|
442
|
-
const tsTypeAnnotation = node.typeAnnotation;
|
|
443
|
-
if (tsTypeAnnotation?.typeAnnotation?.type === "TSTypeReference") {
|
|
444
|
-
if ("typeName" in node.typeAnnotation.typeAnnotation && node.typeAnnotation.typeAnnotation?.typeName?.type === "Identifier") {
|
|
445
|
-
const type = node.typeAnnotation.typeAnnotation?.typeName;
|
|
446
|
-
return UAST.variableDeclaration(visit(node.key, opts), visit(node.value, opts), false, UAST.scopedType(visit(type, opts), null));
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
return UAST.variableDeclaration(visit(node.key, opts), visit(node.value, opts), false, UAST.dynamicType());
|
|
452
|
-
},
|
|
453
|
-
ClassPrivateProperty(node) {
|
|
454
|
-
if (node.typeAnnotation && node.typeAnnotation != null) {
|
|
455
|
-
if (node.typeAnnotation.type === "TSTypeAnnotation") {
|
|
456
|
-
const tsTypeAnnotation = node.typeAnnotation;
|
|
457
|
-
if (tsTypeAnnotation?.typeAnnotation?.type === "TSTypeReference") {
|
|
458
|
-
if ("typeName" in node.typeAnnotation.typeAnnotation && node.typeAnnotation.typeAnnotation?.typeName?.type === "Identifier") {
|
|
459
|
-
const type = node.typeAnnotation.typeAnnotation?.typeName;
|
|
460
|
-
return UAST.variableDeclaration(visit(node.key, opts), visit(node.value, opts), false, UAST.scopedType(visit(type, opts), null));
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return UAST.variableDeclaration(visit(node.key, opts), visit(node.value, opts), false, UAST.dynamicType());
|
|
466
|
-
},
|
|
467
|
-
StaticBlock(node) {
|
|
468
|
-
return UAST.scopedStatement(node.body.map((stmt) => visit(stmt, opts)));
|
|
469
|
-
},
|
|
470
|
-
ClassDeclaration(node) {
|
|
471
|
-
return UAST.classDefinition(visit(node.id, opts), node.body.body.map((n) => {
|
|
472
|
-
return visit(n, opts);
|
|
473
|
-
}), [visit(node.superClass, opts)]);
|
|
474
|
-
},
|
|
475
|
-
ClassExpression(node) {
|
|
476
|
-
return UAST.classDefinition(visit(node.id, opts), node.body.body.map((n) => {
|
|
477
|
-
return visit(n, opts);
|
|
478
|
-
}), [visit(node.superClass, opts)]);
|
|
479
|
-
},
|
|
480
|
-
MetaProperty(node) {
|
|
481
|
-
return UAST.objectProperty(visit(node.meta, opts), visit(node.property, opts));
|
|
482
|
-
},
|
|
483
|
-
ImportDeclaration(node) {
|
|
484
|
-
const importExpression = UAST.importExpression(visit(node.source, opts));
|
|
485
|
-
const exportVal = createTmpVariableDeclaration(importExpression);
|
|
486
|
-
const sequence = node.specifiers.map((n) => {
|
|
487
|
-
const specifierDecl = visit(n, opts);
|
|
488
|
-
if (AST.isImportSpecifier(n)) {
|
|
489
|
-
specifierDecl.init = UAST.memberAccess(exportVal.id, visit(n.imported, opts), false);
|
|
490
|
-
}
|
|
491
|
-
else if (AST.isImportDefaultSpecifier(n)) {
|
|
492
|
-
specifierDecl.init = UAST.memberAccess(exportVal.id, UAST.identifier('default'), false);
|
|
493
|
-
}
|
|
494
|
-
else { // ImportNamespaceSpecifier
|
|
495
|
-
specifierDecl.init = exportVal.id;
|
|
496
|
-
}
|
|
497
|
-
return specifierDecl;
|
|
498
|
-
});
|
|
499
|
-
sequence.unshift(exportVal);
|
|
500
|
-
return UAST.sequence(sequence);
|
|
501
|
-
},
|
|
502
|
-
ImportSpecifier(node) {
|
|
503
|
-
return UAST.variableDeclaration(visit(node.local, opts), null, false, UAST.dynamicType());
|
|
504
|
-
},
|
|
505
|
-
ImportDefaultSpecifier(node) {
|
|
506
|
-
return UAST.variableDeclaration(visit(node.local, opts), null, false, UAST.dynamicType());
|
|
507
|
-
},
|
|
508
|
-
ImportNamespaceSpecifier(node) {
|
|
509
|
-
return UAST.variableDeclaration(visit(node.local, opts), null, false, UAST.dynamicType());
|
|
510
|
-
},
|
|
511
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
512
|
-
ImportAttribute(node) {
|
|
513
|
-
throw new Error('ImportAttribute is not implemented yet');
|
|
514
|
-
},
|
|
515
|
-
ExportNamedDeclaration(node) {
|
|
516
|
-
// case 1: export let x = 1;
|
|
517
|
-
if (node.declaration) {
|
|
518
|
-
if ('id' in node.declaration) {
|
|
519
|
-
return UAST.sequence([visit(node.declaration, opts), UAST.exportStatement(visit(node.declaration, opts), visit(node.declaration.id, opts))]);
|
|
520
|
-
}
|
|
521
|
-
else if ('declarations' in node.declaration) {
|
|
522
|
-
return UAST.sequence([visit(node.declaration, opts), UAST.exportStatement(visit(node.declaration.declarations[0].id, opts), visit(node.declaration.declarations[0].id, opts))]);
|
|
523
|
-
}
|
|
524
|
-
throw new Error('ExportNamedDeclaration: declaration should have id or declarations');
|
|
525
|
-
}
|
|
526
|
-
// export ... from 'xx'
|
|
527
|
-
if (node.source) {
|
|
528
|
-
const importVal = createTmpVariableDeclaration(UAST.importExpression(visit(node.source, opts)));
|
|
529
|
-
const sequence = UAST.sequence([]);
|
|
530
|
-
const scopeList = sequence.expressions;
|
|
531
|
-
scopeList.push(importVal);
|
|
532
|
-
node.specifiers.map((specifier) => {
|
|
533
|
-
if (AST.isExportSpecifier(specifier)) {
|
|
534
|
-
const exportId = visit(specifier.exported, opts);
|
|
535
|
-
scopeList.push(UAST.variableDeclaration(exportId, UAST.memberAccess(importVal.id, visit(specifier.local, opts)), false, UAST.dynamicType()));
|
|
536
|
-
scopeList.push(UAST.exportStatement(exportId, exportId));
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
return sequence;
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
const seqList = node.specifiers.map((specifier) => {
|
|
543
|
-
if (AST.isExportSpecifier(specifier)) {
|
|
544
|
-
return UAST.exportStatement(visit(specifier.local, opts), visit(specifier.exported, opts));
|
|
545
|
-
}
|
|
546
|
-
throw new Error('specifier should only be ExportSpecifier');
|
|
547
|
-
});
|
|
548
|
-
return UAST.sequence(seqList);
|
|
549
|
-
}
|
|
550
|
-
},
|
|
551
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
552
|
-
ExportSpecifier(node) {
|
|
553
|
-
throw new Error('ExportSpecifier should not be visited, since it has processed where it occurs');
|
|
554
|
-
},
|
|
555
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
556
|
-
ExportDefaultSpecifier(node) {
|
|
557
|
-
throw new Error('ExportDefaultSpecifier should not be visited, since it has processed where it occurs');
|
|
558
|
-
},
|
|
559
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
560
|
-
ExportNamespaceSpecifier(node) {
|
|
561
|
-
throw new Error('ExportNamespaceSpecifier should not be visited, since it has processed where it occurs');
|
|
562
|
-
},
|
|
563
|
-
ExportDefaultDeclaration(node) {
|
|
564
|
-
const importVal = createTmpVariableDeclaration(visit(node.declaration, opts));
|
|
565
|
-
const scope = UAST.scopedStatement([]);
|
|
566
|
-
const scopeList = scope.body;
|
|
567
|
-
scopeList.push(importVal);
|
|
568
|
-
scopeList.push(UAST.exportStatement(importVal.id, UAST.identifier('default')));
|
|
569
|
-
return scope;
|
|
570
|
-
},
|
|
571
|
-
ExportAllDeclaration(node) {
|
|
572
|
-
// const importVal = createTmpVariableDeclaration(UAST.importExpression(visit(node.source) as UAST.Literal));
|
|
573
|
-
// const tmpVar = createTmpVariableDeclaration(null, false, node);
|
|
574
|
-
// const tmpVarId = tmpVar.id;
|
|
575
|
-
// return UAST.rangeStatement(null, tmpVar, importVal, UAST.exportStatement(tmpVarId, tmpVarId));
|
|
576
|
-
return UAST.spreadElement(UAST.importExpression(visit(node.source, opts)));
|
|
577
|
-
},
|
|
578
|
-
Decorator(node) {
|
|
579
|
-
return visit(node.expression, opts);
|
|
580
|
-
},
|
|
581
|
-
/** typescript node processing **/
|
|
582
|
-
TSAsExpression(node) {
|
|
583
|
-
return visit(node.expression, opts);
|
|
584
|
-
},
|
|
585
|
-
TSNonNullExpression(node) {
|
|
586
|
-
return visit(node.expression, opts);
|
|
587
|
-
},
|
|
588
|
-
TSTypeAssertion(node) {
|
|
589
|
-
return visit(node.expression, opts);
|
|
590
|
-
},
|
|
591
|
-
TSEnumDeclaration(node) {
|
|
592
|
-
const objectProperties = [];
|
|
593
|
-
let lastValue = UAST.literal(0, 'number');
|
|
594
|
-
for (const prop of node.members) {
|
|
595
|
-
const op = visit(prop, opts);
|
|
596
|
-
if (!op.value) {
|
|
597
|
-
op.value = UAST.binaryExpression('+', lastValue, UAST.literal(1, 'number'));
|
|
598
|
-
lastValue = op.value;
|
|
599
|
-
}
|
|
600
|
-
objectProperties.push(op);
|
|
601
|
-
}
|
|
602
|
-
return UAST.objectExpression(objectProperties, visit(node.id, opts));
|
|
603
|
-
},
|
|
604
|
-
TSEnumMember(node) {
|
|
605
|
-
return UAST.objectProperty(visit(node.id, opts), visit(node.initializer, opts));
|
|
606
|
-
},
|
|
607
|
-
// FIXME: 接口内部属性visitor待增加
|
|
608
|
-
TSInterfaceDeclaration(node) {
|
|
609
|
-
return UAST.classDefinition(visit(node.id, opts), [], []);
|
|
610
|
-
},
|
|
611
|
-
TSTypeAliasDeclaration(node) {
|
|
612
|
-
return UAST.noop();
|
|
613
|
-
},
|
|
614
|
-
TSImportEqualsDeclaration(node) {
|
|
615
|
-
const importExpression = UAST.importExpression(visit(node.moduleReference, opts));
|
|
616
|
-
const id = visit(node.id, opts);
|
|
617
|
-
return UAST.variableDeclaration(id, importExpression, false, UAST.dynamicType());
|
|
618
|
-
},
|
|
619
|
-
TSExternalModuleReference(node) {
|
|
620
|
-
return visit(node.expression, opts);
|
|
621
|
-
},
|
|
622
|
-
TSModuleDeclaration(node) {
|
|
623
|
-
return UAST.noop();
|
|
624
|
-
},
|
|
625
|
-
TSParameterProperty(node) {
|
|
626
|
-
return visit(node.parameter, opts);
|
|
627
|
-
},
|
|
628
|
-
TSDeclareMethod(node) {
|
|
629
|
-
return UAST.noop();
|
|
630
|
-
},
|
|
631
|
-
TSInstantiationExpression(node) {
|
|
632
|
-
return visit(node.expression, opts);
|
|
633
|
-
},
|
|
634
|
-
TSDeclareFunction(node) {
|
|
635
|
-
return UAST.noop();
|
|
636
|
-
},
|
|
637
|
-
//TSX
|
|
638
|
-
JSXElement(node) {
|
|
639
|
-
return UAST.noop();
|
|
640
|
-
}
|
|
641
|
-
});
|
|
642
|
-
function _visitObjectPattern(node, initExpr, opts) {
|
|
643
|
-
const sequence = UAST.sequence([]);
|
|
644
|
-
const exprList = sequence.expressions;
|
|
645
|
-
const consumedKey = [];
|
|
646
|
-
node.properties.forEach((objProperty) => {
|
|
647
|
-
//processObjectProperty
|
|
648
|
-
if (!AST.isRestElement(objProperty)) {
|
|
649
|
-
const { key, value } = objProperty;
|
|
650
|
-
const memberAccess = UAST.memberAccess(initExpr, visit(key, opts));
|
|
651
|
-
consumedKey.push(visit(key, opts));
|
|
652
|
-
if (AST.isIdentifier(value)) {
|
|
653
|
-
const varDecl = UAST.variableDeclaration(visit(value, opts), memberAccess, false, UAST.dynamicType());
|
|
654
|
-
exprList.push(varDecl);
|
|
655
|
-
}
|
|
656
|
-
else if (AST.isPatternLike(value)) { // process pattern like
|
|
657
|
-
exprList.push(..._visitPatternLike(value, memberAccess, opts).expressions);
|
|
658
|
-
}
|
|
659
|
-
else {
|
|
660
|
-
throw new Error(`unexpected type ${value.type}`);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
else { // rest element
|
|
664
|
-
const varDecl = UAST.variableDeclaration(visit(objProperty.argument, opts), initExpr, true, UAST.dynamicType());
|
|
665
|
-
const varId = varDecl.id;
|
|
666
|
-
exprList.push(varDecl);
|
|
667
|
-
consumedKey.forEach((k) => {
|
|
668
|
-
const ma = UAST.memberAccess(varId, k);
|
|
669
|
-
const deleteExpression = UAST.unaryExpression('delete', ma, true);
|
|
670
|
-
exprList.push(deleteExpression);
|
|
671
|
-
});
|
|
672
|
-
const assignmentExpression = UAST.assignmentExpression(visit(objProperty.argument, opts), varId, '=');
|
|
673
|
-
exprList.push(assignmentExpression);
|
|
674
|
-
}
|
|
675
|
-
});
|
|
676
|
-
return sequence;
|
|
677
|
-
}
|
|
678
|
-
// const [a,b,c] = [a1, b1, c1];
|
|
679
|
-
// transformed to:
|
|
680
|
-
// const {1:a, 2:b, 3:c} = [a1, b1, c1];
|
|
681
|
-
function _visitArrayPattern(node, initExpr, opts) {
|
|
682
|
-
const exprList = [];
|
|
683
|
-
const consumed = [];
|
|
684
|
-
for (let i = 0; i < node.elements.length; i++) {
|
|
685
|
-
const ele = node.elements[i];
|
|
686
|
-
if (ele) {
|
|
687
|
-
if (AST.isRestElement(ele)) {
|
|
688
|
-
const varDecl = createTmpVariableDeclaration(initExpr, true);
|
|
689
|
-
exprList.push(varDecl);
|
|
690
|
-
const varId = varDecl.id;
|
|
691
|
-
for (const c of consumed) {
|
|
692
|
-
const ma = UAST.memberAccess(varId, c);
|
|
693
|
-
const deleteExpression = UAST.unaryExpression('delete', ma, true);
|
|
694
|
-
exprList.push(deleteExpression);
|
|
695
|
-
}
|
|
696
|
-
const assignmentExpression = UAST.assignmentExpression(visit(ele.argument, opts), varId, '=');
|
|
697
|
-
exprList.push(assignmentExpression);
|
|
698
|
-
}
|
|
699
|
-
else {
|
|
700
|
-
const memberAccess = UAST.memberAccess(initExpr, UAST.identifier(String(i)));
|
|
701
|
-
const seq = _visitPatternLike(ele, memberAccess, opts);
|
|
702
|
-
exprList.push(seq);
|
|
703
|
-
consumed.push(UAST.identifier(String(i)));
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return _flatSequence(exprList);
|
|
708
|
-
}
|
|
709
|
-
function _visitAssignmentPattern(node, initExpr, opts) {
|
|
710
|
-
const condExpr = UAST.conditionalExpression(initExpr, initExpr, visit(node.right, opts));
|
|
711
|
-
return _visitPatternLike(node.left, condExpr, opts);
|
|
712
|
-
}
|
|
713
|
-
function _visitPatternLike(node, initExpr, opts) {
|
|
714
|
-
if (AST.isIdentifier(node)) {
|
|
715
|
-
return UAST.sequence([UAST.variableDeclaration(visit(node, opts), initExpr, false, UAST.dynamicType())]);
|
|
716
|
-
}
|
|
717
|
-
if (AST.isObjectPattern(node)) {
|
|
718
|
-
return _visitObjectPattern(node, initExpr, opts);
|
|
719
|
-
}
|
|
720
|
-
if (AST.isArrayPattern(node)) {
|
|
721
|
-
return _visitArrayPattern(node, initExpr, opts);
|
|
722
|
-
}
|
|
723
|
-
if (AST.isAssignmentPattern(node)) {
|
|
724
|
-
return _visitAssignmentPattern(node, initExpr, opts);
|
|
725
|
-
}
|
|
726
|
-
if (AST.isMemberExpression(node)) {
|
|
727
|
-
return UAST.sequence([UAST.assignmentExpression(visit(node, opts), initExpr, '=')]);
|
|
728
|
-
}
|
|
729
|
-
if (AST.isRestElement(node)) {
|
|
730
|
-
return UAST.sequence([UAST.assignmentExpression(visit(node.argument, opts), initExpr, '=')]);
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
function _visitFunctionLike(node, opts) {
|
|
734
|
-
const attachBodyList = [];
|
|
735
|
-
// @ts-ignore
|
|
736
|
-
const id = visit(node.key || node.id);
|
|
737
|
-
// for ArrowFunctionExpression
|
|
738
|
-
let body = AST.isExpression(node.body) ? UAST.scopedStatement([UAST.returnStatement(visit(node.body, opts))]) :
|
|
739
|
-
visit(node.body, opts);
|
|
740
|
-
const params = node.params.map((param) => {
|
|
741
|
-
if (AST.isTSParameterProperty(param)) {
|
|
742
|
-
param = param.parameter;
|
|
743
|
-
}
|
|
744
|
-
if (AST.isObjectPattern(param) || AST.isArrayPattern(param) || AST.isAssignmentPattern(param) || AST.isRestElement(param)) {
|
|
745
|
-
const varDecl = createTmpVariableDeclaration();
|
|
746
|
-
if (AST.isRestElement(param)) {
|
|
747
|
-
varDecl._meta.isRestElement = true;
|
|
748
|
-
}
|
|
749
|
-
attachBodyList.push(..._visitPatternLike(param, varDecl.id, opts).expressions);
|
|
750
|
-
varDecl.loc = param.loc;
|
|
751
|
-
return varDecl;
|
|
752
|
-
}
|
|
753
|
-
// type of param is Identifier
|
|
754
|
-
const id = visit(param, opts);
|
|
755
|
-
const decl = UAST.variableDeclaration(id, null, false, UAST.dynamicType());
|
|
756
|
-
decl.loc = id.loc;
|
|
757
|
-
return decl;
|
|
758
|
-
});
|
|
759
|
-
attachBodyList.push(body);
|
|
760
|
-
if (attachBodyList.length > 0) {
|
|
761
|
-
body = UAST.scopedStatement([...attachBodyList]);
|
|
762
|
-
}
|
|
763
|
-
const res = UAST.functionDefinition(id, params, UAST.dynamicType(), body, []);
|
|
764
|
-
return res;
|
|
765
|
-
}
|
|
766
|
-
function _visitRangeStatement(node, opts) {
|
|
767
|
-
const right = visit(node.right, opts);
|
|
768
|
-
//TODO this should be a iterator
|
|
769
|
-
const keyOrValue = createTmpVariableDeclaration(right);
|
|
770
|
-
keyOrValue.loc = node.left.loc;
|
|
771
|
-
let body = visit(node.body, opts);
|
|
772
|
-
if (AST.isVariableDeclaration(node.left)) {
|
|
773
|
-
// 有且仅能有一个decl
|
|
774
|
-
const seq = _visitPatternLike(node.left.declarations[0].id, keyOrValue.id, opts);
|
|
775
|
-
const seqExpressions = [seq, body];
|
|
776
|
-
body = UAST.scopedStatement(seqExpressions);
|
|
777
|
-
}
|
|
778
|
-
else if (AST.isObjectPattern(node.left) || AST.isArrayPattern(node.left) || AST.isAssignmentPattern(node.left)) {
|
|
779
|
-
const seq = _visitPatternLike(node.left, keyOrValue.id, opts);
|
|
780
|
-
const seqExpressions = [seq, body];
|
|
781
|
-
body = UAST.scopedStatement(seqExpressions);
|
|
782
|
-
}
|
|
783
|
-
else {
|
|
784
|
-
const expr = UAST.sequence([UAST.assignmentExpression(visit(node.left, opts), keyOrValue.id, '=')]);
|
|
785
|
-
const seqExpressions = [expr, body];
|
|
786
|
-
body = UAST.scopedStatement(seqExpressions);
|
|
787
|
-
}
|
|
788
|
-
if (AST.isForInStatement(node)) {
|
|
789
|
-
return UAST.rangeStatement(keyOrValue, null, right, body);
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
return UAST.rangeStatement(null, keyOrValue, right, body);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
//扁平化优化, 减少sequence层次
|
|
796
|
-
function _flatSequence(node) {
|
|
797
|
-
const sequenceArray = [];
|
|
798
|
-
const res = UAST.sequence(sequenceArray);
|
|
799
|
-
if (Array.isArray(node)) {
|
|
800
|
-
for (const n of node) {
|
|
801
|
-
const subSequence = _flatSequence(n);
|
|
802
|
-
sequenceArray.push(...subSequence.expressions);
|
|
803
|
-
}
|
|
804
|
-
return res;
|
|
805
|
-
}
|
|
806
|
-
else if (UAST.isSequence(node)) {
|
|
807
|
-
return node;
|
|
808
|
-
}
|
|
809
|
-
else {
|
|
810
|
-
sequenceArray.push(node);
|
|
811
|
-
return res;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
function createTmpIdentifier(locNode) {
|
|
815
|
-
if (!getUid) {
|
|
816
|
-
getUid = (() => {
|
|
817
|
-
let id = 0;
|
|
818
|
-
return () => id++;
|
|
819
|
-
})();
|
|
820
|
-
}
|
|
821
|
-
const id = UAST.identifier(`__tmp${getUid()}__`);
|
|
822
|
-
return id;
|
|
823
|
-
}
|
|
824
|
-
exports.createTmpIdentifier = createTmpIdentifier;
|
|
825
|
-
function createTmpVariableDeclaration(init, cloned = false, locNode) {
|
|
826
|
-
locNode = locNode ? locNode : init;
|
|
827
|
-
return UAST.variableDeclaration(createTmpIdentifier(locNode), init, cloned, UAST.dynamicType());
|
|
828
|
-
}
|
|
829
|
-
exports.createTmpVariableDeclaration = createTmpVariableDeclaration;
|
|
830
|
-
function appendLocMeta(dest, src) {
|
|
831
|
-
if (Array.isArray(src)) {
|
|
832
|
-
if (src.length > 0) {
|
|
833
|
-
dest.loc = src[0].loc;
|
|
834
|
-
dest.loc.start = src[0].loc.start;
|
|
835
|
-
dest.loc.end = src[src.length - 1].loc.end;
|
|
836
|
-
if ('sourcefile' in src[0].loc) {
|
|
837
|
-
dest.loc.sourcefile = src[0].loc.sourcefile;
|
|
838
|
-
}
|
|
839
|
-
else if ('filename' in src[0].loc) {
|
|
840
|
-
dest.loc.sourcefile = src[0].loc.filename;
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
else {
|
|
845
|
-
// @ts-ignore
|
|
846
|
-
dest.loc = src.loc;
|
|
847
|
-
}
|
|
848
|
-
dest._meta.loc = dest.loc;
|
|
849
|
-
return dest;
|
|
850
|
-
}
|
|
851
|
-
exports.appendLocMeta = appendLocMeta;
|
|
852
|
-
// append loc to node recursively
|
|
853
|
-
function appendLocRec(node, parent_node, sourcefile) {
|
|
854
|
-
function mergeLocs(locs) {
|
|
855
|
-
if (!locs)
|
|
856
|
-
return null;
|
|
857
|
-
let minLine = Number.MAX_SAFE_INTEGER, minColumn = Number.MAX_SAFE_INTEGER, maxLine = 0, maxColumn = 0;
|
|
858
|
-
locs.forEach(loc => {
|
|
859
|
-
if (!loc)
|
|
860
|
-
return;
|
|
861
|
-
minLine = Math.min(minLine, loc.start.line);
|
|
862
|
-
minColumn = Math.min(minColumn, loc.start.column);
|
|
863
|
-
maxLine = Math.max(maxLine, loc.end.line);
|
|
864
|
-
maxColumn = Math.max(maxColumn, loc.end.column);
|
|
865
|
-
});
|
|
866
|
-
return {
|
|
867
|
-
start: { line: minLine, column: minColumn },
|
|
868
|
-
end: { line: maxLine, column: maxColumn },
|
|
869
|
-
sourcefile: sourcefile
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
if (!node)
|
|
873
|
-
return null;
|
|
874
|
-
if (Array.isArray(node)) {
|
|
875
|
-
const locs = node.map(v => {
|
|
876
|
-
return appendLocRec(v, parent_node, sourcefile);
|
|
877
|
-
});
|
|
878
|
-
return mergeLocs(locs);
|
|
879
|
-
}
|
|
880
|
-
if (!node.type)
|
|
881
|
-
return null;
|
|
882
|
-
const locs = [];
|
|
883
|
-
for (const prop in node) {
|
|
884
|
-
if (['type', 'loc', '_meta', 'varType', 'operator'].indexOf(prop) !== -1) {
|
|
885
|
-
continue;
|
|
886
|
-
}
|
|
887
|
-
const loc = appendLocRec(node[prop], node.loc ? node : parent_node, sourcefile);
|
|
888
|
-
if (loc) {
|
|
889
|
-
locs.push(loc);
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
if (!node.loc) {
|
|
893
|
-
if (parent_node?.loc) {
|
|
894
|
-
node.loc = parent_node.loc;
|
|
895
|
-
if (!node.loc.sourcefile) {
|
|
896
|
-
node.loc.sourcefile = sourcefile;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
else {
|
|
900
|
-
node.loc = mergeLocs(locs);
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return node.loc;
|
|
904
|
-
}
|
|
905
|
-
// function visit(ast: AST.Node & AST.Node[] | {type?:any} = {}): ParseResult<UAST.ASTNode | UAST.ASTNode[] > {
|
|
906
|
-
function visit(orig, opts) {
|
|
907
|
-
if (!orig) {
|
|
908
|
-
// @ts-ignore
|
|
909
|
-
return null;
|
|
910
|
-
}
|
|
911
|
-
// @ts-ignore
|
|
912
|
-
const mVisitor = visitor(opts);
|
|
913
|
-
if (typeof mVisitor[orig.type] !== 'function') {
|
|
914
|
-
console.warn(`There is no visitor function associated with '${orig.type}'`);
|
|
915
|
-
const noop = UAST.noop();
|
|
916
|
-
appendLocRec(noop, orig, opts?.sourcefile);
|
|
917
|
-
noop._meta.origin = orig; // 如果没有找到对应节点的visitor, 将原始ast节点记录到meta.origin中
|
|
918
|
-
return noop;
|
|
919
|
-
}
|
|
920
|
-
// @ts-ignore
|
|
921
|
-
const node = mVisitor[orig.type](orig);
|
|
922
|
-
appendLocRec(node, orig, opts?.sourcefile);
|
|
923
|
-
if (UAST.isNode(node)) {
|
|
924
|
-
if (node.loc) {
|
|
925
|
-
// @ts-ignore
|
|
926
|
-
delete node.loc.start.index;
|
|
927
|
-
// @ts-ignore
|
|
928
|
-
delete node.loc.end.index;
|
|
929
|
-
Object.keys(node.loc).forEach(key => {
|
|
930
|
-
if (!['start', 'end', 'sourcefile'].includes(key)) {
|
|
931
|
-
// @ts-ignore
|
|
932
|
-
delete node.loc[key];
|
|
933
|
-
}
|
|
934
|
-
});
|
|
935
|
-
}
|
|
936
|
-
const metaProperties = ['start', 'end', 'range', 'leadingComments', 'trailingComments', 'innerComments', 'directives', 'interpreter'];
|
|
937
|
-
if (UAST.isNoop(node)) {
|
|
938
|
-
node._meta.origin = orig;
|
|
939
|
-
}
|
|
940
|
-
for (const prop of metaProperties) {
|
|
941
|
-
if (orig && orig[prop]) {
|
|
942
|
-
node._meta._extra = node._meta._extra || {};
|
|
943
|
-
const extra = node._meta._extra;
|
|
944
|
-
extra[prop] = orig[prop];
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
// process decorators
|
|
949
|
-
if ("decorators" in orig) {
|
|
950
|
-
const decorators = [];
|
|
951
|
-
orig.decorators.forEach(decorator => {
|
|
952
|
-
decorators.push(visit(decorator, opts));
|
|
953
|
-
});
|
|
954
|
-
node._meta.decorators = decorators;
|
|
955
|
-
}
|
|
956
|
-
return node;
|
|
957
|
-
}
|
|
958
|
-
function fix_column(node) {
|
|
959
|
-
if (!node)
|
|
960
|
-
return;
|
|
961
|
-
if (Array.isArray(node)) {
|
|
962
|
-
node.forEach(function (n) {
|
|
963
|
-
fix_column(n);
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
if (!node.loc)
|
|
967
|
-
return;
|
|
968
|
-
//uast统一 column从1开始,babel从0开始,因此整体+1
|
|
969
|
-
if (node.loc.start?.column >= 0 && node.loc.start.hasFixColumn === undefined) {
|
|
970
|
-
node.loc.start.column = node.loc.start.column + 1;
|
|
971
|
-
node.loc.start.hasFixColumn = true;
|
|
972
|
-
}
|
|
973
|
-
if (node.loc.end?.column >= 0 && node.loc.end.hasFixColumn === undefined) {
|
|
974
|
-
node.loc.end.column = node.loc.end.column + 1;
|
|
975
|
-
node.loc.end.hasFixColumn = true;
|
|
976
|
-
}
|
|
977
|
-
Object.keys(node).forEach(key => {
|
|
978
|
-
fix_column(node[key]);
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
function sanitize(node) {
|
|
982
|
-
if (!node)
|
|
983
|
-
return;
|
|
984
|
-
if (Array.isArray(node)) {
|
|
985
|
-
node.forEach(function (n) {
|
|
986
|
-
sanitize(n);
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
if (!node.type)
|
|
990
|
-
return;
|
|
991
|
-
// clean up loc
|
|
992
|
-
if (node.loc) {
|
|
993
|
-
// @ts-ignore
|
|
994
|
-
delete node.loc.start.index;
|
|
995
|
-
delete node.loc.end.index;
|
|
996
|
-
delete node.loc.start.hasFixColumn;
|
|
997
|
-
delete node.loc.end.hasFixColumn;
|
|
998
|
-
Object.keys(node.loc).forEach(key => {
|
|
999
|
-
if (!['start', 'end', 'sourcefile'].includes(key)) {
|
|
1000
|
-
// @ts-ignore
|
|
1001
|
-
delete node.loc[key];
|
|
1002
|
-
}
|
|
1003
|
-
});
|
|
1004
|
-
}
|
|
1005
|
-
Object.keys(node).forEach(key => {
|
|
1006
|
-
sanitize(node[key]);
|
|
1007
|
-
});
|
|
1008
|
-
}
|
|
1009
|
-
let getUid;
|
|
1010
|
-
function parse(content, opts) {
|
|
1011
|
-
//init engine state
|
|
1012
|
-
getUid = (() => {
|
|
1013
|
-
let id = 0;
|
|
1014
|
-
return () => id++;
|
|
1015
|
-
})();
|
|
1016
|
-
let ast;
|
|
1017
|
-
try {
|
|
1018
|
-
ast = tryBabelCore(content, opts);
|
|
1019
|
-
}
|
|
1020
|
-
catch (e) {
|
|
1021
|
-
ast = tryBabelParse(content, opts);
|
|
1022
|
-
}
|
|
1023
|
-
const node = visit(ast, opts);
|
|
1024
|
-
fix_column(node);
|
|
1025
|
-
sanitize(node);
|
|
1026
|
-
return node;
|
|
1027
|
-
}
|
|
1028
|
-
exports.parse = parse;
|
|
1029
|
-
function tryBabelParse(content, opts) {
|
|
1030
|
-
return babelParser.parse(content, {
|
|
1031
|
-
sourceFilename: opts?.sourcefile,
|
|
1032
|
-
sourceType: 'module',
|
|
1033
|
-
allowImportExportEverywhere: true,
|
|
1034
|
-
allowAwaitOutsideFunction: true,
|
|
1035
|
-
allowReturnOutsideFunction: true,
|
|
1036
|
-
allowSuperOutsideMethod: true,
|
|
1037
|
-
allowUndeclaredExports: true,
|
|
1038
|
-
attachComment: true,
|
|
1039
|
-
errorRecovery: true,
|
|
1040
|
-
createParenthesizedExpressions: false,
|
|
1041
|
-
plugins: [
|
|
1042
|
-
"asyncDoExpressions",
|
|
1043
|
-
"asyncGenerators",
|
|
1044
|
-
"bigInt",
|
|
1045
|
-
"classPrivateMethods",
|
|
1046
|
-
"classPrivateProperties",
|
|
1047
|
-
"classProperties",
|
|
1048
|
-
"classStaticBlock" // Enabled by default
|
|
1049
|
-
,
|
|
1050
|
-
"decimal",
|
|
1051
|
-
"decorators",
|
|
1052
|
-
["decorators", { decoratorsBeforeExport: true }],
|
|
1053
|
-
"decoratorAutoAccessors",
|
|
1054
|
-
"destructuringPrivate",
|
|
1055
|
-
"doExpressions",
|
|
1056
|
-
"dynamicImport",
|
|
1057
|
-
"exportDefaultFrom",
|
|
1058
|
-
"exportNamespaceFrom" // deprecated
|
|
1059
|
-
// , "decorators-legacy"
|
|
1060
|
-
// , "flow"
|
|
1061
|
-
// , "estree"
|
|
1062
|
-
,
|
|
1063
|
-
"flowComments",
|
|
1064
|
-
"functionBind",
|
|
1065
|
-
"functionSent",
|
|
1066
|
-
"importMeta",
|
|
1067
|
-
"jsx",
|
|
1068
|
-
"logicalAssignment",
|
|
1069
|
-
"importAssertions",
|
|
1070
|
-
"moduleBlocks",
|
|
1071
|
-
"moduleStringNames",
|
|
1072
|
-
"nullishCoalescingOperator",
|
|
1073
|
-
"numericSeparator",
|
|
1074
|
-
"objectRestSpread",
|
|
1075
|
-
"optionalCatchBinding",
|
|
1076
|
-
"optionalChaining",
|
|
1077
|
-
"partialApplication"
|
|
1078
|
-
// , "pipelineOperator"
|
|
1079
|
-
// , ["pipelineOperator", { proposal: "minimal" }]
|
|
1080
|
-
,
|
|
1081
|
-
"placeholders",
|
|
1082
|
-
"privateIn" // Enabled by default
|
|
1083
|
-
,
|
|
1084
|
-
"recordAndTuple",
|
|
1085
|
-
"regexpUnicodeSets",
|
|
1086
|
-
"throwExpressions",
|
|
1087
|
-
"topLevelAwait",
|
|
1088
|
-
"typescript",
|
|
1089
|
-
["typescript", { dts: true }]
|
|
1090
|
-
// , "v8intrinsic"
|
|
1091
|
-
],
|
|
1092
|
-
});
|
|
1093
|
-
}
|
|
1094
|
-
function tryBabelCore(content, opts) {
|
|
1095
|
-
return babelCore.parse(content, {
|
|
1096
|
-
filename: opts?.sourcefile,
|
|
1097
|
-
sourceType: 'module',
|
|
1098
|
-
plugins: [
|
|
1099
|
-
"@babel/plugin-proposal-optional-chaining",
|
|
1100
|
-
"@babel/plugin-syntax-json-strings",
|
|
1101
|
-
"@babel/plugin-syntax-export-default-from",
|
|
1102
|
-
"@babel/plugin-syntax-jsx",
|
|
1103
|
-
"@babel/plugin-transform-react-jsx",
|
|
1104
|
-
["@babel/plugin-proposal-decorators", {
|
|
1105
|
-
decoratorsBeforeExport: true,
|
|
1106
|
-
legacy: false
|
|
1107
|
-
}],
|
|
1108
|
-
],
|
|
1109
|
-
parserOpts: {
|
|
1110
|
-
errorRecovery: true,
|
|
1111
|
-
},
|
|
1112
|
-
presets: [
|
|
1113
|
-
["@babel/preset-typescript", {
|
|
1114
|
-
allExtensions: true,
|
|
1115
|
-
// isTSX:false,
|
|
1116
|
-
}],
|
|
1117
|
-
"@babel/preset-react"
|
|
1118
|
-
],
|
|
1119
|
-
});
|
|
1120
|
-
}
|
|
1121
|
-
//# sourceMappingURL=parser.js.map
|