hermes-transform 0.5.0 → 0.8.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 +1728 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +921 -780
- package/dist/generated/node-types.js +1099 -1156
- package/dist/generated/node-types.js.flow +77 -125
- 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 +8 -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 +4 -3
- package/dist/transform/TransformContext.js +100 -58
- package/dist/transform/TransformContext.js.flow +277 -141
- package/dist/transform/comments/comments.js +127 -40
- package/dist/transform/comments/comments.js.flow +106 -29
- 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 +81 -104
- package/dist/transform/getTransformedAST.js.flow +18 -13
- package/dist/transform/mutations/AddComments.js +43 -0
- package/dist/transform/mutations/AddComments.js.flow +50 -0
- package/dist/transform/mutations/CloneCommentsTo.js +16 -24
- package/dist/transform/mutations/CloneCommentsTo.js.flow +2 -3
- package/dist/transform/mutations/InsertStatement.js +28 -30
- package/dist/transform/mutations/InsertStatement.js.flow +7 -2
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +192 -0
- package/dist/transform/mutations/RemoveNode.js.flow +290 -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 +16 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +16 -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 +58 -30
- package/dist/transform/transform.js.flow +51 -13
- 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 +64 -52
- package/dist/traverse/traverse.js.flow +63 -10
- package/package.json +5 -3
- package/dist/transform/mutations/AddLeadingComments.js +0 -47
- package/dist/transform/mutations/AddLeadingComments.js.flow +0 -49
- package/dist/transform/mutations/AddTrailingComments.js +0 -47
- package/dist/transform/mutations/AddTrailingComments.js.flow +0 -49
|
@@ -10,16 +10,19 @@ var _SimpleTraverser = require("../../traverse/SimpleTraverser");
|
|
|
10
10
|
|
|
11
11
|
var _comments = require("../comments/comments");
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
+
*/
|
|
19
22
|
function createRemoveCommentMutation(comment) {
|
|
20
23
|
return {
|
|
21
24
|
type: 'removeComment',
|
|
22
|
-
comment
|
|
25
|
+
comment
|
|
23
26
|
};
|
|
24
27
|
}
|
|
25
28
|
|
|
@@ -28,37 +31,25 @@ function performRemoveCommentMutations(ast, mutations) {
|
|
|
28
31
|
return;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
return m.comment;
|
|
33
|
-
}));
|
|
34
|
+
const commentsToRemove = new Set(mutations.map(m => m.comment));
|
|
34
35
|
|
|
35
36
|
_SimpleTraverser.SimpleTraverser.traverse(ast, {
|
|
36
|
-
enter
|
|
37
|
+
enter(node) {
|
|
37
38
|
if (node === ast) {
|
|
38
39
|
return;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
const nodeCommentsSet = new Set((0, _comments.getCommentsForNode)(node));
|
|
42
43
|
|
|
43
44
|
if (nodeCommentsSet.size === 0) {
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
54
|
-
var comment = _step.value;
|
|
55
|
-
commentsToRemove["delete"](comment);
|
|
56
|
-
nodeCommentsSet["delete"](comment);
|
|
57
|
-
}
|
|
58
|
-
} catch (err) {
|
|
59
|
-
_iterator.e(err);
|
|
60
|
-
} finally {
|
|
61
|
-
_iterator.f();
|
|
48
|
+
const matchedComments = intersectSets(commentsToRemove, nodeCommentsSet);
|
|
49
|
+
|
|
50
|
+
for (const comment of matchedComments) {
|
|
51
|
+
commentsToRemove.delete(comment);
|
|
52
|
+
nodeCommentsSet.delete(comment);
|
|
62
53
|
}
|
|
63
54
|
|
|
64
55
|
(0, _comments.setCommentsOnNode)(node, Array.from(nodeCommentsSet));
|
|
@@ -68,28 +59,19 @@ function performRemoveCommentMutations(ast, mutations) {
|
|
|
68
59
|
throw _SimpleTraverser.SimpleTraverserBreak;
|
|
69
60
|
}
|
|
70
61
|
},
|
|
71
|
-
|
|
62
|
+
|
|
63
|
+
leave() {}
|
|
64
|
+
|
|
72
65
|
});
|
|
73
66
|
}
|
|
74
67
|
|
|
75
68
|
function intersectSets(first, other) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
var _iterator2 = _createForOfIteratorHelper(first),
|
|
79
|
-
_step2;
|
|
69
|
+
const ret = new Set();
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (other.has(value)) {
|
|
86
|
-
ret.add(value);
|
|
87
|
-
}
|
|
71
|
+
for (const value of first) {
|
|
72
|
+
if (other.has(value)) {
|
|
73
|
+
ret.add(value);
|
|
88
74
|
}
|
|
89
|
-
} catch (err) {
|
|
90
|
-
_iterator2.e(err);
|
|
91
|
-
} finally {
|
|
92
|
-
_iterator2.f();
|
|
93
75
|
}
|
|
94
76
|
|
|
95
77
|
return ret;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createRemoveNodeMutation = createRemoveNodeMutation;
|
|
7
|
+
exports.performRemoveNodeMutation = performRemoveNodeMutation;
|
|
8
|
+
|
|
9
|
+
var _arrayUtils = require("./utils/arrayUtils");
|
|
10
|
+
|
|
11
|
+
var _Errors = require("../Errors");
|
|
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
|
+
function createRemoveNodeMutation(node) {
|
|
23
|
+
return {
|
|
24
|
+
type: 'removeNode',
|
|
25
|
+
node
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const VALID_ENUM_MEMBER_PARENTS = ['EnumBooleanBody', 'EnumNumberBody', 'EnumStringBody', 'EnumSymbolBody'];
|
|
30
|
+
const VALID_FUNCTION_PARAMETER_PARENTS = ['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression'];
|
|
31
|
+
const VALID_PROPERTY_PARENTS = ['ObjectExpression', 'ObjectPattern'];
|
|
32
|
+
|
|
33
|
+
function getRemovalParent(node) {
|
|
34
|
+
const key = (() => {
|
|
35
|
+
function getErrorMessage(expectedParents) {
|
|
36
|
+
return `Tried to remove ${node.type} from parent of type ${node.parent.type}.\nHowever ${node.type} can only be safely removed from parent of type ${expectedParents.join(' | ')}.`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function assertParent(expectedParents_) {
|
|
40
|
+
const expectedParents = typeof expectedParents_ === 'string' ? [expectedParents_] : expectedParents_;
|
|
41
|
+
|
|
42
|
+
if (!expectedParents.includes(node.parent.type)) {
|
|
43
|
+
return new _Errors.InvalidRemovalError(getErrorMessage(expectedParents));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
switch (node.type) {
|
|
48
|
+
// ClassMember
|
|
49
|
+
case 'PropertyDefinition':
|
|
50
|
+
case 'MethodDefinition':
|
|
51
|
+
assertParent('ClassBody');
|
|
52
|
+
return 'body';
|
|
53
|
+
|
|
54
|
+
case 'EnumBooleanMember':
|
|
55
|
+
case 'EnumDefaultedMember':
|
|
56
|
+
case 'EnumNumberMember':
|
|
57
|
+
case 'EnumStringMember':
|
|
58
|
+
assertParent(VALID_ENUM_MEMBER_PARENTS);
|
|
59
|
+
return 'members';
|
|
60
|
+
// FunctionParameter
|
|
61
|
+
|
|
62
|
+
case 'AssignmentPattern':
|
|
63
|
+
case 'ArrayPattern':
|
|
64
|
+
case 'ObjectPattern':
|
|
65
|
+
assertParent(VALID_FUNCTION_PARAMETER_PARENTS);
|
|
66
|
+
return 'params';
|
|
67
|
+
|
|
68
|
+
case 'FunctionTypeParam':
|
|
69
|
+
assertParent('FunctionTypeAnnotation');
|
|
70
|
+
return 'params';
|
|
71
|
+
|
|
72
|
+
case 'JSXAttribute':
|
|
73
|
+
assertParent('JSXOpeningElement');
|
|
74
|
+
return 'attributes';
|
|
75
|
+
|
|
76
|
+
case 'ImportDeclaration':
|
|
77
|
+
assertParent('Program');
|
|
78
|
+
return 'body';
|
|
79
|
+
|
|
80
|
+
case 'ObjectTypeCallProperty':
|
|
81
|
+
assertParent('ObjectTypeAnnotation');
|
|
82
|
+
return 'callProperties';
|
|
83
|
+
|
|
84
|
+
case 'ObjectTypeIndexer':
|
|
85
|
+
assertParent('ObjectTypeAnnotation');
|
|
86
|
+
return 'indexers';
|
|
87
|
+
|
|
88
|
+
case 'ObjectTypeInternalSlot':
|
|
89
|
+
assertParent('ObjectTypeAnnotation');
|
|
90
|
+
return 'internalSlots';
|
|
91
|
+
|
|
92
|
+
case 'ObjectTypeProperty':
|
|
93
|
+
case 'ObjectTypeSpreadProperty':
|
|
94
|
+
assertParent('ObjectTypeAnnotation');
|
|
95
|
+
return 'properties';
|
|
96
|
+
|
|
97
|
+
case 'Property':
|
|
98
|
+
assertParent(VALID_PROPERTY_PARENTS);
|
|
99
|
+
return 'properties';
|
|
100
|
+
// Identifier can be the child of a number of usecases
|
|
101
|
+
|
|
102
|
+
case 'Identifier':
|
|
103
|
+
switch (node.parent.type) {
|
|
104
|
+
case 'ArrowFunctionExpression':
|
|
105
|
+
case 'FunctionDeclaration':
|
|
106
|
+
case 'FunctionExpression':
|
|
107
|
+
return 'params';
|
|
108
|
+
|
|
109
|
+
case 'ArrayExpression':
|
|
110
|
+
case 'ArrayPattern':
|
|
111
|
+
return 'elements';
|
|
112
|
+
|
|
113
|
+
default:
|
|
114
|
+
throw new _Errors.InvalidRemovalError(getErrorMessage(['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrayExpression', 'ArrayPattern']));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// RestElement can be the child of a number of usecases
|
|
118
|
+
|
|
119
|
+
case 'RestElement':
|
|
120
|
+
switch (node.parent.type) {
|
|
121
|
+
case 'ArrowFunctionExpression':
|
|
122
|
+
case 'FunctionDeclaration':
|
|
123
|
+
case 'FunctionExpression':
|
|
124
|
+
return 'params';
|
|
125
|
+
|
|
126
|
+
case 'ArrayPattern':
|
|
127
|
+
return 'elements';
|
|
128
|
+
|
|
129
|
+
case 'ObjectPattern':
|
|
130
|
+
return 'properties';
|
|
131
|
+
|
|
132
|
+
case 'CallExpression':
|
|
133
|
+
case 'OptionalCallExpression':
|
|
134
|
+
case 'NewExpression':
|
|
135
|
+
return 'arguments';
|
|
136
|
+
|
|
137
|
+
default:
|
|
138
|
+
throw new _Errors.InvalidRemovalError(getErrorMessage(['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrayPattern', 'ObjectPattern', 'CallExpression', 'OptionalCallExpression', 'NewExpression']));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// SpreadElement can be the child of a number of usecases
|
|
142
|
+
|
|
143
|
+
case 'SpreadElement':
|
|
144
|
+
switch (node.parent.type) {
|
|
145
|
+
case 'ArrayExpression':
|
|
146
|
+
return 'elements';
|
|
147
|
+
|
|
148
|
+
case 'ObjectExpression':
|
|
149
|
+
return 'properties';
|
|
150
|
+
|
|
151
|
+
case 'CallExpression':
|
|
152
|
+
case 'OptionalCallExpression':
|
|
153
|
+
case 'NewExpression':
|
|
154
|
+
return 'arguments';
|
|
155
|
+
|
|
156
|
+
default:
|
|
157
|
+
throw new _Errors.InvalidRemovalError(getErrorMessage(['ArrayExpression', 'ObjectExpression', 'CallExpression', 'OptionalCallExpression', 'NewExpression']));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
default:
|
|
161
|
+
throw new _Errors.InvalidRemovalError(`Cannot perform a remove mutation on node of type ${node.type}`);
|
|
162
|
+
}
|
|
163
|
+
})();
|
|
164
|
+
|
|
165
|
+
const targetIndex = (() => {
|
|
166
|
+
// $FlowExpectedError[prop-missing]
|
|
167
|
+
const arr = node.parent[key];
|
|
168
|
+
const idx = arr.indexOf(node);
|
|
169
|
+
|
|
170
|
+
if (idx === -1) {
|
|
171
|
+
throw new _Errors.InvalidRemovalError(`Could not find target in array of \`${node.parent.type}.${key}\`.`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return idx;
|
|
175
|
+
})();
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
type: 'array',
|
|
179
|
+
parent: node.parent,
|
|
180
|
+
key,
|
|
181
|
+
targetIndex
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function performRemoveNodeMutation(mutationContext, mutation) {
|
|
186
|
+
const removalParent = getRemovalParent(mutation.node);
|
|
187
|
+
mutationContext.markDeletion(mutation.node);
|
|
188
|
+
mutationContext.markMutation(removalParent.parent, removalParent.key);
|
|
189
|
+
const parent = removalParent.parent;
|
|
190
|
+
parent[removalParent.key] = (0, _arrayUtils.removeFromArray)(parent[removalParent.key], removalParent.targetIndex);
|
|
191
|
+
return removalParent.parent;
|
|
192
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
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
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ESNode,
|
|
13
|
+
ClassMember,
|
|
14
|
+
EnumBooleanMember,
|
|
15
|
+
EnumDefaultedMember,
|
|
16
|
+
EnumNumberMember,
|
|
17
|
+
EnumStringMember,
|
|
18
|
+
FunctionParameter,
|
|
19
|
+
FunctionTypeParam,
|
|
20
|
+
JSXAttribute,
|
|
21
|
+
ImportDeclaration,
|
|
22
|
+
ObjectTypeCallProperty,
|
|
23
|
+
ObjectTypeIndexer,
|
|
24
|
+
ObjectTypeInternalSlot,
|
|
25
|
+
ObjectTypeProperty,
|
|
26
|
+
ObjectTypeSpreadProperty,
|
|
27
|
+
Property,
|
|
28
|
+
SpreadElement,
|
|
29
|
+
} from 'hermes-estree';
|
|
30
|
+
import type {MutationContext} from '../MutationContext';
|
|
31
|
+
import type {DetachedNode} from '../../detachedNode';
|
|
32
|
+
|
|
33
|
+
import {removeFromArray} from './utils/arrayUtils';
|
|
34
|
+
import {InvalidRemovalError} from '../Errors';
|
|
35
|
+
|
|
36
|
+
export type RemoveNodeMutation = $ReadOnly<{
|
|
37
|
+
type: 'removeNode',
|
|
38
|
+
node:
|
|
39
|
+
| ClassMember
|
|
40
|
+
| EnumBooleanMember
|
|
41
|
+
| EnumDefaultedMember
|
|
42
|
+
| EnumNumberMember
|
|
43
|
+
| EnumStringMember
|
|
44
|
+
| FunctionParameter
|
|
45
|
+
| FunctionTypeParam
|
|
46
|
+
| JSXAttribute
|
|
47
|
+
| ImportDeclaration
|
|
48
|
+
| ObjectTypeCallProperty
|
|
49
|
+
| ObjectTypeIndexer
|
|
50
|
+
| ObjectTypeInternalSlot
|
|
51
|
+
| ObjectTypeProperty
|
|
52
|
+
| ObjectTypeSpreadProperty
|
|
53
|
+
| Property
|
|
54
|
+
| SpreadElement,
|
|
55
|
+
}>;
|
|
56
|
+
|
|
57
|
+
export function createRemoveNodeMutation(
|
|
58
|
+
node: RemoveNodeMutation['node'],
|
|
59
|
+
): RemoveNodeMutation {
|
|
60
|
+
return {
|
|
61
|
+
type: 'removeNode',
|
|
62
|
+
node,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const VALID_ENUM_MEMBER_PARENTS: $ReadOnlyArray<string> = [
|
|
67
|
+
'EnumBooleanBody',
|
|
68
|
+
'EnumNumberBody',
|
|
69
|
+
'EnumStringBody',
|
|
70
|
+
'EnumSymbolBody',
|
|
71
|
+
];
|
|
72
|
+
const VALID_FUNCTION_PARAMETER_PARENTS: $ReadOnlyArray<string> = [
|
|
73
|
+
'ArrowFunctionExpression',
|
|
74
|
+
'FunctionDeclaration',
|
|
75
|
+
'FunctionExpression',
|
|
76
|
+
];
|
|
77
|
+
const VALID_PROPERTY_PARENTS: $ReadOnlyArray<string> = [
|
|
78
|
+
'ObjectExpression',
|
|
79
|
+
'ObjectPattern',
|
|
80
|
+
];
|
|
81
|
+
function getRemovalParent(node: RemoveNodeMutation['node']): $ReadOnly<{
|
|
82
|
+
type: 'array',
|
|
83
|
+
parent: ESNode,
|
|
84
|
+
key: string,
|
|
85
|
+
targetIndex: number,
|
|
86
|
+
}> {
|
|
87
|
+
const key = ((): string => {
|
|
88
|
+
function getErrorMessage(expectedParents: $ReadOnlyArray<string>): string {
|
|
89
|
+
return `Tried to remove ${node.type} from parent of type ${
|
|
90
|
+
node.parent.type
|
|
91
|
+
}.\nHowever ${
|
|
92
|
+
node.type
|
|
93
|
+
} can only be safely removed from parent of type ${expectedParents.join(
|
|
94
|
+
' | ',
|
|
95
|
+
)}.`;
|
|
96
|
+
}
|
|
97
|
+
function assertParent(expectedParents_: string | $ReadOnlyArray<string>) {
|
|
98
|
+
const expectedParents =
|
|
99
|
+
typeof expectedParents_ === 'string'
|
|
100
|
+
? [expectedParents_]
|
|
101
|
+
: expectedParents_;
|
|
102
|
+
if (!expectedParents.includes(node.parent.type)) {
|
|
103
|
+
return new InvalidRemovalError(getErrorMessage(expectedParents));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
switch (node.type) {
|
|
108
|
+
// ClassMember
|
|
109
|
+
case 'PropertyDefinition':
|
|
110
|
+
case 'MethodDefinition':
|
|
111
|
+
assertParent('ClassBody');
|
|
112
|
+
return 'body';
|
|
113
|
+
|
|
114
|
+
case 'EnumBooleanMember':
|
|
115
|
+
case 'EnumDefaultedMember':
|
|
116
|
+
case 'EnumNumberMember':
|
|
117
|
+
case 'EnumStringMember':
|
|
118
|
+
assertParent(VALID_ENUM_MEMBER_PARENTS);
|
|
119
|
+
return 'members';
|
|
120
|
+
|
|
121
|
+
// FunctionParameter
|
|
122
|
+
case 'AssignmentPattern':
|
|
123
|
+
case 'ArrayPattern':
|
|
124
|
+
case 'ObjectPattern':
|
|
125
|
+
assertParent(VALID_FUNCTION_PARAMETER_PARENTS);
|
|
126
|
+
return 'params';
|
|
127
|
+
|
|
128
|
+
case 'FunctionTypeParam':
|
|
129
|
+
assertParent('FunctionTypeAnnotation');
|
|
130
|
+
return 'params';
|
|
131
|
+
|
|
132
|
+
case 'JSXAttribute':
|
|
133
|
+
assertParent('JSXOpeningElement');
|
|
134
|
+
return 'attributes';
|
|
135
|
+
|
|
136
|
+
case 'ImportDeclaration':
|
|
137
|
+
assertParent('Program');
|
|
138
|
+
return 'body';
|
|
139
|
+
|
|
140
|
+
case 'ObjectTypeCallProperty':
|
|
141
|
+
assertParent('ObjectTypeAnnotation');
|
|
142
|
+
return 'callProperties';
|
|
143
|
+
|
|
144
|
+
case 'ObjectTypeIndexer':
|
|
145
|
+
assertParent('ObjectTypeAnnotation');
|
|
146
|
+
return 'indexers';
|
|
147
|
+
|
|
148
|
+
case 'ObjectTypeInternalSlot':
|
|
149
|
+
assertParent('ObjectTypeAnnotation');
|
|
150
|
+
return 'internalSlots';
|
|
151
|
+
|
|
152
|
+
case 'ObjectTypeProperty':
|
|
153
|
+
case 'ObjectTypeSpreadProperty':
|
|
154
|
+
assertParent('ObjectTypeAnnotation');
|
|
155
|
+
return 'properties';
|
|
156
|
+
|
|
157
|
+
case 'Property':
|
|
158
|
+
assertParent(VALID_PROPERTY_PARENTS);
|
|
159
|
+
return 'properties';
|
|
160
|
+
|
|
161
|
+
// Identifier can be the child of a number of usecases
|
|
162
|
+
case 'Identifier':
|
|
163
|
+
switch (node.parent.type) {
|
|
164
|
+
case 'ArrowFunctionExpression':
|
|
165
|
+
case 'FunctionDeclaration':
|
|
166
|
+
case 'FunctionExpression':
|
|
167
|
+
return 'params';
|
|
168
|
+
|
|
169
|
+
case 'ArrayExpression':
|
|
170
|
+
case 'ArrayPattern':
|
|
171
|
+
return 'elements';
|
|
172
|
+
|
|
173
|
+
default:
|
|
174
|
+
throw new InvalidRemovalError(
|
|
175
|
+
getErrorMessage([
|
|
176
|
+
'ArrowFunctionExpression',
|
|
177
|
+
'FunctionDeclaration',
|
|
178
|
+
'FunctionExpression',
|
|
179
|
+
'ArrayExpression',
|
|
180
|
+
'ArrayPattern',
|
|
181
|
+
]),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// RestElement can be the child of a number of usecases
|
|
186
|
+
case 'RestElement':
|
|
187
|
+
switch (node.parent.type) {
|
|
188
|
+
case 'ArrowFunctionExpression':
|
|
189
|
+
case 'FunctionDeclaration':
|
|
190
|
+
case 'FunctionExpression':
|
|
191
|
+
return 'params';
|
|
192
|
+
|
|
193
|
+
case 'ArrayPattern':
|
|
194
|
+
return 'elements';
|
|
195
|
+
|
|
196
|
+
case 'ObjectPattern':
|
|
197
|
+
return 'properties';
|
|
198
|
+
|
|
199
|
+
case 'CallExpression':
|
|
200
|
+
case 'OptionalCallExpression':
|
|
201
|
+
case 'NewExpression':
|
|
202
|
+
return 'arguments';
|
|
203
|
+
|
|
204
|
+
default:
|
|
205
|
+
throw new InvalidRemovalError(
|
|
206
|
+
getErrorMessage([
|
|
207
|
+
'ArrowFunctionExpression',
|
|
208
|
+
'FunctionDeclaration',
|
|
209
|
+
'FunctionExpression',
|
|
210
|
+
'ArrayPattern',
|
|
211
|
+
'ObjectPattern',
|
|
212
|
+
'CallExpression',
|
|
213
|
+
'OptionalCallExpression',
|
|
214
|
+
'NewExpression',
|
|
215
|
+
]),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// SpreadElement can be the child of a number of usecases
|
|
220
|
+
case 'SpreadElement':
|
|
221
|
+
switch (node.parent.type) {
|
|
222
|
+
case 'ArrayExpression':
|
|
223
|
+
return 'elements';
|
|
224
|
+
|
|
225
|
+
case 'ObjectExpression':
|
|
226
|
+
return 'properties';
|
|
227
|
+
|
|
228
|
+
case 'CallExpression':
|
|
229
|
+
case 'OptionalCallExpression':
|
|
230
|
+
case 'NewExpression':
|
|
231
|
+
return 'arguments';
|
|
232
|
+
|
|
233
|
+
default:
|
|
234
|
+
throw new InvalidRemovalError(
|
|
235
|
+
getErrorMessage([
|
|
236
|
+
'ArrayExpression',
|
|
237
|
+
'ObjectExpression',
|
|
238
|
+
'CallExpression',
|
|
239
|
+
'OptionalCallExpression',
|
|
240
|
+
'NewExpression',
|
|
241
|
+
]),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
default:
|
|
246
|
+
throw new InvalidRemovalError(
|
|
247
|
+
`Cannot perform a remove mutation on node of type ${node.type}`,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
})();
|
|
251
|
+
|
|
252
|
+
const targetIndex = (() => {
|
|
253
|
+
// $FlowExpectedError[prop-missing]
|
|
254
|
+
const arr = node.parent[key];
|
|
255
|
+
const idx = arr.indexOf(node);
|
|
256
|
+
if (idx === -1) {
|
|
257
|
+
throw new InvalidRemovalError(
|
|
258
|
+
`Could not find target in array of \`${node.parent.type}.${key}\`.`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
return idx;
|
|
262
|
+
})();
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
type: 'array',
|
|
266
|
+
parent: node.parent,
|
|
267
|
+
key,
|
|
268
|
+
targetIndex,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function performRemoveNodeMutation(
|
|
273
|
+
mutationContext: MutationContext,
|
|
274
|
+
mutation: RemoveNodeMutation,
|
|
275
|
+
): ESNode {
|
|
276
|
+
const removalParent = getRemovalParent(mutation.node);
|
|
277
|
+
|
|
278
|
+
mutationContext.markDeletion(mutation.node);
|
|
279
|
+
mutationContext.markMutation(removalParent.parent, removalParent.key);
|
|
280
|
+
|
|
281
|
+
const parent: interface {
|
|
282
|
+
[string]: $ReadOnlyArray<DetachedNode<RemoveNodeMutation['node']>>,
|
|
283
|
+
} = removalParent.parent;
|
|
284
|
+
parent[removalParent.key] = removeFromArray(
|
|
285
|
+
parent[removalParent.key],
|
|
286
|
+
removalParent.targetIndex,
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return removalParent.parent;
|
|
290
|
+
}
|
|
@@ -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
|
});
|
|
@@ -14,12 +12,12 @@ var _getStatementParent = require("./utils/getStatementParent");
|
|
|
14
12
|
|
|
15
13
|
var t = _interopRequireWildcard(require("../../generated/node-types"));
|
|
16
14
|
|
|
17
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function
|
|
15
|
+
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); }
|
|
18
16
|
|
|
19
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null ||
|
|
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; }
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
|
-
* Copyright (c)
|
|
20
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
23
21
|
*
|
|
24
22
|
* This source code is licensed under the MIT license found in the
|
|
25
23
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -30,17 +28,17 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
30
28
|
function createRemoveStatementMutation(node) {
|
|
31
29
|
return {
|
|
32
30
|
type: 'removeStatement',
|
|
33
|
-
node
|
|
31
|
+
node
|
|
34
32
|
};
|
|
35
33
|
}
|
|
36
34
|
|
|
37
35
|
function performRemoveStatementMutation(mutationContext, mutation) {
|
|
38
|
-
|
|
36
|
+
const removalParent = (0, _getStatementParent.getStatementParent)(mutation.node);
|
|
39
37
|
mutationContext.markDeletion(mutation.node);
|
|
40
38
|
mutationContext.markMutation(removalParent.parent, removalParent.key);
|
|
41
39
|
|
|
42
40
|
if (removalParent.type === 'array') {
|
|
43
|
-
|
|
41
|
+
const parent = removalParent.parent;
|
|
44
42
|
parent[removalParent.key] = (0, _arrayUtils.removeFromArray)(parent[removalParent.key], removalParent.targetIndex);
|
|
45
43
|
} else {
|
|
46
44
|
// The parent has a 1:1 relationship on this key, so we can't just
|
|
@@ -50,7 +48,7 @@ function performRemoveStatementMutation(mutationContext, mutation) {
|
|
|
50
48
|
// to write codemods.
|
|
51
49
|
// Worst case it creates some dead code that can be easily detected
|
|
52
50
|
// and cleaned up later.
|
|
53
|
-
|
|
51
|
+
const blockStatement = t.BlockStatement({
|
|
54
52
|
body: [],
|
|
55
53
|
parent: removalParent.parent
|
|
56
54
|
});
|