hermes-transform 0.5.0 → 0.6.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/LICENSE +1 -1
- package/dist/detachedNode.js +25 -49
- package/dist/detachedNode.js.flow +3 -3
- package/dist/generated/TransformCloneSignatures.js.flow +1761 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +936 -774
- package/dist/generated/node-types.js +1117 -1132
- package/dist/generated/node-types.js.flow +26 -1
- package/dist/generated/special-case-node-types.js +106 -85
- package/dist/generated/special-case-node-types.js.flow +28 -2
- package/dist/getVisitorKeys.js +4 -6
- package/dist/getVisitorKeys.js.flow +1 -1
- package/dist/index.js +13 -18
- package/dist/index.js.flow +7 -2
- package/dist/transform/Errors.js +8 -116
- package/dist/transform/Errors.js.flow +1 -1
- package/dist/transform/MutationContext.js +54 -67
- package/dist/transform/MutationContext.js.flow +1 -1
- package/dist/transform/TransformContext.js +106 -54
- package/dist/transform/TransformContext.js.flow +255 -121
- package/dist/transform/comments/comments.js +25 -25
- package/dist/transform/comments/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/common/util.js +46 -47
- package/dist/transform/comments/prettier/common/util.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/comments.js +199 -216
- package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/loc.js +10 -9
- package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/utils.js +18 -30
- package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
- package/dist/transform/comments/prettier/main/comments.js +225 -298
- package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/utils/get-last.js +2 -4
- package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
- package/dist/transform/getTransformedAST.js +85 -101
- package/dist/transform/getTransformedAST.js.flow +7 -2
- package/dist/transform/mutations/AddLeadingComments.js +15 -22
- package/dist/transform/mutations/AddLeadingComments.js.flow +1 -1
- package/dist/transform/mutations/AddTrailingComments.js +15 -22
- package/dist/transform/mutations/AddTrailingComments.js.flow +1 -1
- package/dist/transform/mutations/CloneCommentsTo.js +16 -23
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +27 -30
- package/dist/transform/mutations/InsertStatement.js.flow +1 -1
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +185 -0
- package/dist/transform/mutations/RemoveNode.js.flow +279 -0
- package/dist/transform/mutations/RemoveStatement.js +7 -9
- package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
- package/dist/transform/mutations/ReplaceNode.js +36 -44
- package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
- package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
- package/dist/transform/mutations/utils/arrayUtils.js +2 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +1 -1
- package/dist/transform/mutations/utils/getStatementParent.js +23 -28
- package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
- package/dist/transform/transform.js +20 -24
- package/dist/transform/transform.js.flow +12 -8
- package/dist/traverse/NodeEventGenerator.js +91 -165
- package/dist/traverse/NodeEventGenerator.js.flow +1 -1
- package/dist/traverse/SafeEmitter.js +20 -38
- package/dist/traverse/SafeEmitter.js.flow +1 -1
- package/dist/traverse/SimpleTraverser.js +67 -98
- package/dist/traverse/SimpleTraverser.js.flow +4 -1
- package/dist/traverse/esquery.js +6 -6
- package/dist/traverse/esquery.js.flow +1 -1
- package/dist/traverse/traverse.js +39 -51
- package/dist/traverse/traverse.js.flow +1 -1
- package/package.json +4 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -8,8 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
return arr[arr.length - 1];
|
|
13
|
-
};
|
|
11
|
+
const getLast = arr => arr[arr.length - 1];
|
|
14
12
|
|
|
15
13
|
module.exports = getLast;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -36,123 +36,107 @@ var _InsertStatement = require("./mutations/InsertStatement");
|
|
|
36
36
|
|
|
37
37
|
var _RemoveComment = require("./mutations/RemoveComment");
|
|
38
38
|
|
|
39
|
+
var _RemoveNode = require("./mutations/RemoveNode");
|
|
40
|
+
|
|
39
41
|
var _RemoveStatement = require("./mutations/RemoveStatement");
|
|
40
42
|
|
|
41
43
|
var _ReplaceNode = require("./mutations/ReplaceNode");
|
|
42
44
|
|
|
43
45
|
var _ReplaceStatementWithMany = require("./mutations/ReplaceStatementWithMany");
|
|
44
46
|
|
|
45
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
46
|
-
|
|
47
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
48
|
-
|
|
49
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
50
|
-
|
|
51
47
|
function getTransformedAST(code, visitors) {
|
|
52
|
-
|
|
48
|
+
const {
|
|
49
|
+
ast,
|
|
50
|
+
scopeManager
|
|
51
|
+
} = (0, _hermesEslint.parseForESLint)(code, {
|
|
53
52
|
sourceType: 'module'
|
|
54
|
-
})
|
|
55
|
-
ast = _parseForESLint.ast,
|
|
56
|
-
scopeManager = _parseForESLint.scopeManager; // attach comments before mutation. this will ensure that as nodes are
|
|
53
|
+
}); // attach comments before mutation. this will ensure that as nodes are
|
|
57
54
|
// cloned / moved around - comments remain in the correct place with respect to the node
|
|
58
55
|
|
|
59
|
-
|
|
60
56
|
(0, _comments.attachComments)(ast.comments, ast, code); // traverse the AST and colllect the mutations
|
|
61
57
|
|
|
62
|
-
|
|
63
|
-
(0, _traverse.traverseWithContext)(ast, scopeManager,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
{
|
|
118
|
-
return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}(); // ensure the subtree's parent pointers are correct
|
|
122
|
-
// this is required for two reasons:
|
|
123
|
-
// 1) The userland transform is just JS - so there's nothing stopping them
|
|
124
|
-
// from doing anything dodgy. The flow types have some enforcement, but
|
|
125
|
-
// ofc that can just be ignored with a suppression.
|
|
126
|
-
// 2) Shallow clones are a necessary evil in the transform because they
|
|
127
|
-
// allow codemods to do simple changes to just one node without the
|
|
128
|
-
// weight that comes with deeply cloning the entire AST.
|
|
129
|
-
// However we can't update the parent pointers of the cloned node's
|
|
130
|
-
// children until the mutation step or else we would be mutating
|
|
131
|
-
// real AST nodes and potentially break the traverse step.
|
|
132
|
-
//
|
|
133
|
-
// Being strict here just helps us ensure we keep everything in sync
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (mutationRoot) {
|
|
137
|
-
(0, _detachedNode.updateAllParentPointers)(mutationRoot);
|
|
58
|
+
const transformContext = (0, _TransformContext.getTransformContext)(code);
|
|
59
|
+
(0, _traverse.traverseWithContext)(ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST
|
|
60
|
+
|
|
61
|
+
const mutationContext = new _MutationContext.MutationContext(code);
|
|
62
|
+
const removeCommentMutations = [];
|
|
63
|
+
|
|
64
|
+
for (const mutation of transformContext.mutations) {
|
|
65
|
+
const mutationRoot = (() => {
|
|
66
|
+
switch (mutation.type) {
|
|
67
|
+
case 'insertStatement':
|
|
68
|
+
{
|
|
69
|
+
return (0, _InsertStatement.performInsertStatementMutation)(mutationContext, mutation);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case 'replaceNode':
|
|
73
|
+
{
|
|
74
|
+
return (0, _ReplaceNode.performReplaceNodeMutation)(mutationContext, mutation);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
case 'replaceStatementWithMany':
|
|
78
|
+
{
|
|
79
|
+
return (0, _ReplaceStatementWithMany.performReplaceStatementWithManyMutation)(mutationContext, mutation);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
case 'removeNode':
|
|
83
|
+
{
|
|
84
|
+
return (0, _RemoveNode.performRemoveNodeMutation)(mutationContext, mutation);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
case 'removeStatement':
|
|
88
|
+
{
|
|
89
|
+
return (0, _RemoveStatement.performRemoveStatementMutation)(mutationContext, mutation);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
case 'removeComment':
|
|
93
|
+
{
|
|
94
|
+
// these are handled later
|
|
95
|
+
removeCommentMutations.push(mutation);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
case 'addLeadingComments':
|
|
100
|
+
{
|
|
101
|
+
return (0, _AddLeadingComments.performAddLeadingCommentsMutation)(mutationContext, mutation);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
case 'addTrailingComments':
|
|
105
|
+
{
|
|
106
|
+
return (0, _AddTrailingComments.performAddTrailingCommentsMutation)(mutationContext, mutation);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
case 'cloneCommentsTo':
|
|
110
|
+
{
|
|
111
|
+
return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
|
|
112
|
+
}
|
|
138
113
|
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
//
|
|
145
|
-
//
|
|
114
|
+
})(); // ensure the subtree's parent pointers are correct
|
|
115
|
+
// this is required for two reasons:
|
|
116
|
+
// 1) The userland transform is just JS - so there's nothing stopping them
|
|
117
|
+
// from doing anything dodgy. The flow types have some enforcement, but
|
|
118
|
+
// ofc that can just be ignored with a suppression.
|
|
119
|
+
// 2) Shallow clones are a necessary evil in the transform because they
|
|
120
|
+
// allow codemods to do simple changes to just one node without the
|
|
121
|
+
// weight that comes with deeply cloning the entire AST.
|
|
122
|
+
// However we can't update the parent pointers of the cloned node's
|
|
123
|
+
// children until the mutation step or else we would be mutating
|
|
124
|
+
// real AST nodes and potentially break the traverse step.
|
|
125
|
+
//
|
|
126
|
+
// Being strict here just helps us ensure we keep everything in sync
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if (mutationRoot) {
|
|
130
|
+
(0, _detachedNode.updateAllParentPointers)(mutationRoot);
|
|
131
|
+
}
|
|
132
|
+
} // remove the comments
|
|
133
|
+
// this is done at the end because it requires a complete traversal of the AST
|
|
134
|
+
// so that we can find relevant node's attachment array
|
|
146
135
|
|
|
147
|
-
} catch (err) {
|
|
148
|
-
_iterator.e(err);
|
|
149
|
-
} finally {
|
|
150
|
-
_iterator.f();
|
|
151
|
-
}
|
|
152
136
|
|
|
153
137
|
(0, _RemoveComment.performRemoveCommentMutations)(ast, removeCommentMutations);
|
|
154
138
|
return {
|
|
155
|
-
ast
|
|
139
|
+
ast,
|
|
156
140
|
astWasMutated: transformContext.astWasMutated,
|
|
157
141
|
mutatedCode: mutationContext.code
|
|
158
142
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -26,6 +26,7 @@ import {performAddTrailingCommentsMutation} from './mutations/AddTrailingComment
|
|
|
26
26
|
import {performCloneCommentsToMutation} from './mutations/CloneCommentsTo';
|
|
27
27
|
import {performInsertStatementMutation} from './mutations/InsertStatement';
|
|
28
28
|
import {performRemoveCommentMutations} from './mutations/RemoveComment';
|
|
29
|
+
import {performRemoveNodeMutation} from './mutations/RemoveNode';
|
|
29
30
|
import {performRemoveStatementMutation} from './mutations/RemoveStatement';
|
|
30
31
|
import {performReplaceNodeMutation} from './mutations/ReplaceNode';
|
|
31
32
|
import {performReplaceStatementWithManyMutation} from './mutations/ReplaceStatementWithMany';
|
|
@@ -47,7 +48,7 @@ export function getTransformedAST(
|
|
|
47
48
|
attachComments(ast.comments, ast, code);
|
|
48
49
|
|
|
49
50
|
// traverse the AST and colllect the mutations
|
|
50
|
-
const transformContext = getTransformContext();
|
|
51
|
+
const transformContext = getTransformContext(code);
|
|
51
52
|
traverseWithContext(ast, scopeManager, () => transformContext, visitors);
|
|
52
53
|
|
|
53
54
|
// apply the mutations to the AST
|
|
@@ -73,6 +74,10 @@ export function getTransformedAST(
|
|
|
73
74
|
);
|
|
74
75
|
}
|
|
75
76
|
|
|
77
|
+
case 'removeNode': {
|
|
78
|
+
return performRemoveNodeMutation(mutationContext, mutation);
|
|
79
|
+
}
|
|
80
|
+
|
|
76
81
|
case 'removeStatement': {
|
|
77
82
|
return performRemoveStatementMutation(mutationContext, mutation);
|
|
78
83
|
}
|
|
@@ -8,12 +8,15 @@ exports.performAddLeadingCommentsMutation = performAddLeadingCommentsMutation;
|
|
|
8
8
|
|
|
9
9
|
var _comments = require("../comments/comments");
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
*/
|
|
17
20
|
function createAddLeadingCommentsMutation(node, comments) {
|
|
18
21
|
if (comments.length === 0) {
|
|
19
22
|
return null;
|
|
@@ -21,26 +24,16 @@ function createAddLeadingCommentsMutation(node, comments) {
|
|
|
21
24
|
|
|
22
25
|
return {
|
|
23
26
|
type: 'addLeadingComments',
|
|
24
|
-
comments
|
|
25
|
-
node
|
|
27
|
+
comments,
|
|
28
|
+
node
|
|
26
29
|
};
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
function performAddLeadingCommentsMutation(mutationContext, mutation) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
35
|
-
var originalComment = _step.value;
|
|
36
|
-
var comment = (0, _comments.cloneComment)(originalComment);
|
|
37
|
-
mutationContext.appendCommentToSource(comment);
|
|
38
|
-
(0, _comments.addLeadingComment)(mutation.node, comment);
|
|
39
|
-
}
|
|
40
|
-
} catch (err) {
|
|
41
|
-
_iterator.e(err);
|
|
42
|
-
} finally {
|
|
43
|
-
_iterator.f();
|
|
33
|
+
for (const originalComment of mutation.comments) {
|
|
34
|
+
const comment = (0, _comments.cloneComment)(originalComment);
|
|
35
|
+
mutationContext.appendCommentToSource(comment);
|
|
36
|
+
(0, _comments.addLeadingComment)(mutation.node, comment);
|
|
44
37
|
}
|
|
45
38
|
|
|
46
39
|
return null;
|
|
@@ -8,12 +8,15 @@ exports.performAddTrailingCommentsMutation = performAddTrailingCommentsMutation;
|
|
|
8
8
|
|
|
9
9
|
var _comments = require("../comments/comments");
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
*/
|
|
17
20
|
function createAddTrailingCommentsMutation(node, comments) {
|
|
18
21
|
if (comments.length === 0) {
|
|
19
22
|
return null;
|
|
@@ -21,26 +24,16 @@ function createAddTrailingCommentsMutation(node, comments) {
|
|
|
21
24
|
|
|
22
25
|
return {
|
|
23
26
|
type: 'addTrailingComments',
|
|
24
|
-
comments
|
|
25
|
-
node
|
|
27
|
+
comments,
|
|
28
|
+
node
|
|
26
29
|
};
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
function performAddTrailingCommentsMutation(mutationContext, mutation) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
35
|
-
var originalComment = _step.value;
|
|
36
|
-
var comment = (0, _comments.cloneComment)(originalComment);
|
|
37
|
-
mutationContext.appendCommentToSource(comment);
|
|
38
|
-
(0, _comments.addTrailingComment)(mutation.node, comment);
|
|
39
|
-
}
|
|
40
|
-
} catch (err) {
|
|
41
|
-
_iterator.e(err);
|
|
42
|
-
} finally {
|
|
43
|
-
_iterator.f();
|
|
33
|
+
for (const originalComment of mutation.comments) {
|
|
34
|
+
const comment = (0, _comments.cloneComment)(originalComment);
|
|
35
|
+
mutationContext.appendCommentToSource(comment);
|
|
36
|
+
(0, _comments.addTrailingComment)(mutation.node, comment);
|
|
44
37
|
}
|
|
45
38
|
|
|
46
39
|
return null;
|
|
@@ -8,37 +8,30 @@ exports.performCloneCommentsToMutation = performCloneCommentsToMutation;
|
|
|
8
8
|
|
|
9
9
|
var _comments = require("../comments/comments");
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
*/
|
|
17
20
|
function createCloneCommentsToMutation(target, destination) {
|
|
18
21
|
return {
|
|
19
22
|
type: 'cloneCommentsTo',
|
|
20
|
-
target
|
|
21
|
-
destination
|
|
23
|
+
target,
|
|
24
|
+
destination
|
|
22
25
|
};
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
function performCloneCommentsToMutation(mutationContext, mutation) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var _iterator = _createForOfIteratorHelper((0, _comments.getCommentsForNode)(mutation.target)),
|
|
29
|
-
_step;
|
|
29
|
+
const newComments = [];
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
mutationContext.appendCommentToSource(comment);
|
|
36
|
-
newComments.push(comment);
|
|
37
|
-
}
|
|
38
|
-
} catch (err) {
|
|
39
|
-
_iterator.e(err);
|
|
40
|
-
} finally {
|
|
41
|
-
_iterator.f();
|
|
31
|
+
for (const originalComment of (0, _comments.getCommentsForNode)(mutation.target)) {
|
|
32
|
+
const comment = (0, _comments.cloneCommentWithMarkers)(originalComment);
|
|
33
|
+
mutationContext.appendCommentToSource(comment);
|
|
34
|
+
newComments.push(comment);
|
|
42
35
|
}
|
|
43
36
|
|
|
44
37
|
(0, _comments.addCommentsToNode)(mutation.destination, newComments);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
@@ -18,22 +16,19 @@ var _Errors = require("../Errors");
|
|
|
18
16
|
|
|
19
17
|
var t = _interopRequireWildcard(require("../../generated/node-types"));
|
|
20
18
|
|
|
21
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function
|
|
22
|
-
|
|
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
|
-
|
|
25
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
26
|
-
|
|
27
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
28
|
-
|
|
29
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
30
|
-
|
|
31
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
32
|
-
|
|
33
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
19
|
+
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); }
|
|
34
20
|
|
|
35
|
-
function
|
|
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; }
|
|
36
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
|
+
*/
|
|
37
32
|
function createInsertStatementMutation(side, target, nodesToInsert) {
|
|
38
33
|
if (nodesToInsert.length === 0) {
|
|
39
34
|
return null;
|
|
@@ -41,34 +36,34 @@ function createInsertStatementMutation(side, target, nodesToInsert) {
|
|
|
41
36
|
|
|
42
37
|
return {
|
|
43
38
|
type: 'insertStatement',
|
|
44
|
-
side
|
|
45
|
-
target
|
|
46
|
-
nodesToInsert
|
|
39
|
+
side,
|
|
40
|
+
target,
|
|
41
|
+
nodesToInsert
|
|
47
42
|
};
|
|
48
43
|
}
|
|
49
44
|
|
|
50
45
|
function performInsertStatementMutation(mutationContext, mutation) {
|
|
51
|
-
|
|
46
|
+
const insertionParent = (0, _getStatementParent.getStatementParent)(mutation.target); // enforce that if we are inserting module declarations - they are being inserted in a valid location
|
|
52
47
|
|
|
53
48
|
if (!(0, _isValidModuleDeclarationParent.isValidModuleDeclarationParent)(insertionParent.parent, mutation.nodesToInsert)) {
|
|
54
|
-
throw new _Errors.InvalidInsertionError(
|
|
49
|
+
throw new _Errors.InvalidInsertionError(`import/export cannot be inserted into a ${insertionParent.parent.type}.`);
|
|
55
50
|
}
|
|
56
51
|
|
|
57
52
|
mutationContext.markMutation(insertionParent.parent, insertionParent.key);
|
|
58
53
|
|
|
59
54
|
if (insertionParent.type === 'array') {
|
|
60
|
-
|
|
55
|
+
const parent = insertionParent.parent;
|
|
61
56
|
|
|
62
57
|
switch (mutation.side) {
|
|
63
58
|
case 'before':
|
|
64
59
|
{
|
|
65
|
-
|
|
60
|
+
parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex - 1, mutation.nodesToInsert);
|
|
66
61
|
break;
|
|
67
62
|
}
|
|
68
63
|
|
|
69
64
|
case 'after':
|
|
70
65
|
{
|
|
71
|
-
|
|
66
|
+
parent[insertionParent.key] = (0, _arrayUtils.insertInArray)(parent[insertionParent.key], insertionParent.targetIndex + 1, mutation.nodesToInsert);
|
|
72
67
|
break;
|
|
73
68
|
}
|
|
74
69
|
}
|
|
@@ -76,15 +71,17 @@ function performInsertStatementMutation(mutationContext, mutation) {
|
|
|
76
71
|
return insertionParent.parent;
|
|
77
72
|
}
|
|
78
73
|
|
|
79
|
-
|
|
74
|
+
const statementsToInsert = // $FlowExpectedError[incompatible-cast] -- this is enforced by isValidModuleDeclarationParent above
|
|
80
75
|
mutation.nodesToInsert;
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
const {
|
|
77
|
+
parent,
|
|
78
|
+
key
|
|
79
|
+
} = insertionParent; // $FlowExpectedError[prop-missing]
|
|
83
80
|
|
|
84
|
-
|
|
81
|
+
const statementToWrap = parent[key]; // we need to wrap this key in a BlockStatement so we can insert the new statement
|
|
85
82
|
|
|
86
|
-
|
|
87
|
-
body: mutation.side === 'before' ? [
|
|
83
|
+
const blockStatement = t.BlockStatement({
|
|
84
|
+
body: mutation.side === 'before' ? [...statementsToInsert, statementToWrap] : [statementToWrap, ...statementsToInsert],
|
|
88
85
|
parent: insertionParent.parent
|
|
89
86
|
});
|
|
90
87
|
insertionParent.parent[insertionParent.key] = blockStatement;
|