hermes-transform 0.36.0 → 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 +5 -5
- 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,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;
|
|
@@ -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',
|
|
@@ -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
|
|
@@ -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
|
|
|
@@ -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
|
},
|
|
@@ -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}\`.`);
|
|
@@ -16,15 +16,6 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
16
16
|
|
|
17
17
|
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; }
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
21
|
-
*
|
|
22
|
-
* This source code is licensed under the MIT license found in the
|
|
23
|
-
* LICENSE file in the root directory of this source tree.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @format
|
|
27
|
-
*/
|
|
28
19
|
function createRemoveStatementMutation(node) {
|
|
29
20
|
return {
|
|
30
21
|
type: 'removeStatement',
|
|
@@ -41,13 +32,6 @@ function performRemoveStatementMutation(mutationContext, mutation) {
|
|
|
41
32
|
const parent = removalParent.parent;
|
|
42
33
|
parent[removalParent.key] = _hermesParser.astArrayMutationHelpers.removeFromArray(parent[removalParent.key], removalParent.targetIndex);
|
|
43
34
|
} else {
|
|
44
|
-
// The parent has a 1:1 relationship on this key, so we can't just
|
|
45
|
-
// remove the node. Instead we replace it with an empty block statement.
|
|
46
|
-
// We COULD throw an error here and make the codemodder write a stricter
|
|
47
|
-
// codemod - but we decided to add this bit of magic to make it easier
|
|
48
|
-
// to write codemods.
|
|
49
|
-
// Worst case it creates some dead code that can be easily detected
|
|
50
|
-
// and cleaned up later.
|
|
51
35
|
const blockStatement = t.BlockStatement({
|
|
52
36
|
body: [],
|
|
53
37
|
parent: removalParent.parent
|
|
@@ -14,15 +14,6 @@ var _Errors = require("../Errors");
|
|
|
14
14
|
|
|
15
15
|
var _detachedNode = require("../../detachedNode");
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
19
|
-
*
|
|
20
|
-
* This source code is licensed under the MIT license found in the
|
|
21
|
-
* LICENSE file in the root directory of this source tree.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @format
|
|
25
|
-
*/
|
|
26
17
|
function createReplaceNodeMutation(target, nodeToReplaceWith, options) {
|
|
27
18
|
var _options$keepComments;
|
|
28
19
|
|
|
@@ -37,10 +28,7 @@ function createReplaceNodeMutation(target, nodeToReplaceWith, options) {
|
|
|
37
28
|
function performReplaceNodeMutation(mutationContext, mutation) {
|
|
38
29
|
const replacementParent = getParentKey(mutation.target);
|
|
39
30
|
mutationContext.markDeletion(mutation.target);
|
|
40
|
-
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
41
|
-
// it does no runtime checks and provides no guarantees about the
|
|
42
|
-
// correctness of the resulting code.
|
|
43
|
-
// TODO: maybe add some runtime checks based on codegenned predicates?
|
|
31
|
+
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
44
32
|
|
|
45
33
|
if (replacementParent.type === 'array') {
|
|
46
34
|
const parent = replacementParent.parent;
|
|
@@ -63,7 +51,6 @@ function getParentKey(target) {
|
|
|
63
51
|
const child = parent[key];
|
|
64
52
|
|
|
65
53
|
if ((0, _hermesParser.isNode)(child)) {
|
|
66
|
-
// $FlowFixMe[invalid-compare]
|
|
67
54
|
if (child === target) {
|
|
68
55
|
return {
|
|
69
56
|
type: 'single',
|
|
@@ -86,8 +73,7 @@ function getParentKey(target) {
|
|
|
86
73
|
}
|
|
87
74
|
}
|
|
88
75
|
}
|
|
89
|
-
}
|
|
90
|
-
|
|
76
|
+
}
|
|
91
77
|
|
|
92
78
|
throw new _Errors.InvalidReplacementError(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
93
79
|
}
|
|
@@ -22,15 +22,6 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
22
22
|
|
|
23
23
|
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; }
|
|
24
24
|
|
|
25
|
-
/**
|
|
26
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
27
|
-
*
|
|
28
|
-
* This source code is licensed under the MIT license found in the
|
|
29
|
-
* LICENSE file in the root directory of this source tree.
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @format
|
|
33
|
-
*/
|
|
34
25
|
function createReplaceStatementWithManyMutation(target, nodesToReplaceWith, options) {
|
|
35
26
|
var _options$keepComments;
|
|
36
27
|
|
|
@@ -47,7 +38,7 @@ function createReplaceStatementWithManyMutation(target, nodesToReplaceWith, opti
|
|
|
47
38
|
}
|
|
48
39
|
|
|
49
40
|
function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
50
|
-
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
41
|
+
const replacementParent = (0, _getStatementParent.getStatementParent)(mutation.target);
|
|
51
42
|
|
|
52
43
|
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(replacementParent.parent, mutation.nodesToReplaceWith)) {
|
|
53
44
|
throw new _Errors.InvalidReplacementError(`import/export cannot be replaced into a ${replacementParent.parent.type}.`);
|
|
@@ -57,7 +48,6 @@ function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
|
57
48
|
mutationContext.markMutation(replacementParent.parent, replacementParent.key);
|
|
58
49
|
|
|
59
50
|
if (mutation.keepComments) {
|
|
60
|
-
// attach comments to the very first replacement node
|
|
61
51
|
(0, _comments.moveCommentsToNewNode)(mutation.target, mutation.nodesToReplaceWith[0]);
|
|
62
52
|
}
|
|
63
53
|
|
|
@@ -67,9 +57,7 @@ function performReplaceStatementWithManyMutation(mutationContext, mutation) {
|
|
|
67
57
|
return replacementParent.parent;
|
|
68
58
|
}
|
|
69
59
|
|
|
70
|
-
const statementsToReplaceWith =
|
|
71
|
-
mutation.nodesToReplaceWith; // we need to wrap the nodes in a BlockStatement as before there was only 1 node
|
|
72
|
-
|
|
60
|
+
const statementsToReplaceWith = mutation.nodesToReplaceWith;
|
|
73
61
|
const blockStatement = t.BlockStatement({
|
|
74
62
|
body: statementsToReplaceWith,
|
|
75
63
|
parent: replacementParent.parent
|
|
@@ -7,23 +7,12 @@ exports.getStatementParent = getStatementParent;
|
|
|
7
7
|
|
|
8
8
|
var _Errors = require("../../Errors");
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
-
*
|
|
13
|
-
* This source code is licensed under the MIT license found in the
|
|
14
|
-
* LICENSE file in the root directory of this source tree.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @format
|
|
18
|
-
*/
|
|
19
10
|
function getStatementParent(target) {
|
|
20
11
|
function assertValidStatementLocation(parentWithType, ...invalidKeys) {
|
|
21
12
|
for (const key of invalidKeys) {
|
|
22
|
-
// $FlowExpectedError[prop-missing]
|
|
23
13
|
const value = parentWithType[key];
|
|
24
14
|
|
|
25
|
-
if (
|
|
26
|
-
value === target || Array.isArray(value) && value.includes(target)) {
|
|
15
|
+
if (value === target || Array.isArray(value) && value.includes(target)) {
|
|
27
16
|
throw new _Errors.InvalidStatementError(`Attempted to insert a statement into \`${parentWithType.type}.${key}\`.`);
|
|
28
17
|
}
|
|
29
18
|
}
|
|
@@ -98,9 +87,7 @@ function getStatementParent(target) {
|
|
|
98
87
|
case 'ForInStatement':
|
|
99
88
|
case 'ForOfStatement':
|
|
100
89
|
{
|
|
101
|
-
assertValidStatementLocation(parent,
|
|
102
|
-
'left', // $FlowExpectedError[prop-missing] - flow does not track properties from parent interface
|
|
103
|
-
'right');
|
|
90
|
+
assertValidStatementLocation(parent, 'left', 'right');
|
|
104
91
|
return {
|
|
105
92
|
type: 'single',
|
|
106
93
|
parent,
|
|
@@ -134,10 +121,7 @@ function getStatementParent(target) {
|
|
|
134
121
|
throw new _Errors.InvalidStatementError(`Expected to find a valid statement parent, but found a parent of type "${parent.type}".`);
|
|
135
122
|
})();
|
|
136
123
|
|
|
137
|
-
if (
|
|
138
|
-
result.targetIndex == null && // $FlowExpectedError[prop-missing]
|
|
139
|
-
// $FlowFixMe[invalid-compare]
|
|
140
|
-
result.parent[result.key] !== target) {
|
|
124
|
+
if (result.targetIndex == null && result.parent[result.key] !== target) {
|
|
141
125
|
throw new _Errors.InvalidStatementError(`Expected to find the target "${target.type}" on the "${result.parent.type}.${result.key}", but found a different node. ` + 'This likely means that you attempted to mutate around the target after it was deleted/replaced.');
|
|
142
126
|
}
|
|
143
127
|
|