hermes-transform 0.36.1 → 0.37.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/detachedNode.js +3 -57
- package/dist/detachedNode.js.flow +12 -12
- package/dist/generated/TransformCloneSignatures.js.flow +1 -1
- package/dist/generated/TransformModifySignatures.js.flow +1 -1
- package/dist/generated/TransformReplaceSignatures.js.flow +207 -207
- package/dist/generated/node-types.js +0 -21
- package/dist/generated/node-types.js.flow +1086 -978
- package/dist/generated/special-case-node-types/Comment.js +0 -13
- package/dist/generated/special-case-node-types/Comment.js.flow +2 -2
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +10 -10
- package/dist/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/generated/special-case-node-types/DeclareHook.js.flow +4 -4
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +9 -9
- package/dist/generated/special-case-node-types/Literal.js +1 -12
- package/dist/generated/special-case-node-types/Literal.js.flow +20 -20
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +23 -21
- package/dist/generated/special-case-node-types/Property.js +0 -9
- package/dist/generated/special-case-node-types/Property.js.flow +50 -42
- package/dist/generated/special-case-node-types/misc.js +6 -35
- package/dist/generated/special-case-node-types/misc.js.flow +56 -48
- package/dist/index.js +0 -9
- package/dist/src/detachedNode.js +3 -57
- package/dist/src/generated/node-types.js +0 -21
- package/dist/src/generated/special-case-node-types/Comment.js +0 -13
- package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/src/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/src/generated/special-case-node-types/Literal.js +1 -12
- package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/src/generated/special-case-node-types/Property.js +0 -9
- package/dist/src/generated/special-case-node-types/misc.js +6 -35
- package/dist/src/index.js +0 -9
- package/dist/src/transform/Errors.js +0 -9
- package/dist/src/transform/MutationContext.js +1 -28
- package/dist/src/transform/TransformContext.js +0 -18
- package/dist/src/transform/comments/comments.js +12 -78
- package/dist/src/transform/comments/prettier/common/util.js +4 -133
- package/dist/src/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/src/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/src/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/src/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/src/transform/comments/prettier/main/comments.js +11 -77
- package/dist/src/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/src/transform/mutations/AddComments.js +0 -9
- package/dist/src/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/src/transform/mutations/InsertStatement.js +4 -16
- package/dist/src/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/src/transform/mutations/RemoveComment.js +0 -10
- package/dist/src/transform/mutations/RemoveNode.js +1 -23
- package/dist/src/transform/mutations/RemoveStatement.js +0 -16
- package/dist/src/transform/mutations/ReplaceNode.js +2 -16
- package/dist/src/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/src/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/src/transform/parse.js +6 -25
- package/dist/src/transform/print.js +9 -35
- package/dist/src/transform/transform.js +0 -9
- package/dist/src/transform/transformAST.js +3 -31
- package/dist/src/traverse/NodeEventGenerator.js +2 -108
- package/dist/src/traverse/SafeEmitter.js +0 -19
- package/dist/src/traverse/esquery.js +0 -18
- package/dist/src/traverse/traverse.js +1 -22
- package/dist/transform/Errors.js +0 -9
- package/dist/transform/MutationContext.js +1 -28
- package/dist/transform/MutationContext.js.flow +3 -3
- package/dist/transform/TransformContext.js +0 -18
- package/dist/transform/TransformContext.js.flow +60 -54
- package/dist/transform/comments/comments.js +12 -78
- package/dist/transform/comments/comments.js.flow +8 -8
- package/dist/transform/comments/prettier/common/util.js +4 -133
- package/dist/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/transform/comments/prettier/main/comments.js +11 -77
- package/dist/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/transform/mutations/AddComments.js +0 -9
- package/dist/transform/mutations/AddComments.js.flow +2 -2
- package/dist/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +4 -16
- package/dist/transform/mutations/InsertStatement.js.flow +7 -6
- package/dist/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/transform/mutations/ModifyNodeInPlace.js.flow +2 -2
- package/dist/transform/mutations/RemoveComment.js +0 -10
- package/dist/transform/mutations/RemoveComment.js.flow +4 -4
- package/dist/transform/mutations/RemoveNode.js +1 -23
- package/dist/transform/mutations/RemoveNode.js.flow +9 -9
- package/dist/transform/mutations/RemoveStatement.js +0 -16
- package/dist/transform/mutations/RemoveStatement.js.flow +3 -3
- package/dist/transform/mutations/ReplaceNode.js +2 -16
- package/dist/transform/mutations/ReplaceNode.js.flow +6 -6
- package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +7 -8
- package/dist/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -6
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +4 -3
- package/dist/transform/parse.js +6 -25
- package/dist/transform/print.js +9 -35
- package/dist/transform/print.js.flow +7 -15
- package/dist/transform/transform.js +0 -9
- package/dist/transform/transformAST.js +3 -31
- package/dist/traverse/NodeEventGenerator.js +2 -108
- package/dist/traverse/NodeEventGenerator.js.flow +13 -9
- package/dist/traverse/SafeEmitter.js +0 -19
- package/dist/traverse/SafeEmitter.js.flow +2 -2
- package/dist/traverse/esquery.js +0 -18
- package/dist/traverse/esquery.js.flow +10 -10
- package/dist/traverse/traverse.js +1 -22
- package/dist/traverse/traverse.js.flow +4 -4
- package/package.json +7 -12
|
@@ -1,45 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
|
-
/**
|
|
11
|
-
* @param {Comment} comment
|
|
12
|
-
* @returns {boolean}
|
|
13
|
-
*/
|
|
14
2
|
|
|
15
3
|
function isBlockComment(comment) {
|
|
16
|
-
return comment.type === 'Block' || comment.type === 'CommentBlock' ||
|
|
17
|
-
comment.type === 'MultiLine';
|
|
4
|
+
return comment.type === 'Block' || comment.type === 'CommentBlock' || comment.type === 'MultiLine';
|
|
18
5
|
}
|
|
19
|
-
/**
|
|
20
|
-
* @param {Comment} comment
|
|
21
|
-
* @returns {boolean}
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
6
|
|
|
25
7
|
function isLineComment(comment) {
|
|
26
|
-
return comment.type === 'Line' || comment.type === 'CommentLine' ||
|
|
27
|
-
comment.type === 'SingleLine' || comment.type === 'HashbangComment' || comment.type === 'HTMLOpen' || comment.type === 'HTMLClose';
|
|
8
|
+
return comment.type === 'Line' || comment.type === 'CommentLine' || comment.type === 'SingleLine' || comment.type === 'HashbangComment' || comment.type === 'HTMLOpen' || comment.type === 'HTMLClose';
|
|
28
9
|
}
|
|
29
|
-
/**
|
|
30
|
-
* @param {Node} node
|
|
31
|
-
* @returns {boolean}
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
10
|
|
|
35
11
|
function isCallExpression(node) {
|
|
36
12
|
return node && (node.type === 'CallExpression' || node.type === 'OptionalCallExpression');
|
|
37
13
|
}
|
|
38
|
-
/**
|
|
39
|
-
* @param {Node} node
|
|
40
|
-
* @returns {boolean}
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
14
|
|
|
44
15
|
function isMemberExpression(node) {
|
|
45
16
|
return node && (node.type === 'MemberExpression' || node.type === 'OptionalMemberExpression');
|
|
@@ -56,8 +27,7 @@ function getFunctionParameters(node) {
|
|
|
56
27
|
|
|
57
28
|
if (node.this) {
|
|
58
29
|
parameters.push(node.this);
|
|
59
|
-
}
|
|
60
|
-
|
|
30
|
+
}
|
|
61
31
|
|
|
62
32
|
if (Array.isArray(node.parameters)) {
|
|
63
33
|
parameters.push(...node.parameters);
|
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const assert = require('assert');
|
|
@@ -32,9 +24,6 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
32
24
|
|
|
33
25
|
if (resultArray) {
|
|
34
26
|
if (printer.canAttachComment && printer.canAttachComment(node)) {
|
|
35
|
-
// This reverse insertion sort almost always takes constant
|
|
36
|
-
// time because we almost always (maybe always?) append the
|
|
37
|
-
// nodes in order anyway.
|
|
38
27
|
let i;
|
|
39
28
|
|
|
40
29
|
for (i = resultArray.length - 1; i >= 0; --i) {
|
|
@@ -66,10 +55,7 @@ function getSortedChildNodes(node, options, resultArray) {
|
|
|
66
55
|
}
|
|
67
56
|
|
|
68
57
|
return resultArray;
|
|
69
|
-
}
|
|
70
|
-
// .precedingNode, .enclosingNode, and/or .followingNode properties, at
|
|
71
|
-
// least one of which is guaranteed to be defined.
|
|
72
|
-
|
|
58
|
+
}
|
|
73
59
|
|
|
74
60
|
function decorateComment(node, comment, options, enclosingNode) {
|
|
75
61
|
const {
|
|
@@ -80,8 +66,7 @@ function decorateComment(node, comment, options, enclosingNode) {
|
|
|
80
66
|
const commentEnd = locEnd(comment);
|
|
81
67
|
const childNodes = getSortedChildNodes(node, options);
|
|
82
68
|
let precedingNode;
|
|
83
|
-
let followingNode;
|
|
84
|
-
|
|
69
|
+
let followingNode;
|
|
85
70
|
let left = 0;
|
|
86
71
|
let right = childNodes.length;
|
|
87
72
|
|
|
@@ -89,39 +74,26 @@ function decorateComment(node, comment, options, enclosingNode) {
|
|
|
89
74
|
const middle = left + right >> 1;
|
|
90
75
|
const child = childNodes[middle];
|
|
91
76
|
const start = locStart(child);
|
|
92
|
-
const end = locEnd(child);
|
|
77
|
+
const end = locEnd(child);
|
|
93
78
|
|
|
94
79
|
if (start <= commentStart && commentEnd <= end) {
|
|
95
|
-
// Abandon the binary search at this level.
|
|
96
80
|
return decorateComment(child, comment, options, child);
|
|
97
81
|
}
|
|
98
82
|
|
|
99
83
|
if (end <= commentStart) {
|
|
100
|
-
// This child node falls completely before the comment.
|
|
101
|
-
// Because we will never consider this node or any nodes
|
|
102
|
-
// before it again, this node must be the closest preceding
|
|
103
|
-
// node we have encountered so far.
|
|
104
84
|
precedingNode = child;
|
|
105
85
|
left = middle + 1;
|
|
106
86
|
continue;
|
|
107
87
|
}
|
|
108
88
|
|
|
109
89
|
if (commentEnd <= start) {
|
|
110
|
-
// This child node falls completely after the comment.
|
|
111
|
-
// Because we will never consider this node or any nodes after
|
|
112
|
-
// it again, this node must be the closest following node we
|
|
113
|
-
// have encountered so far.
|
|
114
90
|
followingNode = child;
|
|
115
91
|
right = middle;
|
|
116
92
|
continue;
|
|
117
93
|
}
|
|
118
|
-
/* istanbul ignore next */
|
|
119
|
-
|
|
120
94
|
|
|
121
95
|
throw new Error('Comment location overlaps with node location');
|
|
122
|
-
}
|
|
123
|
-
// template literal to move to another expression.
|
|
124
|
-
|
|
96
|
+
}
|
|
125
97
|
|
|
126
98
|
if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
|
|
127
99
|
const {
|
|
@@ -159,8 +131,7 @@ function attach(comments, ast, text, options) {
|
|
|
159
131
|
printer: {
|
|
160
132
|
handleComments = {}
|
|
161
133
|
}
|
|
162
|
-
} = options;
|
|
163
|
-
|
|
134
|
+
} = options;
|
|
164
135
|
const {
|
|
165
136
|
avoidAstMutation,
|
|
166
137
|
ownLine: handleOwnLineComment = returnFalse,
|
|
@@ -211,51 +182,33 @@ function attach(comments, ast, text, options) {
|
|
|
211
182
|
}
|
|
212
183
|
|
|
213
184
|
if (isOwnLineComment(text, options, decoratedComments, index)) {
|
|
214
|
-
comment.placement = 'ownLine';
|
|
215
|
-
// We also need to check if it's the first line of the file.
|
|
185
|
+
comment.placement = 'ownLine';
|
|
216
186
|
|
|
217
|
-
if (handleOwnLineComment(...args)) {
|
|
218
|
-
} else if (followingNode) {
|
|
219
|
-
// Always a leading comment.
|
|
187
|
+
if (handleOwnLineComment(...args)) {} else if (followingNode) {
|
|
220
188
|
addLeadingComment(followingNode, comment);
|
|
221
189
|
} else if (precedingNode) {
|
|
222
190
|
addTrailingComment(precedingNode, comment);
|
|
223
191
|
} else if (enclosingNode) {
|
|
224
192
|
addDanglingComment(enclosingNode, comment);
|
|
225
193
|
} else {
|
|
226
|
-
// There are no nodes, let's attach it to the root of the ast
|
|
227
|
-
|
|
228
|
-
/* istanbul ignore next */
|
|
229
194
|
addDanglingComment(ast, comment);
|
|
230
195
|
}
|
|
231
196
|
} else if (isEndOfLineComment(text, options, decoratedComments, index)) {
|
|
232
197
|
comment.placement = 'endOfLine';
|
|
233
198
|
|
|
234
|
-
if (handleEndOfLineComment(...args)) {
|
|
235
|
-
} else if (precedingNode) {
|
|
236
|
-
// There is content before this comment on the same line, but
|
|
237
|
-
// none after it, so prefer a trailing comment of the previous node.
|
|
199
|
+
if (handleEndOfLineComment(...args)) {} else if (precedingNode) {
|
|
238
200
|
addTrailingComment(precedingNode, comment);
|
|
239
201
|
} else if (followingNode) {
|
|
240
202
|
addLeadingComment(followingNode, comment);
|
|
241
203
|
} else if (enclosingNode) {
|
|
242
204
|
addDanglingComment(enclosingNode, comment);
|
|
243
205
|
} else {
|
|
244
|
-
// There are no nodes, let's attach it to the root of the ast
|
|
245
|
-
|
|
246
|
-
/* istanbul ignore next */
|
|
247
206
|
addDanglingComment(ast, comment);
|
|
248
207
|
}
|
|
249
208
|
} else {
|
|
250
209
|
comment.placement = 'remaining';
|
|
251
210
|
|
|
252
|
-
if (handleRemainingComment(...args)) {
|
|
253
|
-
} else if (precedingNode && followingNode) {
|
|
254
|
-
// Otherwise, text exists both before and after the comment on
|
|
255
|
-
// the same line. If there is both a preceding and following
|
|
256
|
-
// node, use a tie-breaking algorithm to determine if it should
|
|
257
|
-
// be attached to the next or previous node. In the last case,
|
|
258
|
-
// simply attach the right node;
|
|
211
|
+
if (handleRemainingComment(...args)) {} else if (precedingNode && followingNode) {
|
|
259
212
|
const tieCount = tiesToBreak.length;
|
|
260
213
|
|
|
261
214
|
if (tieCount > 0) {
|
|
@@ -274,9 +227,6 @@ function attach(comments, ast, text, options) {
|
|
|
274
227
|
} else if (enclosingNode) {
|
|
275
228
|
addDanglingComment(enclosingNode, comment);
|
|
276
229
|
} else {
|
|
277
|
-
// There are no nodes, let's attach it to the root of the ast
|
|
278
|
-
|
|
279
|
-
/* istanbul ignore next */
|
|
280
230
|
addDanglingComment(ast, comment);
|
|
281
231
|
}
|
|
282
232
|
}
|
|
@@ -286,9 +236,6 @@ function attach(comments, ast, text, options) {
|
|
|
286
236
|
|
|
287
237
|
if (!avoidAstMutation) {
|
|
288
238
|
for (const comment of comments) {
|
|
289
|
-
// These node references were useful for breaking ties, but we
|
|
290
|
-
// don't need them anymore, and they create cycles in the AST that
|
|
291
|
-
// may lead to infinite recursion if we don't delete them here.
|
|
292
239
|
delete comment.precedingNode;
|
|
293
240
|
delete comment.enclosingNode;
|
|
294
241
|
delete comment.followingNode;
|
|
@@ -310,7 +257,6 @@ function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
310
257
|
let start = locStart(comment);
|
|
311
258
|
|
|
312
259
|
if (precedingNode) {
|
|
313
|
-
// Find first comment on the same line
|
|
314
260
|
for (let index = commentIndex - 1; index >= 0; index--) {
|
|
315
261
|
const {
|
|
316
262
|
comment,
|
|
@@ -342,7 +288,6 @@ function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
|
342
288
|
let end = locEnd(comment);
|
|
343
289
|
|
|
344
290
|
if (followingNode) {
|
|
345
|
-
// Find last comment on the same line
|
|
346
291
|
for (let index = commentIndex + 1; index < decoratedComments.length; index++) {
|
|
347
292
|
const {
|
|
348
293
|
comment,
|
|
@@ -373,12 +318,7 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
373
318
|
enclosingNode
|
|
374
319
|
} = tiesToBreak[0];
|
|
375
320
|
const gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
|
|
376
|
-
let gapEndPos = options.locStart(followingNode);
|
|
377
|
-
// between the tied comments. In order to qualify as leading, a
|
|
378
|
-
// comment must be separated from followingNode by an unbroken series of
|
|
379
|
-
// gaps (or other comments). Gaps should only contain whitespace or open
|
|
380
|
-
// parentheses.
|
|
381
|
-
|
|
321
|
+
let gapEndPos = options.locStart(followingNode);
|
|
382
322
|
let indexOfFirstLeadingComment;
|
|
383
323
|
|
|
384
324
|
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
|
|
@@ -394,8 +334,6 @@ function breakTies(tiesToBreak, text, options) {
|
|
|
394
334
|
if (gapRegExp.test(gap)) {
|
|
395
335
|
gapEndPos = options.locStart(comment);
|
|
396
336
|
} else {
|
|
397
|
-
// The gap string contained something other than whitespace or open
|
|
398
|
-
// parentheses.
|
|
399
337
|
break;
|
|
400
338
|
}
|
|
401
339
|
}
|
|
@@ -426,11 +364,7 @@ function findExpressionIndexForComment(quasis, comment, options) {
|
|
|
426
364
|
if (startPos < options.locStart(quasis[i])) {
|
|
427
365
|
return i - 1;
|
|
428
366
|
}
|
|
429
|
-
}
|
|
430
|
-
// Let's just return the first one.
|
|
431
|
-
|
|
432
|
-
/* istanbul ignore next */
|
|
433
|
-
|
|
367
|
+
}
|
|
434
368
|
|
|
435
369
|
return 0;
|
|
436
370
|
}
|
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const getLast = arr => arr[arr.length - 1];
|
|
@@ -8,15 +8,6 @@ exports.performAddCommentsMutation = performAddCommentsMutation;
|
|
|
8
8
|
|
|
9
9
|
var _comments = require("../comments/comments");
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
-
*
|
|
14
|
-
* This source code is licensed under the MIT license found in the
|
|
15
|
-
* LICENSE file in the root directory of this source tree.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @format
|
|
19
|
-
*/
|
|
20
11
|
function createAddCommentsMutation(node, comments) {
|
|
21
12
|
if (comments.length === 0) {
|
|
22
13
|
return null;
|
|
@@ -15,9 +15,9 @@ import type {CommentPlacement} from '../comments/comments';
|
|
|
15
15
|
|
|
16
16
|
import {addComment, cloneComment} from '../comments/comments';
|
|
17
17
|
|
|
18
|
-
export type AddCommentsMutation =
|
|
18
|
+
export type AddCommentsMutation = Readonly<{
|
|
19
19
|
type: 'addComments',
|
|
20
|
-
comments:
|
|
20
|
+
comments: ReadonlyArray<{comment: Comment, placement: CommentPlacement}>,
|
|
21
21
|
node: ESNode | DetachedNode<ESNode>,
|
|
22
22
|
}>;
|
|
23
23
|
|
|
@@ -8,15 +8,6 @@ exports.performCloneCommentsToMutation = performCloneCommentsToMutation;
|
|
|
8
8
|
|
|
9
9
|
var _comments = require("../comments/comments");
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
-
*
|
|
14
|
-
* This source code is licensed under the MIT license found in the
|
|
15
|
-
* LICENSE file in the root directory of this source tree.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @format
|
|
19
|
-
*/
|
|
20
11
|
function createCloneCommentsToMutation(target, destination) {
|
|
21
12
|
return {
|
|
22
13
|
type: 'cloneCommentsTo',
|
|
@@ -14,7 +14,7 @@ import type {MutationContext} from '../MutationContext';
|
|
|
14
14
|
|
|
15
15
|
import {cloneCommentsToNewNode} from '../comments/comments';
|
|
16
16
|
|
|
17
|
-
export type CloneCommentsToMutation =
|
|
17
|
+
export type CloneCommentsToMutation = Readonly<{
|
|
18
18
|
type: 'cloneCommentsTo',
|
|
19
19
|
target: ESNode | DetachedNode<ESNode>,
|
|
20
20
|
destination: ESNode | DetachedNode<ESNode>,
|
|
@@ -20,15 +20,6 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
20
20
|
|
|
21
21
|
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; }
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
25
|
-
*
|
|
26
|
-
* This source code is licensed under the MIT license found in the
|
|
27
|
-
* LICENSE file in the root directory of this source tree.
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @format
|
|
31
|
-
*/
|
|
32
23
|
function createInsertStatementMutation(side, target, nodesToInsert) {
|
|
33
24
|
if (nodesToInsert.length === 0) {
|
|
34
25
|
return null;
|
|
@@ -44,7 +35,7 @@ function createInsertStatementMutation(side, target, nodesToInsert) {
|
|
|
44
35
|
|
|
45
36
|
function performInsertStatementMutation(mutationContext, mutation) {
|
|
46
37
|
mutationContext.assertNotDeleted(mutation.target, `Attempted to insert ${mutation.side} a deleted ${mutation.target.type} node. This likely means that you attempted to mutate around the target after it was deleted/replaced.`);
|
|
47
|
-
const insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
38
|
+
const insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
48
39
|
|
|
49
40
|
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(insertionParent.parent, mutation.nodesToInsert)) {
|
|
50
41
|
throw new _Errors.InvalidInsertionError(`import/export cannot be inserted into a ${insertionParent.parent.type}.`);
|
|
@@ -72,15 +63,12 @@ function performInsertStatementMutation(mutationContext, mutation) {
|
|
|
72
63
|
return insertionParent.parent;
|
|
73
64
|
}
|
|
74
65
|
|
|
75
|
-
const statementsToInsert =
|
|
76
|
-
mutation.nodesToInsert;
|
|
66
|
+
const statementsToInsert = mutation.nodesToInsert;
|
|
77
67
|
const {
|
|
78
68
|
parent,
|
|
79
69
|
key
|
|
80
|
-
} = insertionParent;
|
|
81
|
-
|
|
82
|
-
const statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
|
|
83
|
-
|
|
70
|
+
} = insertionParent;
|
|
71
|
+
const statementToWrap = parent[key];
|
|
84
72
|
const blockStatement = t.BlockStatement({
|
|
85
73
|
body: mutation.side === 'before' ? [...statementsToInsert, statementToWrap] : [statementToWrap, ...statementsToInsert],
|
|
86
74
|
parent: insertionParent.parent
|
|
@@ -18,11 +18,11 @@ import {isValidModuleDeclarationParent} from './utils/isValidModuleDeclarationPa
|
|
|
18
18
|
import {InvalidInsertionError} from '../Errors';
|
|
19
19
|
import * as t from '../../generated/node-types';
|
|
20
20
|
|
|
21
|
-
export type InsertStatementMutation =
|
|
21
|
+
export type InsertStatementMutation = Readonly<{
|
|
22
22
|
type: 'insertStatement',
|
|
23
23
|
side: 'before' | 'after',
|
|
24
24
|
target: ModuleDeclaration | Statement,
|
|
25
|
-
nodesToInsert:
|
|
25
|
+
nodesToInsert: ReadonlyArray<DetachedNode<Statement | ModuleDeclaration>>,
|
|
26
26
|
}>;
|
|
27
27
|
|
|
28
28
|
export function createInsertStatementMutation(
|
|
@@ -69,7 +69,7 @@ export function performInsertStatementMutation(
|
|
|
69
69
|
|
|
70
70
|
if (insertionParent.type === 'array') {
|
|
71
71
|
const parent: interface {
|
|
72
|
-
[string]:
|
|
72
|
+
[string]: ReadonlyArray<DetachedNode<Statement | ModuleDeclaration>>,
|
|
73
73
|
} = insertionParent.parent;
|
|
74
74
|
switch (mutation.side) {
|
|
75
75
|
case 'before': {
|
|
@@ -96,7 +96,7 @@ export function performInsertStatementMutation(
|
|
|
96
96
|
|
|
97
97
|
const statementsToInsert =
|
|
98
98
|
// $FlowExpectedError[incompatible-type] -- this is enforced by isValidModuleDeclarationParent above
|
|
99
|
-
|
|
99
|
+
mutation.nodesToInsert as ReadonlyArray<DetachedNode<Statement>>;
|
|
100
100
|
|
|
101
101
|
const {parent, key} = insertionParent;
|
|
102
102
|
|
|
@@ -111,8 +111,9 @@ export function performInsertStatementMutation(
|
|
|
111
111
|
parent: insertionParent.parent,
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
-
(insertionParent.parent
|
|
115
|
-
|
|
114
|
+
(insertionParent.parent as interface {[string]: unknown})[
|
|
115
|
+
insertionParent.key
|
|
116
|
+
] = blockStatement;
|
|
116
117
|
statementToWrap.parent = blockStatement;
|
|
117
118
|
|
|
118
119
|
return insertionParent.parent;
|
|
@@ -8,15 +8,6 @@ exports.performModifyNodeInPlaceMutation = performModifyNodeInPlaceMutation;
|
|
|
8
8
|
|
|
9
9
|
var _hermesParser = require("hermes-parser");
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
13
|
-
*
|
|
14
|
-
* This source code is licensed under the MIT license found in the
|
|
15
|
-
* LICENSE file in the root directory of this source tree.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @format
|
|
19
|
-
*/
|
|
20
11
|
function createModifyNodeInPlaceMutation(target, newProps) {
|
|
21
12
|
return {
|
|
22
13
|
type: 'modifyNodeInPlace',
|
|
@@ -32,13 +23,11 @@ function performModifyNodeInPlaceMutation(mutationContext, mutation) {
|
|
|
32
23
|
} = mutation;
|
|
33
24
|
|
|
34
25
|
for (const [key, newPropValue] of Object.entries(newProps)) {
|
|
35
|
-
const prevPropValue = target[key];
|
|
36
|
-
// $FlowFixMe[invalid-compare]
|
|
26
|
+
const prevPropValue = target[key];
|
|
37
27
|
|
|
38
28
|
if (prevPropValue === newPropValue) {
|
|
39
29
|
continue;
|
|
40
|
-
}
|
|
41
|
-
|
|
30
|
+
}
|
|
42
31
|
|
|
43
32
|
if ((0, _hermesParser.isNode)(prevPropValue)) {
|
|
44
33
|
mutationContext.markDeletion(prevPropValue);
|
|
@@ -48,11 +37,9 @@ function performModifyNodeInPlaceMutation(mutationContext, mutation) {
|
|
|
48
37
|
mutationContext.markDeletion(prevPropValueItem);
|
|
49
38
|
}
|
|
50
39
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
mutationContext.markMutation(target, key); // Assign new property.
|
|
40
|
+
}
|
|
55
41
|
|
|
42
|
+
mutationContext.markMutation(target, key);
|
|
56
43
|
target[key] = newPropValue;
|
|
57
44
|
}
|
|
58
45
|
|
|
@@ -13,10 +13,10 @@ import type {MutationContext} from '../MutationContext';
|
|
|
13
13
|
|
|
14
14
|
import {isNode} from 'hermes-parser';
|
|
15
15
|
|
|
16
|
-
export type ModifyNodeInPlaceMutation =
|
|
16
|
+
export type ModifyNodeInPlaceMutation = Readonly<{
|
|
17
17
|
type: 'modifyNodeInPlace',
|
|
18
18
|
target: ESNode,
|
|
19
|
-
newProps:
|
|
19
|
+
newProps: Readonly<{...}>,
|
|
20
20
|
}>;
|
|
21
21
|
|
|
22
22
|
export function createModifyNodeInPlaceMutation(
|
|
@@ -10,15 +10,6 @@ var _hermesParser = require("hermes-parser");
|
|
|
10
10
|
|
|
11
11
|
var _comments = require("../comments/comments");
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
-
*
|
|
16
|
-
* This source code is licensed under the MIT license found in the
|
|
17
|
-
* LICENSE file in the root directory of this source tree.
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @format
|
|
21
|
-
*/
|
|
22
13
|
function createRemoveCommentMutation(comment) {
|
|
23
14
|
return {
|
|
24
15
|
type: 'removeComment',
|
|
@@ -55,7 +46,6 @@ function performRemoveCommentMutations(ast, mutations) {
|
|
|
55
46
|
(0, _comments.setCommentsOnNode)(node, Array.from(nodeCommentsSet));
|
|
56
47
|
|
|
57
48
|
if (commentsToRemove.size === 0) {
|
|
58
|
-
// no more comments to process - so we can exit traversal
|
|
59
49
|
throw _hermesParser.SimpleTraverserBreak;
|
|
60
50
|
}
|
|
61
51
|
},
|
|
@@ -13,7 +13,7 @@ import type {Comment, Program} from 'hermes-estree';
|
|
|
13
13
|
import {SimpleTraverser, SimpleTraverserBreak} from 'hermes-parser';
|
|
14
14
|
import {getCommentsForNode, setCommentsOnNode} from '../comments/comments';
|
|
15
15
|
|
|
16
|
-
export type RemoveCommentMutation =
|
|
16
|
+
export type RemoveCommentMutation = Readonly<{
|
|
17
17
|
type: 'removeComment',
|
|
18
18
|
comment: Comment,
|
|
19
19
|
}>;
|
|
@@ -29,7 +29,7 @@ export function createRemoveCommentMutation(
|
|
|
29
29
|
|
|
30
30
|
export function performRemoveCommentMutations(
|
|
31
31
|
ast: Program,
|
|
32
|
-
mutations:
|
|
32
|
+
mutations: ReadonlyArray<RemoveCommentMutation>,
|
|
33
33
|
): void {
|
|
34
34
|
if (mutations.length === 0) {
|
|
35
35
|
return;
|
|
@@ -65,8 +65,8 @@ export function performRemoveCommentMutations(
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function intersectSets<T>(
|
|
68
|
-
first:
|
|
69
|
-
other:
|
|
68
|
+
first: ReadonlySet<T>,
|
|
69
|
+
other: ReadonlySet<T>,
|
|
70
70
|
): Set<T> {
|
|
71
71
|
const ret = new Set<T>();
|
|
72
72
|
for (const value of first) {
|
|
@@ -10,15 +10,6 @@ var _hermesParser = require("hermes-parser");
|
|
|
10
10
|
|
|
11
11
|
var _Errors = require("../Errors");
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
-
*
|
|
16
|
-
* This source code is licensed under the MIT license found in the
|
|
17
|
-
* LICENSE file in the root directory of this source tree.
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @format
|
|
21
|
-
*/
|
|
22
13
|
function createRemoveNodeMutation(node) {
|
|
23
14
|
return {
|
|
24
15
|
type: 'removeNode',
|
|
@@ -46,7 +37,6 @@ function getRemovalParent(node) {
|
|
|
46
37
|
}
|
|
47
38
|
|
|
48
39
|
switch (node.type) {
|
|
49
|
-
// ClassMember
|
|
50
40
|
case 'PropertyDefinition':
|
|
51
41
|
case 'MethodDefinition':
|
|
52
42
|
assertParent('ClassBody');
|
|
@@ -58,7 +48,6 @@ function getRemovalParent(node) {
|
|
|
58
48
|
case 'EnumStringMember':
|
|
59
49
|
assertParent(VALID_ENUM_MEMBER_PARENTS);
|
|
60
50
|
return 'members';
|
|
61
|
-
// Components
|
|
62
51
|
|
|
63
52
|
case 'ComponentParameter':
|
|
64
53
|
assertParent('ComponentDeclaration');
|
|
@@ -67,7 +56,6 @@ function getRemovalParent(node) {
|
|
|
67
56
|
case 'ComponentTypeParameter':
|
|
68
57
|
assertParent(VALID_COMPONENT_TYPE_PARAMETER_PARENTS);
|
|
69
58
|
return 'params';
|
|
70
|
-
// FunctionParameter
|
|
71
59
|
|
|
72
60
|
case 'AssignmentPattern':
|
|
73
61
|
case 'ArrayPattern':
|
|
@@ -107,7 +95,6 @@ function getRemovalParent(node) {
|
|
|
107
95
|
case 'Property':
|
|
108
96
|
assertParent(VALID_PROPERTY_PARENTS);
|
|
109
97
|
return 'properties';
|
|
110
|
-
// Identifier can be the child of a number of usecases
|
|
111
98
|
|
|
112
99
|
case 'Identifier':
|
|
113
100
|
switch (node.parent.type) {
|
|
@@ -124,8 +111,6 @@ function getRemovalParent(node) {
|
|
|
124
111
|
throw new _Errors.InvalidRemovalError(getErrorMessage(['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrayExpression', 'ArrayPattern']));
|
|
125
112
|
}
|
|
126
113
|
|
|
127
|
-
// RestElement can be the child of a number of usecases
|
|
128
|
-
|
|
129
114
|
case 'RestElement':
|
|
130
115
|
switch (node.parent.type) {
|
|
131
116
|
case 'ArrowFunctionExpression':
|
|
@@ -139,8 +124,6 @@ function getRemovalParent(node) {
|
|
|
139
124
|
|
|
140
125
|
case 'ObjectPattern':
|
|
141
126
|
return 'properties';
|
|
142
|
-
// $FlowFixMe[incompatible-type]
|
|
143
|
-
// $FlowFixMe[invalid-compare]
|
|
144
127
|
|
|
145
128
|
case 'OptionalCallExpression':
|
|
146
129
|
case 'CallExpression':
|
|
@@ -151,8 +134,6 @@ function getRemovalParent(node) {
|
|
|
151
134
|
throw new _Errors.InvalidRemovalError(getErrorMessage(['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'ComponentDeclaration', 'ArrayPattern', 'ObjectPattern', 'CallExpression', 'OptionalCallExpression', 'NewExpression']));
|
|
152
135
|
}
|
|
153
136
|
|
|
154
|
-
// SpreadElement can be the child of a number of usecases
|
|
155
|
-
|
|
156
137
|
case 'SpreadElement':
|
|
157
138
|
switch (node.parent.type) {
|
|
158
139
|
case 'ArrayExpression':
|
|
@@ -160,8 +141,6 @@ function getRemovalParent(node) {
|
|
|
160
141
|
|
|
161
142
|
case 'ObjectExpression':
|
|
162
143
|
return 'properties';
|
|
163
|
-
// $FlowFixMe[incompatible-type]
|
|
164
|
-
// $FlowFixMe[invalid-compare]
|
|
165
144
|
|
|
166
145
|
case 'OptionalCallExpression':
|
|
167
146
|
case 'CallExpression':
|
|
@@ -178,9 +157,8 @@ function getRemovalParent(node) {
|
|
|
178
157
|
})();
|
|
179
158
|
|
|
180
159
|
const targetIndex = (() => {
|
|
181
|
-
// $FlowExpectedError[prop-missing]
|
|
182
160
|
const arr = node.parent[key];
|
|
183
|
-
const idx = arr.indexOf(node);
|
|
161
|
+
const idx = arr.indexOf(node);
|
|
184
162
|
|
|
185
163
|
if (idx === -1) {
|
|
186
164
|
throw new _Errors.InvalidRemovalError(`Could not find target in array of \`${node.parent.type}.${key}\`.`);
|