hermes-estree 0.32.1 → 0.33.1
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/generated/HermesESTreeSelectorTypes.js.flow +262 -27
- package/dist/generated/predicates.js +120 -0
- package/dist/generated/predicates.js.flow +72 -0
- package/dist/predicates.js +2 -4
- package/dist/predicates.js.flow +5 -4
- package/dist/src/generated/predicates.js +120 -0
- package/dist/src/predicates.js +2 -4
- package/dist/types.js.flow +103 -11
- package/package.json +1 -1
- package/dist/HermesAST.js.flow +0 -57
- package/dist/HermesASTAdapter.js +0 -192
- package/dist/HermesASTAdapter.js.flow +0 -189
- package/dist/HermesParser.js +0 -108
- package/dist/HermesParser.js.flow +0 -161
- package/dist/HermesParserDecodeUTF8String.js +0 -68
- package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
- package/dist/HermesParserDeserializer.js +0 -242
- package/dist/HermesParserDeserializer.js.flow +0 -260
- package/dist/HermesParserNodeDeserializers.js +0 -2477
- package/dist/HermesParserNodeDeserializers.js.flow +0 -16
- package/dist/HermesParserWASM.js +0 -6
- package/dist/HermesParserWASM.js.flow +0 -87
- package/dist/HermesToESTreeAdapter.js +0 -439
- package/dist/HermesToESTreeAdapter.js.flow +0 -421
- package/dist/ParserOptions.js +0 -18
- package/dist/ParserOptions.js.flow +0 -41
- package/dist/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
- package/dist/estree/StripFlowTypes.js +0 -175
- package/dist/estree/StripFlowTypes.js.flow +0 -158
- package/dist/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
- package/dist/estree/TransformComponentSyntax.js +0 -788
- package/dist/estree/TransformComponentSyntax.js.flow +0 -864
- package/dist/estree/TransformEnumSyntax.js +0 -106
- package/dist/estree/TransformEnumSyntax.js.flow +0 -125
- package/dist/estree/TransformMatchSyntax.js +0 -1006
- package/dist/estree/TransformMatchSyntax.js.flow +0 -912
- package/dist/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
- package/dist/generated/ParserVisitorKeys.js +0 -794
- package/dist/generated/ParserVisitorKeys.js.flow +0 -17
- package/dist/getModuleDocblock.js +0 -112
- package/dist/getModuleDocblock.js.flow +0 -118
- package/dist/src/HermesASTAdapter.js +0 -192
- package/dist/src/HermesParser.js +0 -108
- package/dist/src/HermesParserDecodeUTF8String.js +0 -68
- package/dist/src/HermesParserDeserializer.js +0 -242
- package/dist/src/HermesParserNodeDeserializers.js +0 -2477
- package/dist/src/HermesToESTreeAdapter.js +0 -439
- package/dist/src/ParserOptions.js +0 -18
- package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
- package/dist/src/estree/StripFlowTypes.js +0 -175
- package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
- package/dist/src/estree/TransformComponentSyntax.js +0 -788
- package/dist/src/estree/TransformEnumSyntax.js +0 -106
- package/dist/src/estree/TransformMatchSyntax.js +0 -1006
- package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
- package/dist/src/generated/ParserVisitorKeys.js +0 -794
- package/dist/src/getModuleDocblock.js +0 -112
- package/dist/src/transform/SimpleTransform.js +0 -136
- package/dist/src/transform/astArrayMutationHelpers.js +0 -62
- package/dist/src/transform/astNodeMutationHelpers.js +0 -200
- package/dist/src/traverse/SimpleTraverser.js +0 -137
- package/dist/src/traverse/getVisitorKeys.js +0 -37
- package/dist/src/utils/Builders.js +0 -191
- package/dist/src/utils/GenID.js +0 -41
- package/dist/src/utils/createSyntaxError.js +0 -25
- package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/transform/SimpleTransform.js +0 -136
- package/dist/transform/SimpleTransform.js.flow +0 -169
- package/dist/transform/astArrayMutationHelpers.js +0 -62
- package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
- package/dist/transform/astNodeMutationHelpers.js +0 -200
- package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
- package/dist/traverse/SimpleTraverser.js +0 -137
- package/dist/traverse/SimpleTraverser.js.flow +0 -133
- package/dist/traverse/getVisitorKeys.js +0 -37
- package/dist/traverse/getVisitorKeys.js.flow +0 -36
- package/dist/utils/Builders.js +0 -191
- package/dist/utils/Builders.js.flow +0 -218
- package/dist/utils/GenID.js +0 -41
- package/dist/utils/GenID.js.flow +0 -38
- package/dist/utils/createSyntaxError.js +0 -25
- package/dist/utils/createSyntaxError.js.flow +0 -24
- package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.arrayIsEqual = arrayIsEqual;
|
|
7
|
-
exports.insertInArray = insertInArray;
|
|
8
|
-
exports.removeFromArray = removeFromArray;
|
|
9
|
-
exports.replaceInArray = replaceInArray;
|
|
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
|
-
function assertArrayBounds(array, index) {
|
|
21
|
-
if (index < 0 || index >= array.length) {
|
|
22
|
-
throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function arrayIsEqual(a1, a2) {
|
|
27
|
-
if (a1 === a2) {
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (a1.length !== a2.length) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
for (let i = 0; i < a1.length; i++) {
|
|
36
|
-
if (a1[i] !== a2[i]) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function insertInArray(array, index, elements) {
|
|
45
|
-
if (index === array.length) {
|
|
46
|
-
// Support the insert at end of array case.
|
|
47
|
-
return array.concat(elements);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
assertArrayBounds(array, index);
|
|
51
|
-
return array.slice(0, index).concat(elements).concat(array.slice(index));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function removeFromArray(array, index) {
|
|
55
|
-
assertArrayBounds(array, index);
|
|
56
|
-
return [...array.slice(0, index), ...array.slice(index + 1)];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function replaceInArray(array, index, elements) {
|
|
60
|
-
assertArrayBounds(array, index);
|
|
61
|
-
return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
|
|
62
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
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
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
function assertArrayBounds<T>(array: $ReadOnlyArray<T>, index: number): void {
|
|
12
|
-
if (index < 0 || index >= array.length) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`,
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function arrayIsEqual(
|
|
20
|
-
a1: $ReadOnlyArray<mixed>,
|
|
21
|
-
a2: $ReadOnlyArray<mixed>,
|
|
22
|
-
): boolean {
|
|
23
|
-
if (a1 === a2) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (a1.length !== a2.length) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
for (let i = 0; i < a1.length; i++) {
|
|
32
|
-
if (a1[i] !== a2[i]) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function insertInArray<T>(
|
|
41
|
-
array: $ReadOnlyArray<T>,
|
|
42
|
-
index: number,
|
|
43
|
-
elements: $ReadOnlyArray<T>,
|
|
44
|
-
): Array<T> {
|
|
45
|
-
if (index === array.length) {
|
|
46
|
-
// Support the insert at end of array case.
|
|
47
|
-
return array.concat(elements);
|
|
48
|
-
}
|
|
49
|
-
assertArrayBounds(array, index);
|
|
50
|
-
return array.slice(0, index).concat(elements).concat(array.slice(index));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function removeFromArray<T>(
|
|
54
|
-
array: $ReadOnlyArray<T>,
|
|
55
|
-
index: number,
|
|
56
|
-
): Array<T> {
|
|
57
|
-
assertArrayBounds(array, index);
|
|
58
|
-
return [...array.slice(0, index), ...array.slice(index + 1)];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function replaceInArray<T>(
|
|
62
|
-
array: $ReadOnlyArray<T>,
|
|
63
|
-
index: number,
|
|
64
|
-
elements: $ReadOnlyArray<T>,
|
|
65
|
-
): Array<T> {
|
|
66
|
-
assertArrayBounds(array, index);
|
|
67
|
-
return array
|
|
68
|
-
.slice(0, index)
|
|
69
|
-
.concat(elements)
|
|
70
|
-
.concat(array.slice(index + 1));
|
|
71
|
-
}
|
|
@@ -1,200 +0,0 @@
|
|
|
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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.deepCloneNode = deepCloneNode;
|
|
16
|
-
exports.nodeWith = nodeWith;
|
|
17
|
-
exports.removeNodeOnParent = removeNodeOnParent;
|
|
18
|
-
exports.replaceNodeOnParent = replaceNodeOnParent;
|
|
19
|
-
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
|
20
|
-
exports.shallowCloneNode = shallowCloneNode;
|
|
21
|
-
exports.updateAllParentPointers = updateAllParentPointers;
|
|
22
|
-
|
|
23
|
-
var _astArrayMutationHelpers = require("./astArrayMutationHelpers");
|
|
24
|
-
|
|
25
|
-
var _getVisitorKeys = require("../traverse/getVisitorKeys");
|
|
26
|
-
|
|
27
|
-
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
28
|
-
|
|
29
|
-
function getParentKey(target, parent, visitorKeys) {
|
|
30
|
-
if (parent == null) {
|
|
31
|
-
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
35
|
-
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
|
-
parent[key])) {
|
|
37
|
-
// $FlowFixMe[invalid-compare]
|
|
38
|
-
if (parent[key] === target) {
|
|
39
|
-
return {
|
|
40
|
-
type: 'single',
|
|
41
|
-
node: parent,
|
|
42
|
-
key
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
} else if (Array.isArray(parent[key])) {
|
|
46
|
-
for (let i = 0; i < parent[key].length; i += 1) {
|
|
47
|
-
// $FlowExpectedError[invalid-tuple-index]
|
|
48
|
-
const current = parent[key][i];
|
|
49
|
-
|
|
50
|
-
if (current === target) {
|
|
51
|
-
return {
|
|
52
|
-
type: 'array',
|
|
53
|
-
node: parent,
|
|
54
|
-
key,
|
|
55
|
-
targetIndex: i
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
} // this shouldn't happen ever
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Replace a node with a new node within an AST (via the parent pointer).
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith, visitorKeys) {
|
|
71
|
-
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
72
|
-
const parent = replacementParent.node;
|
|
73
|
-
|
|
74
|
-
if (replacementParent.type === 'array') {
|
|
75
|
-
// $FlowExpectedError[prop-missing]
|
|
76
|
-
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
77
|
-
parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
|
|
78
|
-
} else {
|
|
79
|
-
if (Array.isArray(nodeToReplaceWith)) {
|
|
80
|
-
throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
|
|
81
|
-
} // $FlowExpectedError[prop-missing]
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
parent[replacementParent.key] = nodeToReplaceWith;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Remove a node from the AST its connected to (via the parent pointer).
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
function removeNodeOnParent(originalNode, originalNodeParent, visitorKeys) {
|
|
93
|
-
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
|
94
|
-
const parent = replacementParent.node;
|
|
95
|
-
|
|
96
|
-
if (replacementParent.type === 'array') {
|
|
97
|
-
// $FlowExpectedError[prop-missing]
|
|
98
|
-
parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)( // $FlowExpectedError[prop-missing]
|
|
99
|
-
parent[replacementParent.key], replacementParent.targetIndex);
|
|
100
|
-
} else {
|
|
101
|
-
// $FlowExpectedError[prop-missing]
|
|
102
|
-
parent[replacementParent.key] = null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Corrects the parent pointers in direct children of the given node.
|
|
107
|
-
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
function setParentPointersInDirectChildren(node, visitorKeys) {
|
|
111
|
-
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node, visitorKeys)) {
|
|
112
|
-
if ((0, _getVisitorKeys.isNode)(node[key])) {
|
|
113
|
-
// $FlowExpectedError[cannot-write]
|
|
114
|
-
node[key].parent = node;
|
|
115
|
-
} else if (Array.isArray(node[key])) {
|
|
116
|
-
for (const child of node[key]) {
|
|
117
|
-
child.parent = node;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
function updateAllParentPointers(node, visitorKeys) {
|
|
128
|
-
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
|
129
|
-
enter(node, parent) {
|
|
130
|
-
// $FlowExpectedError[cannot-write]
|
|
131
|
-
node.parent = parent;
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
leave() {},
|
|
135
|
-
|
|
136
|
-
visitorKeys
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Clone node and add new props.
|
|
141
|
-
*
|
|
142
|
-
* This will only create a new object if the overrides actually result in a change.
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
function nodeWith(node, overrideProps, visitorKeys) {
|
|
147
|
-
// Check if this will actually result in a change, maintaining referential equality is important.
|
|
148
|
-
const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => {
|
|
149
|
-
const node_ = node;
|
|
150
|
-
|
|
151
|
-
if (Array.isArray(value)) {
|
|
152
|
-
return Array.isArray(node_[key]) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node_[key], value) : false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return node_[key] === value;
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
if (willBeUnchanged) {
|
|
159
|
-
return node;
|
|
160
|
-
} // Create new node.
|
|
161
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const newNode = { ...node,
|
|
165
|
-
...overrideProps
|
|
166
|
-
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
167
|
-
|
|
168
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
169
|
-
return newNode;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Shallow clones node, providing a new reference for an existing node.
|
|
173
|
-
*/
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
function shallowCloneNode(node, visitorKeys) {
|
|
177
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
178
|
-
const newNode = { ...node
|
|
179
|
-
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
180
|
-
|
|
181
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
182
|
-
return newNode;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Deeply clones node and its entire tree.
|
|
186
|
-
*/
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
function deepCloneNode(node, visitorKeys) {
|
|
190
|
-
const clone = JSON.parse(JSON.stringify(node, (key, value) => {
|
|
191
|
-
// null out parent pointers
|
|
192
|
-
if (key === 'parent') {
|
|
193
|
-
return undefined;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return value;
|
|
197
|
-
}));
|
|
198
|
-
updateAllParentPointers(clone, visitorKeys);
|
|
199
|
-
return clone;
|
|
200
|
-
}
|
|
@@ -1,246 +0,0 @@
|
|
|
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
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
'use strict';
|
|
12
|
-
|
|
13
|
-
import type {ESNode} from 'hermes-estree';
|
|
14
|
-
import type {VisitorKeysType} from '../traverse/getVisitorKeys';
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
arrayIsEqual,
|
|
18
|
-
removeFromArray,
|
|
19
|
-
replaceInArray,
|
|
20
|
-
} from './astArrayMutationHelpers';
|
|
21
|
-
import {getVisitorKeys, isNode} from '../traverse/getVisitorKeys';
|
|
22
|
-
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
|
23
|
-
|
|
24
|
-
function getParentKey(
|
|
25
|
-
target: ESNode,
|
|
26
|
-
parent: ESNode,
|
|
27
|
-
visitorKeys?: ?VisitorKeysType,
|
|
28
|
-
): $ReadOnly<
|
|
29
|
-
| {
|
|
30
|
-
type: 'single',
|
|
31
|
-
node: ESNode,
|
|
32
|
-
key: string,
|
|
33
|
-
}
|
|
34
|
-
| {
|
|
35
|
-
type: 'array',
|
|
36
|
-
node: ESNode,
|
|
37
|
-
key: string,
|
|
38
|
-
targetIndex: number,
|
|
39
|
-
},
|
|
40
|
-
> {
|
|
41
|
-
if (parent == null) {
|
|
42
|
-
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
|
43
|
-
}
|
|
44
|
-
for (const key of getVisitorKeys(parent, visitorKeys)) {
|
|
45
|
-
if (
|
|
46
|
-
isNode(
|
|
47
|
-
// $FlowExpectedError[prop-missing]
|
|
48
|
-
parent[key],
|
|
49
|
-
)
|
|
50
|
-
) {
|
|
51
|
-
// $FlowFixMe[invalid-compare]
|
|
52
|
-
if (parent[key] === target) {
|
|
53
|
-
return {type: 'single', node: parent, key};
|
|
54
|
-
}
|
|
55
|
-
} else if (Array.isArray(parent[key])) {
|
|
56
|
-
for (let i = 0; i < parent[key].length; i += 1) {
|
|
57
|
-
// $FlowExpectedError[invalid-tuple-index]
|
|
58
|
-
const current = parent[key][i];
|
|
59
|
-
if (current === target) {
|
|
60
|
-
return {type: 'array', node: parent, key, targetIndex: i};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// this shouldn't happen ever
|
|
67
|
-
throw new Error(
|
|
68
|
-
`Expected to find the ${target.type} as a direct child of the ${parent.type}.`,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Replace a node with a new node within an AST (via the parent pointer).
|
|
74
|
-
*/
|
|
75
|
-
export function replaceNodeOnParent(
|
|
76
|
-
originalNode: ESNode,
|
|
77
|
-
originalNodeParent: ESNode,
|
|
78
|
-
nodeToReplaceWith: ESNode | $ReadOnlyArray<ESNode>,
|
|
79
|
-
visitorKeys?: ?VisitorKeysType,
|
|
80
|
-
): void {
|
|
81
|
-
const replacementParent = getParentKey(
|
|
82
|
-
originalNode,
|
|
83
|
-
originalNodeParent,
|
|
84
|
-
visitorKeys,
|
|
85
|
-
);
|
|
86
|
-
const parent = replacementParent.node;
|
|
87
|
-
if (replacementParent.type === 'array') {
|
|
88
|
-
// $FlowExpectedError[prop-missing]
|
|
89
|
-
parent[replacementParent.key] = replaceInArray(
|
|
90
|
-
// $FlowExpectedError[prop-missing]
|
|
91
|
-
parent[replacementParent.key],
|
|
92
|
-
replacementParent.targetIndex,
|
|
93
|
-
Array.isArray(nodeToReplaceWith)
|
|
94
|
-
? nodeToReplaceWith
|
|
95
|
-
: [nodeToReplaceWith],
|
|
96
|
-
);
|
|
97
|
-
} else {
|
|
98
|
-
if (Array.isArray(nodeToReplaceWith)) {
|
|
99
|
-
throw new Error(
|
|
100
|
-
`Cannot insert array into non-array parent type: ${parent.type}`,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
// $FlowExpectedError[prop-missing]
|
|
104
|
-
parent[replacementParent.key] = nodeToReplaceWith;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Remove a node from the AST its connected to (via the parent pointer).
|
|
110
|
-
*/
|
|
111
|
-
export function removeNodeOnParent(
|
|
112
|
-
originalNode: ESNode,
|
|
113
|
-
originalNodeParent: ESNode,
|
|
114
|
-
visitorKeys?: ?VisitorKeysType,
|
|
115
|
-
): void {
|
|
116
|
-
const replacementParent = getParentKey(
|
|
117
|
-
originalNode,
|
|
118
|
-
originalNodeParent,
|
|
119
|
-
visitorKeys,
|
|
120
|
-
);
|
|
121
|
-
const parent = replacementParent.node;
|
|
122
|
-
if (replacementParent.type === 'array') {
|
|
123
|
-
// $FlowExpectedError[prop-missing]
|
|
124
|
-
parent[replacementParent.key] = removeFromArray(
|
|
125
|
-
// $FlowExpectedError[prop-missing]
|
|
126
|
-
parent[replacementParent.key],
|
|
127
|
-
replacementParent.targetIndex,
|
|
128
|
-
);
|
|
129
|
-
} else {
|
|
130
|
-
// $FlowExpectedError[prop-missing]
|
|
131
|
-
parent[replacementParent.key] = null;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Corrects the parent pointers in direct children of the given node.
|
|
137
|
-
*/
|
|
138
|
-
export function setParentPointersInDirectChildren(
|
|
139
|
-
node: ESNode,
|
|
140
|
-
visitorKeys?: ?VisitorKeysType,
|
|
141
|
-
): void {
|
|
142
|
-
for (const key: $FlowFixMe of getVisitorKeys(node, visitorKeys)) {
|
|
143
|
-
if (isNode(node[key])) {
|
|
144
|
-
// $FlowExpectedError[cannot-write]
|
|
145
|
-
node[key].parent = node;
|
|
146
|
-
} else if (Array.isArray(node[key])) {
|
|
147
|
-
for (const child of node[key]) {
|
|
148
|
-
child.parent = node;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
|
156
|
-
*/
|
|
157
|
-
export function updateAllParentPointers(
|
|
158
|
-
node: ESNode,
|
|
159
|
-
visitorKeys?: ?VisitorKeysType,
|
|
160
|
-
) {
|
|
161
|
-
SimpleTraverser.traverse(node, {
|
|
162
|
-
enter(node, parent) {
|
|
163
|
-
// $FlowExpectedError[cannot-write]
|
|
164
|
-
node.parent = parent;
|
|
165
|
-
},
|
|
166
|
-
leave() {},
|
|
167
|
-
visitorKeys,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Clone node and add new props.
|
|
173
|
-
*
|
|
174
|
-
* This will only create a new object if the overrides actually result in a change.
|
|
175
|
-
*/
|
|
176
|
-
export function nodeWith<T: ESNode>(
|
|
177
|
-
node: T,
|
|
178
|
-
overrideProps: Partial<T>,
|
|
179
|
-
visitorKeys?: ?VisitorKeysType,
|
|
180
|
-
): T {
|
|
181
|
-
// Check if this will actually result in a change, maintaining referential equality is important.
|
|
182
|
-
const willBeUnchanged = Object.entries(overrideProps).every(
|
|
183
|
-
([key, value]) => {
|
|
184
|
-
const node_: $FlowFixMe = node;
|
|
185
|
-
if (Array.isArray(value)) {
|
|
186
|
-
return Array.isArray(node_[key])
|
|
187
|
-
? arrayIsEqual(node_[key], value)
|
|
188
|
-
: false;
|
|
189
|
-
}
|
|
190
|
-
return node_[key] === value;
|
|
191
|
-
},
|
|
192
|
-
);
|
|
193
|
-
if (willBeUnchanged) {
|
|
194
|
-
return node;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// Create new node.
|
|
198
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
199
|
-
const newNode: T = {
|
|
200
|
-
...node,
|
|
201
|
-
...overrideProps,
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
// Ensure parent pointers are correctly set within this nodes children.
|
|
205
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
206
|
-
|
|
207
|
-
return newNode;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Shallow clones node, providing a new reference for an existing node.
|
|
212
|
-
*/
|
|
213
|
-
export function shallowCloneNode<T: ESNode>(
|
|
214
|
-
node: T,
|
|
215
|
-
visitorKeys?: ?VisitorKeysType,
|
|
216
|
-
): T {
|
|
217
|
-
// $FlowExpectedError[cannot-spread-interface]
|
|
218
|
-
const newNode: T = {...node};
|
|
219
|
-
|
|
220
|
-
// Ensure parent pointers are correctly set within this nodes children.
|
|
221
|
-
setParentPointersInDirectChildren(newNode, visitorKeys);
|
|
222
|
-
|
|
223
|
-
return newNode;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Deeply clones node and its entire tree.
|
|
228
|
-
*/
|
|
229
|
-
export function deepCloneNode<T: ESNode>(
|
|
230
|
-
node: T,
|
|
231
|
-
visitorKeys?: ?VisitorKeysType,
|
|
232
|
-
): T {
|
|
233
|
-
const clone: T = JSON.parse(
|
|
234
|
-
JSON.stringify(node, (key, value) => {
|
|
235
|
-
// null out parent pointers
|
|
236
|
-
if (key === 'parent') {
|
|
237
|
-
return undefined;
|
|
238
|
-
}
|
|
239
|
-
return value;
|
|
240
|
-
}),
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
updateAllParentPointers(clone, visitorKeys);
|
|
244
|
-
|
|
245
|
-
return clone;
|
|
246
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
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
|
-
*
|
|
8
|
-
* @format
|
|
9
|
-
*/
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "__esModule", {
|
|
13
|
-
value: true
|
|
14
|
-
});
|
|
15
|
-
exports.SimpleTraverserSkip = exports.SimpleTraverserBreak = exports.SimpleTraverser = void 0;
|
|
16
|
-
|
|
17
|
-
var _getVisitorKeys = require("./getVisitorKeys");
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Can be thrown within the traversal "enter" function to prevent the traverser
|
|
21
|
-
* from traversing the node any further, essentially culling the remainder of the
|
|
22
|
-
* AST branch
|
|
23
|
-
*/
|
|
24
|
-
const SimpleTraverserSkip = new Error();
|
|
25
|
-
/**
|
|
26
|
-
* Can be thrown at any point during the traversal to immediately stop traversal
|
|
27
|
-
* entirely.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
exports.SimpleTraverserSkip = SimpleTraverserSkip;
|
|
31
|
-
const SimpleTraverserBreak = new Error();
|
|
32
|
-
/**
|
|
33
|
-
* A very simple traverser class to traverse AST trees.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
exports.SimpleTraverserBreak = SimpleTraverserBreak;
|
|
37
|
-
|
|
38
|
-
class SimpleTraverser {
|
|
39
|
-
/**
|
|
40
|
-
* Traverse the given AST tree.
|
|
41
|
-
* @param node The root node to traverse.
|
|
42
|
-
* @param options The option object.
|
|
43
|
-
*/
|
|
44
|
-
traverse(node, options) {
|
|
45
|
-
try {
|
|
46
|
-
this._traverse(node, null, options);
|
|
47
|
-
} catch (ex) {
|
|
48
|
-
if (ex === SimpleTraverserBreak) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
throw ex;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Traverse the given AST tree recursively.
|
|
57
|
-
* @param node The current node.
|
|
58
|
-
* @param parent The parent node.
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
_traverse(node, parent, options) {
|
|
64
|
-
if (!(0, _getVisitorKeys.isNode)(node)) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
options.enter(node, parent);
|
|
70
|
-
} catch (ex) {
|
|
71
|
-
if (ex === SimpleTraverserSkip) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this._setErrorContext(ex, node);
|
|
76
|
-
|
|
77
|
-
throw ex;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const keys = (0, _getVisitorKeys.getVisitorKeys)(node, options.visitorKeys);
|
|
81
|
-
|
|
82
|
-
for (const key of keys) {
|
|
83
|
-
const child = node[key];
|
|
84
|
-
|
|
85
|
-
if (Array.isArray(child)) {
|
|
86
|
-
for (let j = 0; j < child.length; ++j) {
|
|
87
|
-
this._traverse(child[j], node, options);
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
this._traverse(child, node, options);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
options.leave(node, parent);
|
|
96
|
-
} catch (ex) {
|
|
97
|
-
if (ex === SimpleTraverserSkip) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
this._setErrorContext(ex, node);
|
|
102
|
-
|
|
103
|
-
throw ex;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Set useful contextual information onto the error object.
|
|
108
|
-
* @param ex The error object.
|
|
109
|
-
* @param node The current node.
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
_setErrorContext(ex, node) {
|
|
115
|
-
// $FlowFixMe[prop-missing]
|
|
116
|
-
ex.currentNode = {
|
|
117
|
-
type: node.type,
|
|
118
|
-
range: node.range,
|
|
119
|
-
loc: node.loc
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Traverse the given AST tree.
|
|
124
|
-
* @param node The root node to traverse.
|
|
125
|
-
* @param options The option object.
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
static traverse(node, options) {
|
|
130
|
-
new SimpleTraverser().traverse(node, options);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
exports.SimpleTraverser = SimpleTraverser;
|
|
136
|
-
SimpleTraverser.Break = SimpleTraverserBreak;
|
|
137
|
-
SimpleTraverser.Skip = SimpleTraverserSkip;
|