hermes-parser 0.9.0 → 0.10.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/HermesASTAdapter.js +17 -13
- package/dist/HermesASTAdapter.js.flow +22 -11
- package/dist/HermesParserNodeDeserializers.js +29 -4
- package/dist/HermesParserWASM.js +1 -1
- package/dist/HermesToBabelAdapter.js +34 -11
- package/dist/HermesToBabelAdapter.js.flow +30 -12
- package/dist/HermesToESTreeAdapter.js +7 -21
- package/dist/HermesToESTreeAdapter.js.flow +9 -24
- package/dist/generated/ESTreeVisitorKeys.js +180 -0
- package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
- package/dist/generated/{visitor-keys.js → ParserVisitorKeys.js} +11 -2
- package/dist/getModuleDocblock.js +2 -1
- package/dist/getModuleDocblock.js.flow +3 -1
- package/dist/index.js +46 -0
- package/dist/index.js.flow +8 -1
- package/dist/transform/SimpleTransform.js +92 -0
- package/dist/transform/SimpleTransform.js.flow +104 -0
- package/dist/transform/astArrayMutationHelpers.js +62 -0
- package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
- package/dist/transform/astNodeMutationHelpers.js +186 -0
- package/dist/transform/astNodeMutationHelpers.js.flow +205 -0
- package/dist/traverse/SimpleTraverser.js +138 -0
- package/dist/traverse/SimpleTraverser.js.flow +132 -0
- package/dist/traverse/getVisitorKeys.js +35 -0
- package/dist/traverse/getVisitorKeys.js.flow +36 -0
- package/package.json +3 -4
- /package/dist/generated/{visitor-keys.js.flow → ParserVisitorKeys.js.flow} +0 -0
|
@@ -0,0 +1,92 @@
|
|
|
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.SimpleTransform = void 0;
|
|
16
|
+
|
|
17
|
+
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
|
18
|
+
|
|
19
|
+
var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A simple class to recursively tranform AST trees.
|
|
23
|
+
*/
|
|
24
|
+
class SimpleTransform {
|
|
25
|
+
/**
|
|
26
|
+
* Transform the given AST tree.
|
|
27
|
+
* @param rootNode The root node to traverse.
|
|
28
|
+
* @param options The option object.
|
|
29
|
+
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
|
30
|
+
*/
|
|
31
|
+
transform(rootNode, options) {
|
|
32
|
+
let resultRootNode = rootNode;
|
|
33
|
+
|
|
34
|
+
_SimpleTraverser.SimpleTraverser.traverse(rootNode, {
|
|
35
|
+
enter: (node, parent) => {
|
|
36
|
+
const resultNode = options.transform(node);
|
|
37
|
+
|
|
38
|
+
if (resultNode !== node) {
|
|
39
|
+
const traversedResultNode = resultNode != null ? this.transform(resultNode, options) : null;
|
|
40
|
+
|
|
41
|
+
if (parent == null) {
|
|
42
|
+
if (node !== rootNode) {
|
|
43
|
+
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
resultRootNode = traversedResultNode;
|
|
47
|
+
} else if (traversedResultNode == null) {
|
|
48
|
+
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent);
|
|
49
|
+
} else {
|
|
50
|
+
(0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
throw _SimpleTraverser.SimpleTraverser.Skip;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
leave(_node) {}
|
|
58
|
+
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return resultRootNode;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Transform the given AST tree.
|
|
65
|
+
* @param node The root node to traverse.
|
|
66
|
+
* @param options The option object.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
static transform(node, options) {
|
|
71
|
+
return new SimpleTransform().transform(node, options);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Return a new AST node with the given properties overrided if needed.
|
|
75
|
+
*
|
|
76
|
+
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
|
77
|
+
* is important as its used to know if a node should be re-traversed.
|
|
78
|
+
*
|
|
79
|
+
* @param node The base AST node.
|
|
80
|
+
* @param overrideProps New properties to apply to the node.
|
|
81
|
+
* @return Either the orginal node if the properties matched the existing node or a new node with
|
|
82
|
+
* the new properties.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
static nodeWith(node, overrideProps) {
|
|
87
|
+
return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
exports.SimpleTransform = SimpleTransform;
|
|
@@ -0,0 +1,104 @@
|
|
|
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 {VisitorKeysType} from '../traverse/getVisitorKeys';
|
|
14
|
+
import type {ESNode} from 'hermes-estree';
|
|
15
|
+
|
|
16
|
+
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
|
17
|
+
import {
|
|
18
|
+
nodeWith,
|
|
19
|
+
removeNodeOnParent,
|
|
20
|
+
replaceNodeOnParent,
|
|
21
|
+
} from './astNodeMutationHelpers';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Transform callback
|
|
25
|
+
* @param node The node we are visiting
|
|
26
|
+
* @returns
|
|
27
|
+
* - return input node, signals no changes were made will continue to the next node.
|
|
28
|
+
* - return new node, the old node will be replaced in the AST. The new node and its
|
|
29
|
+
* children are then traversed.
|
|
30
|
+
* - return null, signals the node should be deleted from the AST.
|
|
31
|
+
*/
|
|
32
|
+
export type TransformCallback = (node: ESNode) => ESNode | null;
|
|
33
|
+
|
|
34
|
+
export type TransformOptions = $ReadOnly<{
|
|
35
|
+
/** The callback function which is called on entering each node. */
|
|
36
|
+
transform: TransformCallback,
|
|
37
|
+
|
|
38
|
+
/** The set of visitor keys to use for traversal. Defaults to the Flow ESTree visitor keys */
|
|
39
|
+
visitorKeys?: ?VisitorKeysType,
|
|
40
|
+
}>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A simple class to recursively tranform AST trees.
|
|
44
|
+
*/
|
|
45
|
+
export class SimpleTransform {
|
|
46
|
+
/**
|
|
47
|
+
* Transform the given AST tree.
|
|
48
|
+
* @param rootNode The root node to traverse.
|
|
49
|
+
* @param options The option object.
|
|
50
|
+
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
|
51
|
+
*/
|
|
52
|
+
transform(rootNode: ESNode, options: TransformOptions): ESNode | null {
|
|
53
|
+
let resultRootNode: ESNode | null = rootNode;
|
|
54
|
+
SimpleTraverser.traverse(rootNode, {
|
|
55
|
+
enter: (node: ESNode, parent: ?ESNode) => {
|
|
56
|
+
const resultNode = options.transform(node);
|
|
57
|
+
if (resultNode !== node) {
|
|
58
|
+
const traversedResultNode =
|
|
59
|
+
resultNode != null ? this.transform(resultNode, options) : null;
|
|
60
|
+
if (parent == null) {
|
|
61
|
+
if (node !== rootNode) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
resultRootNode = traversedResultNode;
|
|
67
|
+
} else if (traversedResultNode == null) {
|
|
68
|
+
removeNodeOnParent(node, parent);
|
|
69
|
+
} else {
|
|
70
|
+
replaceNodeOnParent(node, parent, traversedResultNode);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw SimpleTraverser.Skip;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
leave(_node: ESNode) {},
|
|
77
|
+
});
|
|
78
|
+
return resultRootNode;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Transform the given AST tree.
|
|
83
|
+
* @param node The root node to traverse.
|
|
84
|
+
* @param options The option object.
|
|
85
|
+
*/
|
|
86
|
+
static transform(node: ESNode, options: TransformOptions): ESNode | null {
|
|
87
|
+
return new SimpleTransform().transform(node, options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Return a new AST node with the given properties overrided if needed.
|
|
92
|
+
*
|
|
93
|
+
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
|
94
|
+
* is important as its used to know if a node should be re-traversed.
|
|
95
|
+
*
|
|
96
|
+
* @param node The base AST node.
|
|
97
|
+
* @param overrideProps New properties to apply to the node.
|
|
98
|
+
* @return Either the orginal node if the properties matched the existing node or a new node with
|
|
99
|
+
* the new properties.
|
|
100
|
+
*/
|
|
101
|
+
static nodeWith<T: ESNode>(node: T, overrideProps: Partial<T>): T {
|
|
102
|
+
return nodeWith<T>(node, overrideProps);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
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) {
|
|
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)) {
|
|
35
|
+
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
|
+
parent[key])) {
|
|
37
|
+
if (parent[key] === target) {
|
|
38
|
+
return {
|
|
39
|
+
type: 'single',
|
|
40
|
+
node: parent,
|
|
41
|
+
key
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
} else if (Array.isArray(parent[key])) {
|
|
45
|
+
for (let i = 0; i < parent[key].length; i += 1) {
|
|
46
|
+
const current = parent[key][i];
|
|
47
|
+
|
|
48
|
+
if (current === target) {
|
|
49
|
+
return {
|
|
50
|
+
type: 'array',
|
|
51
|
+
node: parent,
|
|
52
|
+
key,
|
|
53
|
+
targetIndex: i
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} // this shouldn't happen ever
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Replace a node with a new node within an AST (via the parent pointer).
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith) {
|
|
69
|
+
const replacementParent = getParentKey(originalNode, originalNodeParent);
|
|
70
|
+
const parent = replacementParent.node;
|
|
71
|
+
|
|
72
|
+
if (replacementParent.type === 'array') {
|
|
73
|
+
// $FlowExpectedError[prop-missing]
|
|
74
|
+
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
75
|
+
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
|
76
|
+
} else {
|
|
77
|
+
// $FlowExpectedError[prop-missing]
|
|
78
|
+
parent[replacementParent.key] = nodeToReplaceWith;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Remove a node from the AST its connected to (via the parent pointer).
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
function removeNodeOnParent(originalNode, originalNodeParent) {
|
|
87
|
+
const replacementParent = getParentKey(originalNode, originalNodeParent);
|
|
88
|
+
const parent = replacementParent.node;
|
|
89
|
+
|
|
90
|
+
if (replacementParent.type === 'array') {
|
|
91
|
+
// $FlowExpectedError[prop-missing]
|
|
92
|
+
parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)( // $FlowExpectedError[prop-missing]
|
|
93
|
+
parent[replacementParent.key], replacementParent.targetIndex);
|
|
94
|
+
} else {
|
|
95
|
+
// $FlowExpectedError[prop-missing]
|
|
96
|
+
parent[replacementParent.key] = null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Corrects the parent pointers in direct children of the given node.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
function setParentPointersInDirectChildren(node) {
|
|
105
|
+
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node)) {
|
|
106
|
+
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
107
|
+
node[key])) {
|
|
108
|
+
node[key].parent = node;
|
|
109
|
+
} else if (Array.isArray(node[key])) {
|
|
110
|
+
for (const child of node[key]) {
|
|
111
|
+
child.parent = node;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
function updateAllParentPointers(node) {
|
|
122
|
+
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
|
123
|
+
enter(node, parent) {
|
|
124
|
+
// $FlowExpectedError[cannot-write]
|
|
125
|
+
node.parent = parent;
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
leave() {}
|
|
129
|
+
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Clone node and add new props.
|
|
134
|
+
*
|
|
135
|
+
* This will only create a new object if the overrides actually result in a change.
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
function nodeWith(node, overrideProps) {
|
|
140
|
+
// Check if this will actually result in a change, maintaining referential equality is important.
|
|
141
|
+
const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => // $FlowExpectedError[prop-missing]
|
|
142
|
+
Array.isArray(value) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node[key], value) : node[key] === value);
|
|
143
|
+
|
|
144
|
+
if (willBeUnchanged) {
|
|
145
|
+
return node;
|
|
146
|
+
} // Create new node.
|
|
147
|
+
// $FlowExpectedError[cannot-spread-interface]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
const newNode = { ...node,
|
|
151
|
+
...overrideProps
|
|
152
|
+
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
153
|
+
|
|
154
|
+
setParentPointersInDirectChildren(newNode);
|
|
155
|
+
return newNode;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Shallow clones node, providing a new reference for an existing node.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
function shallowCloneNode(node) {
|
|
163
|
+
// $FlowExpectedError[cannot-spread-interface]
|
|
164
|
+
const newNode = { ...node
|
|
165
|
+
}; // Ensure parent pointers are correctly set within this nodes children.
|
|
166
|
+
|
|
167
|
+
setParentPointersInDirectChildren(newNode);
|
|
168
|
+
return newNode;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Deeply clones node and its entire tree.
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
function deepCloneNode(node) {
|
|
176
|
+
const clone = JSON.parse(JSON.stringify(node, (key, value) => {
|
|
177
|
+
// null out parent pointers
|
|
178
|
+
if (key === 'parent') {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return value;
|
|
183
|
+
}));
|
|
184
|
+
updateAllParentPointers(clone);
|
|
185
|
+
return clone;
|
|
186
|
+
}
|