flow-parser 0.321.0 → 0.322.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/dist/FlowParser.js +3 -53
- package/dist/FlowParserDeserializer.js +3 -41
- package/dist/FlowParserNodeDeserializers.js +0 -7
- package/dist/FlowParserWASM.js +1 -1
- package/dist/HermesParserDecodeUTF8String.js +0 -9
- package/dist/ParserOptions.js +1 -2
- package/dist/babel/TransformESTreeToBabel.js +9 -157
- package/dist/estree/StripFlowTypes.js +0 -26
- package/dist/estree/StripFlowTypesForBabel.js +0 -24
- package/dist/estree/TransformComponentSyntax.js +0 -87
- package/dist/estree/TransformEnumSyntax.js +0 -12
- package/dist/estree/TransformMatchSyntax.js +3 -99
- package/dist/estree/TransformRecordSyntax.js +3 -31
- package/dist/getModuleDocblock.js +0 -21
- package/dist/index.js +25 -99
- package/dist/transform/SimpleTransform.js +0 -21
- package/dist/transform/astArrayMutationHelpers.js +0 -9
- package/dist/transform/astNodeMutationHelpers.js +4 -26
- package/dist/transform/print/comments/comments.js +2 -51
- package/dist/transform/print/comments/prettier/common/util.js +0 -49
- package/dist/transform/print/comments/prettier/language-js/comments.js +0 -111
- package/dist/transform/print/comments/prettier/language-js/loc.js +0 -6
- package/dist/transform/print/comments/prettier/language-js/printer-estree.js +0 -3
- package/dist/transform/print/comments/prettier/language-js/utils.js +0 -20
- package/dist/transform/print/comments/prettier/main/comments.js +2 -63
- package/dist/transform/print/comments/prettier/utils/get-last.js +0 -1
- package/dist/transform/print/print.js +6 -22
- package/dist/traverse/SimpleTraverser.js +2 -22
- package/dist/traverse/getVisitorKeys.js +3 -9
- package/dist/utils/Builders.js +1 -17
- package/dist/utils/GenID.js +0 -7
- package/dist/utils/createSyntaxError.js +0 -1
- package/dist/utils/isReservedWord.js +0 -2
- package/dist/utils/mutateESTreeASTForPrettier.js +6 -21
- package/package.json +2 -2
|
@@ -1,62 +1,49 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
|
-
|
|
5
4
|
const {
|
|
6
5
|
hasNewline,
|
|
7
6
|
addLeadingComment,
|
|
8
7
|
addDanglingComment,
|
|
9
8
|
addTrailingComment
|
|
10
9
|
} = require('../common/util.js');
|
|
11
|
-
|
|
12
10
|
const childNodesCache = new WeakMap();
|
|
13
|
-
|
|
14
11
|
function getSortedChildNodes(node, options, resultArray) {
|
|
15
12
|
if (!node) {
|
|
16
13
|
return;
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
const {
|
|
20
16
|
printer,
|
|
21
17
|
locStart,
|
|
22
18
|
locEnd
|
|
23
19
|
} = options;
|
|
24
|
-
|
|
25
20
|
if (resultArray) {
|
|
26
21
|
if (printer.canAttachComment && printer.canAttachComment(node)) {
|
|
27
22
|
let i;
|
|
28
|
-
|
|
29
23
|
for (i = resultArray.length - 1; i >= 0; --i) {
|
|
30
24
|
if (locStart(resultArray[i]) <= locStart(node) && locEnd(resultArray[i]) <= locEnd(node)) {
|
|
31
25
|
break;
|
|
32
26
|
}
|
|
33
27
|
}
|
|
34
|
-
|
|
35
28
|
resultArray.splice(i + 1, 0, node);
|
|
36
29
|
return;
|
|
37
30
|
}
|
|
38
31
|
} else if (childNodesCache.has(node)) {
|
|
39
32
|
return childNodesCache.get(node);
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments' && key !== 'parent').map(([, value]) => value);
|
|
43
|
-
|
|
44
35
|
if (!childNodes) {
|
|
45
36
|
return;
|
|
46
37
|
}
|
|
47
|
-
|
|
48
38
|
if (!resultArray) {
|
|
49
39
|
resultArray = [];
|
|
50
40
|
childNodesCache.set(node, resultArray);
|
|
51
41
|
}
|
|
52
|
-
|
|
53
42
|
for (const childNode of childNodes) {
|
|
54
43
|
getSortedChildNodes(childNode, options, resultArray);
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
return resultArray;
|
|
58
46
|
}
|
|
59
|
-
|
|
60
47
|
function decorateComment(node, comment, options, enclosingNode) {
|
|
61
48
|
const {
|
|
62
49
|
locStart,
|
|
@@ -69,61 +56,49 @@ function decorateComment(node, comment, options, enclosingNode) {
|
|
|
69
56
|
let followingNode;
|
|
70
57
|
let left = 0;
|
|
71
58
|
let right = childNodes.length;
|
|
72
|
-
|
|
73
59
|
while (left < right) {
|
|
74
60
|
const middle = left + right >> 1;
|
|
75
61
|
const child = childNodes[middle];
|
|
76
62
|
const start = locStart(child);
|
|
77
63
|
const end = locEnd(child);
|
|
78
|
-
|
|
79
64
|
if (start <= commentStart && commentEnd <= end) {
|
|
80
65
|
return decorateComment(child, comment, options, child);
|
|
81
66
|
}
|
|
82
|
-
|
|
83
67
|
if (end <= commentStart) {
|
|
84
68
|
precedingNode = child;
|
|
85
69
|
left = middle + 1;
|
|
86
70
|
continue;
|
|
87
71
|
}
|
|
88
|
-
|
|
89
72
|
if (commentEnd <= start) {
|
|
90
73
|
followingNode = child;
|
|
91
74
|
right = middle;
|
|
92
75
|
continue;
|
|
93
76
|
}
|
|
94
|
-
|
|
95
77
|
throw new Error('Comment location overlaps with node location');
|
|
96
78
|
}
|
|
97
|
-
|
|
98
79
|
if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
|
|
99
80
|
const {
|
|
100
81
|
quasis
|
|
101
82
|
} = enclosingNode;
|
|
102
83
|
const commentIndex = findExpressionIndexForComment(quasis, comment, options);
|
|
103
|
-
|
|
104
84
|
if (precedingNode && findExpressionIndexForComment(quasis, precedingNode, options) !== commentIndex) {
|
|
105
85
|
precedingNode = null;
|
|
106
86
|
}
|
|
107
|
-
|
|
108
87
|
if (followingNode && findExpressionIndexForComment(quasis, followingNode, options) !== commentIndex) {
|
|
109
88
|
followingNode = null;
|
|
110
89
|
}
|
|
111
90
|
}
|
|
112
|
-
|
|
113
91
|
return {
|
|
114
92
|
enclosingNode,
|
|
115
93
|
precedingNode,
|
|
116
94
|
followingNode
|
|
117
95
|
};
|
|
118
96
|
}
|
|
119
|
-
|
|
120
97
|
const returnFalse = () => false;
|
|
121
|
-
|
|
122
98
|
function attach(comments, ast, text, options) {
|
|
123
99
|
if (!Array.isArray(comments)) {
|
|
124
100
|
return;
|
|
125
101
|
}
|
|
126
|
-
|
|
127
102
|
const tiesToBreak = [];
|
|
128
103
|
const {
|
|
129
104
|
locStart,
|
|
@@ -138,14 +113,14 @@ function attach(comments, ast, text, options) {
|
|
|
138
113
|
endOfLine: handleEndOfLineComment = returnFalse,
|
|
139
114
|
remaining: handleRemainingComment = returnFalse
|
|
140
115
|
} = handleComments;
|
|
141
|
-
const decoratedComments = comments.map((comment, index) => ({
|
|
116
|
+
const decoratedComments = comments.map((comment, index) => ({
|
|
117
|
+
...decorateComment(ast, comment, options),
|
|
142
118
|
comment,
|
|
143
119
|
text,
|
|
144
120
|
options,
|
|
145
121
|
ast,
|
|
146
122
|
isLastComment: comments.length - 1 === index
|
|
147
123
|
}));
|
|
148
|
-
|
|
149
124
|
for (const [index, context] of decoratedComments.entries()) {
|
|
150
125
|
const {
|
|
151
126
|
comment,
|
|
@@ -157,21 +132,17 @@ function attach(comments, ast, text, options) {
|
|
|
157
132
|
ast,
|
|
158
133
|
isLastComment
|
|
159
134
|
} = context;
|
|
160
|
-
|
|
161
135
|
if (options.parser === 'json' || options.parser === 'json5' || options.parser === '__js_expression' || options.parser === '__vue_expression') {
|
|
162
136
|
if (locStart(comment) - locStart(ast) <= 0) {
|
|
163
137
|
addLeadingComment(ast, comment);
|
|
164
138
|
continue;
|
|
165
139
|
}
|
|
166
|
-
|
|
167
140
|
if (locEnd(comment) - locEnd(ast) >= 0) {
|
|
168
141
|
addTrailingComment(ast, comment);
|
|
169
142
|
continue;
|
|
170
143
|
}
|
|
171
144
|
}
|
|
172
|
-
|
|
173
145
|
let args;
|
|
174
|
-
|
|
175
146
|
if (avoidAstMutation) {
|
|
176
147
|
args = [context];
|
|
177
148
|
} else {
|
|
@@ -180,10 +151,8 @@ function attach(comments, ast, text, options) {
|
|
|
180
151
|
comment.followingNode = followingNode;
|
|
181
152
|
args = [comment, text, options, ast, isLastComment];
|
|
182
153
|
}
|
|
183
|
-
|
|
184
154
|
if (isOwnLineComment(text, options, decoratedComments, index)) {
|
|
185
155
|
comment.placement = 'ownLine';
|
|
186
|
-
|
|
187
156
|
if (handleOwnLineComment(...args)) {} else if (followingNode) {
|
|
188
157
|
addLeadingComment(followingNode, comment);
|
|
189
158
|
} else if (precedingNode) {
|
|
@@ -195,7 +164,6 @@ function attach(comments, ast, text, options) {
|
|
|
195
164
|
}
|
|
196
165
|
} else if (isEndOfLineComment(text, options, decoratedComments, index)) {
|
|
197
166
|
comment.placement = 'endOfLine';
|
|
198
|
-
|
|
199
167
|
if (handleEndOfLineComment(...args)) {} else if (precedingNode) {
|
|
200
168
|
addTrailingComment(precedingNode, comment);
|
|
201
169
|
} else if (followingNode) {
|
|
@@ -207,18 +175,14 @@ function attach(comments, ast, text, options) {
|
|
|
207
175
|
}
|
|
208
176
|
} else {
|
|
209
177
|
comment.placement = 'remaining';
|
|
210
|
-
|
|
211
178
|
if (handleRemainingComment(...args)) {} else if (precedingNode && followingNode) {
|
|
212
179
|
const tieCount = tiesToBreak.length;
|
|
213
|
-
|
|
214
180
|
if (tieCount > 0) {
|
|
215
181
|
const lastTie = tiesToBreak[tieCount - 1];
|
|
216
|
-
|
|
217
182
|
if (lastTie.followingNode !== followingNode) {
|
|
218
183
|
breakTies(tiesToBreak, text, options);
|
|
219
184
|
}
|
|
220
185
|
}
|
|
221
|
-
|
|
222
186
|
tiesToBreak.push(context);
|
|
223
187
|
} else if (precedingNode) {
|
|
224
188
|
addTrailingComment(precedingNode, comment);
|
|
@@ -231,9 +195,7 @@ function attach(comments, ast, text, options) {
|
|
|
231
195
|
}
|
|
232
196
|
}
|
|
233
197
|
}
|
|
234
|
-
|
|
235
198
|
breakTies(tiesToBreak, text, options);
|
|
236
|
-
|
|
237
199
|
if (!avoidAstMutation) {
|
|
238
200
|
for (const comment of comments) {
|
|
239
201
|
delete comment.precedingNode;
|
|
@@ -242,9 +204,7 @@ function attach(comments, ast, text, options) {
|
|
|
242
204
|
}
|
|
243
205
|
}
|
|
244
206
|
}
|
|
245
|
-
|
|
246
207
|
const isAllEmptyAndNoLineBreak = text => !/[\S\n\u2028\u2029]/.test(text);
|
|
247
|
-
|
|
248
208
|
function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
249
209
|
const {
|
|
250
210
|
comment,
|
|
@@ -255,27 +215,22 @@ function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
255
215
|
locEnd
|
|
256
216
|
} = options;
|
|
257
217
|
let start = locStart(comment);
|
|
258
|
-
|
|
259
218
|
if (precedingNode) {
|
|
260
219
|
for (let index = commentIndex - 1; index >= 0; index--) {
|
|
261
220
|
const {
|
|
262
221
|
comment,
|
|
263
222
|
precedingNode: currentCommentPrecedingNode
|
|
264
223
|
} = decoratedComments[index];
|
|
265
|
-
|
|
266
224
|
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(comment), start))) {
|
|
267
225
|
break;
|
|
268
226
|
}
|
|
269
|
-
|
|
270
227
|
start = locStart(comment);
|
|
271
228
|
}
|
|
272
229
|
}
|
|
273
|
-
|
|
274
230
|
return hasNewline(text, start, {
|
|
275
231
|
backwards: true
|
|
276
232
|
});
|
|
277
233
|
}
|
|
278
|
-
|
|
279
234
|
function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
280
235
|
const {
|
|
281
236
|
comment,
|
|
@@ -286,32 +241,25 @@ function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
286
241
|
locEnd
|
|
287
242
|
} = options;
|
|
288
243
|
let end = locEnd(comment);
|
|
289
|
-
|
|
290
244
|
if (followingNode) {
|
|
291
245
|
for (let index = commentIndex + 1; index < decoratedComments.length; index++) {
|
|
292
246
|
const {
|
|
293
247
|
comment,
|
|
294
248
|
followingNode: currentCommentFollowingNode
|
|
295
249
|
} = decoratedComments[index];
|
|
296
|
-
|
|
297
250
|
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(comment)))) {
|
|
298
251
|
break;
|
|
299
252
|
}
|
|
300
|
-
|
|
301
253
|
end = locEnd(comment);
|
|
302
254
|
}
|
|
303
255
|
}
|
|
304
|
-
|
|
305
256
|
return hasNewline(text, end);
|
|
306
257
|
}
|
|
307
|
-
|
|
308
258
|
function breakTies(tiesToBreak, text, options) {
|
|
309
259
|
const tieCount = tiesToBreak.length;
|
|
310
|
-
|
|
311
260
|
if (tieCount === 0) {
|
|
312
261
|
return;
|
|
313
262
|
}
|
|
314
|
-
|
|
315
263
|
const {
|
|
316
264
|
precedingNode,
|
|
317
265
|
followingNode,
|
|
@@ -320,7 +268,6 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
320
268
|
const gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
|
|
321
269
|
let gapEndPos = options.locStart(followingNode);
|
|
322
270
|
let indexOfFirstLeadingComment;
|
|
323
|
-
|
|
324
271
|
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
|
|
325
272
|
const {
|
|
326
273
|
comment,
|
|
@@ -330,14 +277,12 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
330
277
|
assert.strictEqual(currentCommentPrecedingNode, precedingNode);
|
|
331
278
|
assert.strictEqual(currentCommentFollowingNode, followingNode);
|
|
332
279
|
const gap = text.slice(options.locEnd(comment), gapEndPos);
|
|
333
|
-
|
|
334
280
|
if (gapRegExp.test(gap)) {
|
|
335
281
|
gapEndPos = options.locStart(comment);
|
|
336
282
|
} else {
|
|
337
283
|
break;
|
|
338
284
|
}
|
|
339
285
|
}
|
|
340
|
-
|
|
341
286
|
for (const [i, {
|
|
342
287
|
comment
|
|
343
288
|
}] of tiesToBreak.entries()) {
|
|
@@ -347,28 +292,22 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
347
292
|
addLeadingComment(followingNode, comment);
|
|
348
293
|
}
|
|
349
294
|
}
|
|
350
|
-
|
|
351
295
|
for (const node of [precedingNode, followingNode]) {
|
|
352
296
|
if (node.comments && node.comments.length > 1) {
|
|
353
297
|
node.comments.sort((a, b) => options.locStart(a) - options.locStart(b));
|
|
354
298
|
}
|
|
355
299
|
}
|
|
356
|
-
|
|
357
300
|
tiesToBreak.length = 0;
|
|
358
301
|
}
|
|
359
|
-
|
|
360
302
|
function findExpressionIndexForComment(quasis, comment, options) {
|
|
361
303
|
const startPos = options.locStart(comment) - 1;
|
|
362
|
-
|
|
363
304
|
for (let i = 1; i < quasis.length; ++i) {
|
|
364
305
|
if (startPos < options.locStart(quasis[i])) {
|
|
365
306
|
return i - 1;
|
|
366
307
|
}
|
|
367
308
|
}
|
|
368
|
-
|
|
369
309
|
return 0;
|
|
370
310
|
}
|
|
371
|
-
|
|
372
311
|
module.exports = {
|
|
373
312
|
attach
|
|
374
313
|
};
|
|
@@ -4,40 +4,26 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.print = print;
|
|
7
|
-
|
|
8
7
|
var _mutateESTreeASTForPrettier = _interopRequireDefault(require("../../utils/mutateESTreeASTForPrettier"));
|
|
9
|
-
|
|
10
8
|
var prettier = _interopRequireWildcard(require("prettier"));
|
|
11
|
-
|
|
12
9
|
var _comments = require("./comments/comments");
|
|
13
|
-
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
-
|
|
18
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
-
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
20
12
|
async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
21
13
|
const program = ast;
|
|
22
|
-
|
|
23
14
|
if (program.body.length === 0) {
|
|
24
15
|
var _program$docblock;
|
|
25
|
-
|
|
26
16
|
const docblockComment = (_program$docblock = program.docblock) == null ? void 0 : _program$docblock.comment;
|
|
27
|
-
|
|
28
17
|
if (docblockComment != null) {
|
|
29
18
|
return '/*' + docblockComment.value + '*/\n';
|
|
30
19
|
}
|
|
31
|
-
|
|
32
20
|
return '';
|
|
33
21
|
}
|
|
34
|
-
|
|
35
22
|
const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode);
|
|
36
23
|
(0, _mutateESTreeASTForPrettier.default)(program, visitorKeys);
|
|
37
24
|
let pluginParserName = 'flow';
|
|
38
25
|
let pluginParser;
|
|
39
26
|
let pluginPrinter;
|
|
40
|
-
|
|
41
27
|
try {
|
|
42
28
|
const prettierHermesPlugin = await Promise.resolve().then(() => _interopRequireWildcard(require('prettier-plugin-hermes-parser')));
|
|
43
29
|
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
@@ -45,21 +31,19 @@ async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
|
45
31
|
pluginParserName = 'hermes';
|
|
46
32
|
} catch {
|
|
47
33
|
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
48
|
-
|
|
49
34
|
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
50
35
|
}
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
return prettier.format(codeForPrinting, {
|
|
37
|
+
...prettierOptions,
|
|
53
38
|
parser: pluginParserName,
|
|
54
39
|
requirePragma: false,
|
|
55
40
|
plugins: [{
|
|
56
41
|
parsers: {
|
|
57
|
-
[pluginParserName]: {
|
|
58
|
-
|
|
42
|
+
[pluginParserName]: {
|
|
43
|
+
...pluginParser,
|
|
59
44
|
parse() {
|
|
60
45
|
return program;
|
|
61
46
|
}
|
|
62
|
-
|
|
63
47
|
}
|
|
64
48
|
},
|
|
65
49
|
printers: pluginPrinter
|
|
@@ -4,14 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.SimpleTraverserSkip = exports.SimpleTraverserBreak = exports.SimpleTraverser = void 0;
|
|
7
|
-
|
|
8
7
|
var _getVisitorKeys = require("./getVisitorKeys");
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
exports.SimpleTraverserSkip = SimpleTraverserSkip;
|
|
12
|
-
const SimpleTraverserBreak = new Error();
|
|
13
|
-
exports.SimpleTraverserBreak = SimpleTraverserBreak;
|
|
14
|
-
|
|
8
|
+
const SimpleTraverserSkip = exports.SimpleTraverserSkip = new Error();
|
|
9
|
+
const SimpleTraverserBreak = exports.SimpleTraverserBreak = new Error();
|
|
15
10
|
class SimpleTraverser {
|
|
16
11
|
traverse(node, options) {
|
|
17
12
|
try {
|
|
@@ -20,35 +15,27 @@ class SimpleTraverser {
|
|
|
20
15
|
if (ex === SimpleTraverserBreak) {
|
|
21
16
|
return;
|
|
22
17
|
}
|
|
23
|
-
|
|
24
18
|
throw ex;
|
|
25
19
|
}
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
_traverse(node, parent, options) {
|
|
29
22
|
if (!(0, _getVisitorKeys.isNode)(node)) {
|
|
30
23
|
return;
|
|
31
24
|
}
|
|
32
|
-
|
|
33
25
|
try {
|
|
34
26
|
options.enter(node, parent);
|
|
35
27
|
} catch (ex) {
|
|
36
28
|
if (ex === SimpleTraverserSkip) {
|
|
37
29
|
return;
|
|
38
30
|
}
|
|
39
|
-
|
|
40
31
|
this._setErrorContext(ex, node);
|
|
41
|
-
|
|
42
32
|
throw ex;
|
|
43
33
|
}
|
|
44
|
-
|
|
45
34
|
const keys = (0, _getVisitorKeys.getVisitorKeys)(node, options.visitorKeys);
|
|
46
|
-
|
|
47
35
|
for (const key of keys) {
|
|
48
36
|
const lookupKey = key;
|
|
49
37
|
const childAny = node[lookupKey];
|
|
50
38
|
const child = childAny;
|
|
51
|
-
|
|
52
39
|
if (Array.isArray(child)) {
|
|
53
40
|
for (let j = 0; j < child.length; ++j) {
|
|
54
41
|
this._traverse(child[j], node, options);
|
|
@@ -57,20 +44,16 @@ class SimpleTraverser {
|
|
|
57
44
|
this._traverse(child, node, options);
|
|
58
45
|
}
|
|
59
46
|
}
|
|
60
|
-
|
|
61
47
|
try {
|
|
62
48
|
options.leave(node, parent);
|
|
63
49
|
} catch (ex) {
|
|
64
50
|
if (ex === SimpleTraverserSkip) {
|
|
65
51
|
return;
|
|
66
52
|
}
|
|
67
|
-
|
|
68
53
|
this._setErrorContext(ex, node);
|
|
69
|
-
|
|
70
54
|
throw ex;
|
|
71
55
|
}
|
|
72
56
|
}
|
|
73
|
-
|
|
74
57
|
_setErrorContext(ex, node) {
|
|
75
58
|
ex.currentNode = {
|
|
76
59
|
type: node.type,
|
|
@@ -78,13 +61,10 @@ class SimpleTraverser {
|
|
|
78
61
|
loc: node.loc
|
|
79
62
|
};
|
|
80
63
|
}
|
|
81
|
-
|
|
82
64
|
static traverse(node, options) {
|
|
83
65
|
new SimpleTraverser().traverse(node, options);
|
|
84
66
|
}
|
|
85
|
-
|
|
86
67
|
}
|
|
87
|
-
|
|
88
68
|
exports.SimpleTraverser = SimpleTraverser;
|
|
89
69
|
SimpleTraverser.Break = SimpleTraverserBreak;
|
|
90
70
|
SimpleTraverser.Skip = SimpleTraverserSkip;
|
|
@@ -5,25 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.getVisitorKeys = getVisitorKeys;
|
|
7
7
|
exports.isNode = isNode;
|
|
8
|
-
|
|
9
8
|
var _ESTreeVisitorKeys = _interopRequireDefault(require("../generated/ESTreeVisitorKeys"));
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
default: obj
|
|
9
|
+
function _interopRequireDefault(e) {
|
|
10
|
+
return e && e.__esModule ? e : {
|
|
11
|
+
default: e
|
|
14
12
|
};
|
|
15
13
|
}
|
|
16
|
-
|
|
17
14
|
function isNode(thing) {
|
|
18
15
|
return typeof thing === 'object' && thing != null && typeof thing.type === 'string';
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
function getVisitorKeys(node, visitorKeys) {
|
|
22
18
|
const keys = (visitorKeys != null ? visitorKeys : _ESTreeVisitorKeys.default)[node.type];
|
|
23
|
-
|
|
24
19
|
if (keys == null) {
|
|
25
20
|
throw new Error(`No visitor keys found for node type "${node.type}".`);
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
return keys;
|
|
29
23
|
}
|
package/dist/utils/Builders.js
CHANGED
|
@@ -17,16 +17,13 @@ exports.stringLiteral = stringLiteral;
|
|
|
17
17
|
exports.throwStatement = throwStatement;
|
|
18
18
|
exports.typeofExpression = typeofExpression;
|
|
19
19
|
exports.variableDeclaration = variableDeclaration;
|
|
20
|
-
const EMPTY_PARENT = null;
|
|
21
|
-
exports.EMPTY_PARENT = EMPTY_PARENT;
|
|
22
|
-
|
|
20
|
+
const EMPTY_PARENT = exports.EMPTY_PARENT = null;
|
|
23
21
|
function createDefaultPosition() {
|
|
24
22
|
return {
|
|
25
23
|
line: 1,
|
|
26
24
|
column: 0
|
|
27
25
|
};
|
|
28
26
|
}
|
|
29
|
-
|
|
30
27
|
function etc({
|
|
31
28
|
loc,
|
|
32
29
|
range,
|
|
@@ -41,7 +38,6 @@ function etc({
|
|
|
41
38
|
parent: parent != null ? parent : EMPTY_PARENT
|
|
42
39
|
};
|
|
43
40
|
}
|
|
44
|
-
|
|
45
41
|
function ident(name, info) {
|
|
46
42
|
return {
|
|
47
43
|
type: 'Identifier',
|
|
@@ -51,7 +47,6 @@ function ident(name, info) {
|
|
|
51
47
|
...etc(info)
|
|
52
48
|
};
|
|
53
49
|
}
|
|
54
|
-
|
|
55
50
|
function stringLiteral(value, info) {
|
|
56
51
|
return {
|
|
57
52
|
type: 'Literal',
|
|
@@ -61,7 +56,6 @@ function stringLiteral(value, info) {
|
|
|
61
56
|
...etc(info)
|
|
62
57
|
};
|
|
63
58
|
}
|
|
64
|
-
|
|
65
59
|
function numberLiteral(value, info) {
|
|
66
60
|
return {
|
|
67
61
|
type: 'Literal',
|
|
@@ -71,7 +65,6 @@ function numberLiteral(value, info) {
|
|
|
71
65
|
...etc(info)
|
|
72
66
|
};
|
|
73
67
|
}
|
|
74
|
-
|
|
75
68
|
function nullLiteral(info) {
|
|
76
69
|
return {
|
|
77
70
|
type: 'Literal',
|
|
@@ -81,12 +74,10 @@ function nullLiteral(info) {
|
|
|
81
74
|
...etc(info)
|
|
82
75
|
};
|
|
83
76
|
}
|
|
84
|
-
|
|
85
77
|
function conjunction(tests) {
|
|
86
78
|
if (tests.length === 0) {
|
|
87
79
|
throw new Error('Must have at least one test.');
|
|
88
80
|
}
|
|
89
|
-
|
|
90
81
|
return tests.reduce((acc, test) => ({
|
|
91
82
|
type: 'LogicalExpression',
|
|
92
83
|
left: acc,
|
|
@@ -95,12 +86,10 @@ function conjunction(tests) {
|
|
|
95
86
|
...etc()
|
|
96
87
|
}));
|
|
97
88
|
}
|
|
98
|
-
|
|
99
89
|
function disjunction(tests) {
|
|
100
90
|
if (tests.length === 0) {
|
|
101
91
|
throw new Error('Must have at least one test.');
|
|
102
92
|
}
|
|
103
|
-
|
|
104
93
|
return tests.reduce((acc, test) => ({
|
|
105
94
|
type: 'LogicalExpression',
|
|
106
95
|
left: acc,
|
|
@@ -109,7 +98,6 @@ function disjunction(tests) {
|
|
|
109
98
|
...etc()
|
|
110
99
|
}));
|
|
111
100
|
}
|
|
112
|
-
|
|
113
101
|
function variableDeclaration(kind, id, init, info) {
|
|
114
102
|
return {
|
|
115
103
|
type: 'VariableDeclaration',
|
|
@@ -124,7 +112,6 @@ function variableDeclaration(kind, id, init, info) {
|
|
|
124
112
|
...etc(info)
|
|
125
113
|
};
|
|
126
114
|
}
|
|
127
|
-
|
|
128
115
|
function callExpression(callee, args, info) {
|
|
129
116
|
return {
|
|
130
117
|
type: 'CallExpression',
|
|
@@ -135,7 +122,6 @@ function callExpression(callee, args, info) {
|
|
|
135
122
|
...etc(info)
|
|
136
123
|
};
|
|
137
124
|
}
|
|
138
|
-
|
|
139
125
|
function throwStatement(arg, info) {
|
|
140
126
|
return {
|
|
141
127
|
type: 'ThrowStatement',
|
|
@@ -143,7 +129,6 @@ function throwStatement(arg, info) {
|
|
|
143
129
|
...etc(info)
|
|
144
130
|
};
|
|
145
131
|
}
|
|
146
|
-
|
|
147
132
|
function iife(statements, params = [], args = []) {
|
|
148
133
|
const callee = {
|
|
149
134
|
type: 'ArrowFunctionExpression',
|
|
@@ -163,7 +148,6 @@ function iife(statements, params = [], args = []) {
|
|
|
163
148
|
};
|
|
164
149
|
return callExpression(callee, args);
|
|
165
150
|
}
|
|
166
|
-
|
|
167
151
|
function typeofExpression(arg, kind) {
|
|
168
152
|
return {
|
|
169
153
|
type: 'BinaryExpression',
|
package/dist/utils/GenID.js
CHANGED
|
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
const genPrefix = '$$gen$';
|
|
8
|
-
|
|
9
8
|
class GenID {
|
|
10
9
|
constructor(uniqueTransformPrefix) {
|
|
11
10
|
this.genN = 0;
|
|
@@ -13,25 +12,19 @@ class GenID {
|
|
|
13
12
|
this.prefix = void 0;
|
|
14
13
|
this.prefix = `${genPrefix}${uniqueTransformPrefix}`;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
id() {
|
|
18
16
|
let name;
|
|
19
|
-
|
|
20
17
|
do {
|
|
21
18
|
name = `${this.prefix}${this.genN}`;
|
|
22
19
|
this.genN++;
|
|
23
20
|
} while (this.used.has(name));
|
|
24
|
-
|
|
25
21
|
this.used.add(name);
|
|
26
22
|
return name;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
addUsage(name) {
|
|
30
25
|
if (name.startsWith(this.prefix)) {
|
|
31
26
|
this.used.add(name);
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
exports.default = GenID;
|
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = isReservedWord;
|
|
7
|
-
|
|
8
7
|
function isReservedWord(name) {
|
|
9
8
|
switch (name) {
|
|
10
9
|
case 'await':
|
|
@@ -46,7 +45,6 @@ function isReservedWord(name) {
|
|
|
46
45
|
case 'with':
|
|
47
46
|
case 'yield':
|
|
48
47
|
return true;
|
|
49
|
-
|
|
50
48
|
default:
|
|
51
49
|
return false;
|
|
52
50
|
}
|