mirror of
https://github.com/hex248/ob248.com.git
synced 2026-02-08 02:33:02 +00:00
merge new into master
This commit is contained in:
120
node_modules/hermes-parser/dist/transform/SimpleTransform.js
generated
vendored
Normal file
120
node_modules/hermes-parser/dist/transform/SimpleTransform.js
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.SimpleTransform = void 0;
|
||||
|
||||
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
||||
|
||||
var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
||||
|
||||
function setParentPointer(node, parent) {
|
||||
if (parent != null) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node.parent = parent;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A simple class to recursively tranform AST trees.
|
||||
*/
|
||||
|
||||
|
||||
class SimpleTransform {
|
||||
/**
|
||||
* Transform the given AST tree.
|
||||
* @param rootNode The root node to traverse.
|
||||
* @param options The option object.
|
||||
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
||||
*/
|
||||
transform(rootNode, options) {
|
||||
let resultRootNode = rootNode;
|
||||
|
||||
_SimpleTraverser.SimpleTraverser.traverse(rootNode, {
|
||||
enter: (node, parent) => {
|
||||
// Ensure the parent pointers are correctly set before entering the node.
|
||||
setParentPointer(node, parent);
|
||||
const resultNode = options.transform(node);
|
||||
|
||||
if (resultNode !== node) {
|
||||
let traversedResultNode = null;
|
||||
|
||||
if (resultNode != null) {
|
||||
// Ensure the new node has the correct parent pointers before recursing again.
|
||||
setParentPointer(resultNode, parent);
|
||||
traversedResultNode = this.transform(resultNode, options);
|
||||
}
|
||||
|
||||
if (parent == null) {
|
||||
if (node !== rootNode) {
|
||||
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
||||
}
|
||||
|
||||
resultRootNode = traversedResultNode;
|
||||
} else if (traversedResultNode == null) {
|
||||
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
||||
} else {
|
||||
(0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode, options.visitorKeys);
|
||||
setParentPointer(traversedResultNode, parent);
|
||||
}
|
||||
|
||||
throw _SimpleTraverser.SimpleTraverser.Skip;
|
||||
}
|
||||
},
|
||||
|
||||
leave(_node) {},
|
||||
|
||||
visitorKeys: options.visitorKeys
|
||||
});
|
||||
|
||||
return resultRootNode;
|
||||
}
|
||||
/**
|
||||
* Transform the given AST tree.
|
||||
* @param node The root node to traverse.
|
||||
* @param options The option object.
|
||||
*/
|
||||
|
||||
|
||||
static transform(node, options) {
|
||||
return new SimpleTransform().transform(node, options);
|
||||
}
|
||||
|
||||
static transformProgram(program, options) {
|
||||
const result = SimpleTransform.transform(program, options);
|
||||
|
||||
if ((result == null ? void 0 : result.type) === 'Program') {
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new Error('SimpleTransform.transformProgram: Expected program node.');
|
||||
}
|
||||
/**
|
||||
* Return a new AST node with the given properties overrided if needed.
|
||||
*
|
||||
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
||||
* is important as its used to know if a node should be re-traversed.
|
||||
*
|
||||
* @param node The base AST node.
|
||||
* @param overrideProps New properties to apply to the node.
|
||||
* @return Either the orginal node if the properties matched the existing node or a new node with
|
||||
* the new properties.
|
||||
*/
|
||||
|
||||
|
||||
static nodeWith(node, overrideProps, visitorKeys) {
|
||||
return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps, visitorKeys);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
exports.SimpleTransform = SimpleTransform;
|
||||
143
node_modules/hermes-parser/dist/transform/SimpleTransform.js.flow
generated
vendored
Normal file
143
node_modules/hermes-parser/dist/transform/SimpleTransform.js.flow
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {VisitorKeysType} from '../traverse/getVisitorKeys';
|
||||
import type {ESNode, Program} from 'hermes-estree';
|
||||
|
||||
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
||||
import {
|
||||
nodeWith,
|
||||
removeNodeOnParent,
|
||||
replaceNodeOnParent,
|
||||
} from './astNodeMutationHelpers';
|
||||
|
||||
/**
|
||||
* Transform callback
|
||||
* @param node The node we are visiting
|
||||
* @returns
|
||||
* - return input node, signals no changes were made will continue to the next node.
|
||||
* - return new node, the old node will be replaced in the AST. The new node and its
|
||||
* children are then traversed.
|
||||
* - return null, signals the node should be deleted from the AST.
|
||||
*/
|
||||
export type TransformCallback = (node: ESNode) => ESNode | null;
|
||||
|
||||
export type TransformOptions = $ReadOnly<{
|
||||
/** The callback function which is called on entering each node. */
|
||||
transform: TransformCallback,
|
||||
|
||||
/** The set of visitor keys to use for traversal. Defaults to the Flow ESTree visitor keys */
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
}>;
|
||||
|
||||
function setParentPointer(node: ESNode, parent: ?ESNode): void {
|
||||
if (parent != null) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node.parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple class to recursively tranform AST trees.
|
||||
*/
|
||||
export class SimpleTransform {
|
||||
/**
|
||||
* Transform the given AST tree.
|
||||
* @param rootNode The root node to traverse.
|
||||
* @param options The option object.
|
||||
* @return The modified rootNode or a new node if the rootNode was transformed directly.
|
||||
*/
|
||||
transform(rootNode: ESNode, options: TransformOptions): ESNode | null {
|
||||
let resultRootNode: ESNode | null = rootNode;
|
||||
SimpleTraverser.traverse(rootNode, {
|
||||
enter: (node: ESNode, parent: ?ESNode) => {
|
||||
// Ensure the parent pointers are correctly set before entering the node.
|
||||
setParentPointer(node, parent);
|
||||
|
||||
const resultNode = options.transform(node);
|
||||
if (resultNode !== node) {
|
||||
let traversedResultNode = null;
|
||||
|
||||
if (resultNode != null) {
|
||||
// Ensure the new node has the correct parent pointers before recursing again.
|
||||
setParentPointer(resultNode, parent);
|
||||
|
||||
traversedResultNode = this.transform(resultNode, options);
|
||||
}
|
||||
|
||||
if (parent == null) {
|
||||
if (node !== rootNode) {
|
||||
throw new Error(
|
||||
'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
|
||||
);
|
||||
}
|
||||
resultRootNode = traversedResultNode;
|
||||
} else if (traversedResultNode == null) {
|
||||
removeNodeOnParent(node, parent, options.visitorKeys);
|
||||
} else {
|
||||
replaceNodeOnParent(
|
||||
node,
|
||||
parent,
|
||||
traversedResultNode,
|
||||
options.visitorKeys,
|
||||
);
|
||||
setParentPointer(traversedResultNode, parent);
|
||||
}
|
||||
|
||||
throw SimpleTraverser.Skip;
|
||||
}
|
||||
},
|
||||
leave(_node: ESNode) {},
|
||||
visitorKeys: options.visitorKeys,
|
||||
});
|
||||
return resultRootNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the given AST tree.
|
||||
* @param node The root node to traverse.
|
||||
* @param options The option object.
|
||||
*/
|
||||
static transform(node: ESNode, options: TransformOptions): ESNode | null {
|
||||
return new SimpleTransform().transform(node, options);
|
||||
}
|
||||
|
||||
static transformProgram(
|
||||
program: Program,
|
||||
options: TransformOptions,
|
||||
): Program {
|
||||
const result = SimpleTransform.transform(program, options);
|
||||
if (result?.type === 'Program') {
|
||||
return result;
|
||||
}
|
||||
throw new Error('SimpleTransform.transformProgram: Expected program node.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new AST node with the given properties overrided if needed.
|
||||
*
|
||||
* This function takes care to only create new nodes when needed. Referential equality of nodes
|
||||
* is important as its used to know if a node should be re-traversed.
|
||||
*
|
||||
* @param node The base AST node.
|
||||
* @param overrideProps New properties to apply to the node.
|
||||
* @return Either the orginal node if the properties matched the existing node or a new node with
|
||||
* the new properties.
|
||||
*/
|
||||
static nodeWith<T: ESNode>(
|
||||
node: T,
|
||||
overrideProps: Partial<T>,
|
||||
visitorKeys?: VisitorKeysType,
|
||||
): T {
|
||||
return nodeWith<T>(node, overrideProps, visitorKeys);
|
||||
}
|
||||
}
|
||||
62
node_modules/hermes-parser/dist/transform/astArrayMutationHelpers.js
generated
vendored
Normal file
62
node_modules/hermes-parser/dist/transform/astArrayMutationHelpers.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.arrayIsEqual = arrayIsEqual;
|
||||
exports.insertInArray = insertInArray;
|
||||
exports.removeFromArray = removeFromArray;
|
||||
exports.replaceInArray = replaceInArray;
|
||||
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
function assertArrayBounds(array, index) {
|
||||
if (index < 0 || index >= array.length) {
|
||||
throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
|
||||
}
|
||||
}
|
||||
|
||||
function arrayIsEqual(a1, a2) {
|
||||
if (a1 === a2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (a1.length !== a2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < a1.length; i++) {
|
||||
if (a1[i] !== a2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function insertInArray(array, index, elements) {
|
||||
if (index === array.length) {
|
||||
// Support the insert at end of array case.
|
||||
return array.concat(elements);
|
||||
}
|
||||
|
||||
assertArrayBounds(array, index);
|
||||
return array.slice(0, index).concat(elements).concat(array.slice(index));
|
||||
}
|
||||
|
||||
function removeFromArray(array, index) {
|
||||
assertArrayBounds(array, index);
|
||||
return [...array.slice(0, index), ...array.slice(index + 1)];
|
||||
}
|
||||
|
||||
function replaceInArray(array, index, elements) {
|
||||
assertArrayBounds(array, index);
|
||||
return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
|
||||
}
|
||||
71
node_modules/hermes-parser/dist/transform/astArrayMutationHelpers.js.flow
generated
vendored
Normal file
71
node_modules/hermes-parser/dist/transform/astArrayMutationHelpers.js.flow
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
function assertArrayBounds<T>(array: $ReadOnlyArray<T>, index: number): void {
|
||||
if (index < 0 || index >= array.length) {
|
||||
throw new Error(
|
||||
`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function arrayIsEqual(
|
||||
a1: $ReadOnlyArray<mixed>,
|
||||
a2: $ReadOnlyArray<mixed>,
|
||||
): boolean {
|
||||
if (a1 === a2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (a1.length !== a2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < a1.length; i++) {
|
||||
if (a1[i] !== a2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function insertInArray<T>(
|
||||
array: $ReadOnlyArray<T>,
|
||||
index: number,
|
||||
elements: $ReadOnlyArray<T>,
|
||||
): Array<T> {
|
||||
if (index === array.length) {
|
||||
// Support the insert at end of array case.
|
||||
return array.concat(elements);
|
||||
}
|
||||
assertArrayBounds(array, index);
|
||||
return array.slice(0, index).concat(elements).concat(array.slice(index));
|
||||
}
|
||||
|
||||
export function removeFromArray<T>(
|
||||
array: $ReadOnlyArray<T>,
|
||||
index: number,
|
||||
): Array<T> {
|
||||
assertArrayBounds(array, index);
|
||||
return [...array.slice(0, index), ...array.slice(index + 1)];
|
||||
}
|
||||
|
||||
export function replaceInArray<T>(
|
||||
array: $ReadOnlyArray<T>,
|
||||
index: number,
|
||||
elements: $ReadOnlyArray<T>,
|
||||
): Array<T> {
|
||||
assertArrayBounds(array, index);
|
||||
return array
|
||||
.slice(0, index)
|
||||
.concat(elements)
|
||||
.concat(array.slice(index + 1));
|
||||
}
|
||||
195
node_modules/hermes-parser/dist/transform/astNodeMutationHelpers.js
generated
vendored
Normal file
195
node_modules/hermes-parser/dist/transform/astNodeMutationHelpers.js
generated
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.deepCloneNode = deepCloneNode;
|
||||
exports.nodeWith = nodeWith;
|
||||
exports.removeNodeOnParent = removeNodeOnParent;
|
||||
exports.replaceNodeOnParent = replaceNodeOnParent;
|
||||
exports.setParentPointersInDirectChildren = setParentPointersInDirectChildren;
|
||||
exports.shallowCloneNode = shallowCloneNode;
|
||||
exports.updateAllParentPointers = updateAllParentPointers;
|
||||
|
||||
var _astArrayMutationHelpers = require("./astArrayMutationHelpers");
|
||||
|
||||
var _getVisitorKeys = require("../traverse/getVisitorKeys");
|
||||
|
||||
var _SimpleTraverser = require("../traverse/SimpleTraverser");
|
||||
|
||||
function getParentKey(target, parent, visitorKeys) {
|
||||
if (parent == null) {
|
||||
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
||||
}
|
||||
|
||||
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
||||
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
||||
parent[key])) {
|
||||
if (parent[key] === target) {
|
||||
return {
|
||||
type: 'single',
|
||||
node: parent,
|
||||
key
|
||||
};
|
||||
}
|
||||
} else if (Array.isArray(parent[key])) {
|
||||
for (let i = 0; i < parent[key].length; i += 1) {
|
||||
// $FlowExpectedError[invalid-tuple-index]
|
||||
const current = parent[key][i];
|
||||
|
||||
if (current === target) {
|
||||
return {
|
||||
type: 'array',
|
||||
node: parent,
|
||||
key,
|
||||
targetIndex: i
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
} // this shouldn't happen ever
|
||||
|
||||
|
||||
throw new Error(`Expected to find the ${target.type} as a direct child of the ${parent.type}.`);
|
||||
}
|
||||
/**
|
||||
* Replace a node with a new node within an AST (via the parent pointer).
|
||||
*/
|
||||
|
||||
|
||||
function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith, visitorKeys) {
|
||||
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
||||
const parent = replacementParent.node;
|
||||
|
||||
if (replacementParent.type === 'array') {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
||||
} else {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = nodeToReplaceWith;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Remove a node from the AST its connected to (via the parent pointer).
|
||||
*/
|
||||
|
||||
|
||||
function removeNodeOnParent(originalNode, originalNodeParent, visitorKeys) {
|
||||
const replacementParent = getParentKey(originalNode, originalNodeParent, visitorKeys);
|
||||
const parent = replacementParent.node;
|
||||
|
||||
if (replacementParent.type === 'array') {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = (0, _astArrayMutationHelpers.removeFromArray)( // $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key], replacementParent.targetIndex);
|
||||
} else {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Corrects the parent pointers in direct children of the given node.
|
||||
*/
|
||||
|
||||
|
||||
function setParentPointersInDirectChildren(node, visitorKeys) {
|
||||
for (const key of (0, _getVisitorKeys.getVisitorKeys)(node, visitorKeys)) {
|
||||
if ((0, _getVisitorKeys.isNode)(node[key])) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node[key].parent = node;
|
||||
} else if (Array.isArray(node[key])) {
|
||||
for (const child of node[key]) {
|
||||
child.parent = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
||||
*/
|
||||
|
||||
|
||||
function updateAllParentPointers(node, visitorKeys) {
|
||||
_SimpleTraverser.SimpleTraverser.traverse(node, {
|
||||
enter(node, parent) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node.parent = parent;
|
||||
},
|
||||
|
||||
leave() {},
|
||||
|
||||
visitorKeys
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Clone node and add new props.
|
||||
*
|
||||
* This will only create a new object if the overrides actually result in a change.
|
||||
*/
|
||||
|
||||
|
||||
function nodeWith(node, overrideProps, visitorKeys) {
|
||||
// Check if this will actually result in a change, maintaining referential equality is important.
|
||||
const willBeUnchanged = Object.entries(overrideProps).every(([key, value]) => {
|
||||
const node_ = node;
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return Array.isArray(node_[key]) ? (0, _astArrayMutationHelpers.arrayIsEqual)(node_[key], value) : false;
|
||||
}
|
||||
|
||||
return node_[key] === value;
|
||||
});
|
||||
|
||||
if (willBeUnchanged) {
|
||||
return node;
|
||||
} // Create new node.
|
||||
// $FlowExpectedError[cannot-spread-interface]
|
||||
|
||||
|
||||
const newNode = { ...node,
|
||||
...overrideProps
|
||||
}; // Ensure parent pointers are correctly set within this nodes children.
|
||||
|
||||
setParentPointersInDirectChildren(newNode, visitorKeys);
|
||||
return newNode;
|
||||
}
|
||||
/**
|
||||
* Shallow clones node, providing a new reference for an existing node.
|
||||
*/
|
||||
|
||||
|
||||
function shallowCloneNode(node, visitorKeys) {
|
||||
// $FlowExpectedError[cannot-spread-interface]
|
||||
const newNode = { ...node
|
||||
}; // Ensure parent pointers are correctly set within this nodes children.
|
||||
|
||||
setParentPointersInDirectChildren(newNode, visitorKeys);
|
||||
return newNode;
|
||||
}
|
||||
/**
|
||||
* Deeply clones node and its entire tree.
|
||||
*/
|
||||
|
||||
|
||||
function deepCloneNode(node, visitorKeys) {
|
||||
const clone = JSON.parse(JSON.stringify(node, (key, value) => {
|
||||
// null out parent pointers
|
||||
if (key === 'parent') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return value;
|
||||
}));
|
||||
updateAllParentPointers(clone, visitorKeys);
|
||||
return clone;
|
||||
}
|
||||
238
node_modules/hermes-parser/dist/transform/astNodeMutationHelpers.js.flow
generated
vendored
Normal file
238
node_modules/hermes-parser/dist/transform/astNodeMutationHelpers.js.flow
generated
vendored
Normal file
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {ESNode} from 'hermes-estree';
|
||||
import type {VisitorKeysType} from '../traverse/getVisitorKeys';
|
||||
|
||||
import {
|
||||
arrayIsEqual,
|
||||
removeFromArray,
|
||||
replaceInArray,
|
||||
} from './astArrayMutationHelpers';
|
||||
import {getVisitorKeys, isNode} from '../traverse/getVisitorKeys';
|
||||
import {SimpleTraverser} from '../traverse/SimpleTraverser';
|
||||
|
||||
function getParentKey(
|
||||
target: ESNode,
|
||||
parent: ESNode,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): $ReadOnly<
|
||||
| {
|
||||
type: 'single',
|
||||
node: ESNode,
|
||||
key: string,
|
||||
}
|
||||
| {
|
||||
type: 'array',
|
||||
node: ESNode,
|
||||
key: string,
|
||||
targetIndex: number,
|
||||
},
|
||||
> {
|
||||
if (parent == null) {
|
||||
throw new Error(`Expected parent node to be set on "${target.type}"`);
|
||||
}
|
||||
for (const key of getVisitorKeys(parent, visitorKeys)) {
|
||||
if (
|
||||
isNode(
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[key],
|
||||
)
|
||||
) {
|
||||
if (parent[key] === target) {
|
||||
return {type: 'single', node: parent, key};
|
||||
}
|
||||
} else if (Array.isArray(parent[key])) {
|
||||
for (let i = 0; i < parent[key].length; i += 1) {
|
||||
// $FlowExpectedError[invalid-tuple-index]
|
||||
const current = parent[key][i];
|
||||
if (current === target) {
|
||||
return {type: 'array', node: parent, key, targetIndex: i};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this shouldn't happen ever
|
||||
throw new Error(
|
||||
`Expected to find the ${target.type} as a direct child of the ${parent.type}.`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a node with a new node within an AST (via the parent pointer).
|
||||
*/
|
||||
export function replaceNodeOnParent(
|
||||
originalNode: ESNode,
|
||||
originalNodeParent: ESNode,
|
||||
nodeToReplaceWith: ESNode,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): void {
|
||||
const replacementParent = getParentKey(
|
||||
originalNode,
|
||||
originalNodeParent,
|
||||
visitorKeys,
|
||||
);
|
||||
const parent = replacementParent.node;
|
||||
if (replacementParent.type === 'array') {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = replaceInArray(
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key],
|
||||
replacementParent.targetIndex,
|
||||
[nodeToReplaceWith],
|
||||
);
|
||||
} else {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = nodeToReplaceWith;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a node from the AST its connected to (via the parent pointer).
|
||||
*/
|
||||
export function removeNodeOnParent(
|
||||
originalNode: ESNode,
|
||||
originalNodeParent: ESNode,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): void {
|
||||
const replacementParent = getParentKey(
|
||||
originalNode,
|
||||
originalNodeParent,
|
||||
visitorKeys,
|
||||
);
|
||||
const parent = replacementParent.node;
|
||||
if (replacementParent.type === 'array') {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = removeFromArray(
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key],
|
||||
replacementParent.targetIndex,
|
||||
);
|
||||
} else {
|
||||
// $FlowExpectedError[prop-missing]
|
||||
parent[replacementParent.key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Corrects the parent pointers in direct children of the given node.
|
||||
*/
|
||||
export function setParentPointersInDirectChildren(
|
||||
node: ESNode,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): void {
|
||||
for (const key: $FlowFixMe of getVisitorKeys(node, visitorKeys)) {
|
||||
if (isNode(node[key])) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node[key].parent = node;
|
||||
} else if (Array.isArray(node[key])) {
|
||||
for (const child of node[key]) {
|
||||
child.parent = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses the entire subtree to ensure the parent pointers are set correctly.
|
||||
*/
|
||||
export function updateAllParentPointers(
|
||||
node: ESNode,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
) {
|
||||
SimpleTraverser.traverse(node, {
|
||||
enter(node, parent) {
|
||||
// $FlowExpectedError[cannot-write]
|
||||
node.parent = parent;
|
||||
},
|
||||
leave() {},
|
||||
visitorKeys,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone node and add new props.
|
||||
*
|
||||
* This will only create a new object if the overrides actually result in a change.
|
||||
*/
|
||||
export function nodeWith<T: ESNode>(
|
||||
node: T,
|
||||
overrideProps: Partial<T>,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): T {
|
||||
// Check if this will actually result in a change, maintaining referential equality is important.
|
||||
const willBeUnchanged = Object.entries(overrideProps).every(
|
||||
([key, value]) => {
|
||||
const node_: $FlowFixMe = node;
|
||||
if (Array.isArray(value)) {
|
||||
return Array.isArray(node_[key])
|
||||
? arrayIsEqual(node_[key], value)
|
||||
: false;
|
||||
}
|
||||
return node_[key] === value;
|
||||
},
|
||||
);
|
||||
if (willBeUnchanged) {
|
||||
return node;
|
||||
}
|
||||
|
||||
// Create new node.
|
||||
// $FlowExpectedError[cannot-spread-interface]
|
||||
const newNode: T = {
|
||||
...node,
|
||||
...overrideProps,
|
||||
};
|
||||
|
||||
// Ensure parent pointers are correctly set within this nodes children.
|
||||
setParentPointersInDirectChildren(newNode, visitorKeys);
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shallow clones node, providing a new reference for an existing node.
|
||||
*/
|
||||
export function shallowCloneNode<T: ESNode>(
|
||||
node: T,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): T {
|
||||
// $FlowExpectedError[cannot-spread-interface]
|
||||
const newNode: T = {...node};
|
||||
|
||||
// Ensure parent pointers are correctly set within this nodes children.
|
||||
setParentPointersInDirectChildren(newNode, visitorKeys);
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deeply clones node and its entire tree.
|
||||
*/
|
||||
export function deepCloneNode<T: ESNode>(
|
||||
node: T,
|
||||
visitorKeys?: ?VisitorKeysType,
|
||||
): T {
|
||||
const clone: T = JSON.parse(
|
||||
JSON.stringify(node, (key, value) => {
|
||||
// null out parent pointers
|
||||
if (key === 'parent') {
|
||||
return undefined;
|
||||
}
|
||||
return value;
|
||||
}),
|
||||
);
|
||||
|
||||
updateAllParentPointers(clone, visitorKeys);
|
||||
|
||||
return clone;
|
||||
}
|
||||
Reference in New Issue
Block a user