Skip to content

Commit

Permalink
Add public helper to AstNodeFactory returning the constructor argumen…
Browse files Browse the repository at this point in the history
…ts to recreate a given node
  • Loading branch information
cd1m0 committed Aug 22, 2024
1 parent 3ef6bf2 commit 9a44005
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ast/ast_node_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,10 +1074,24 @@ export class ASTNodeFactory {
return node;
}

/**
* Return a copy of the given `node`, along with all its children.
* Optionally pass in a `remapping` from ids in the old context, to ids of
* the old context of any sibling nodes that may be referred in node (e.g.
* by `referencedDeclaration` fields).
*/
copy<T extends ASTNode>(node: T, remappings?: IDMap): T {
return this.copyWithMapping(node, remappings)[0];
}

/**
* Return a tuple containing a copy of the given `node` and all its
* children, and a mapping of ids between the old nodes and the new nodes.
*
* Optionally pass in a `remapping` from ids in the old context, to ids of
* the old context of any sibling nodes that may be referred in node (e.g.
* by `referencedDeclaration` fields).
*/
copyWithMapping<T extends ASTNode>(node: T, remappings?: IDMap): [T, IDMap] {
const cache = new Map<number, number>(remappings ? remappings.entries() : []);
const clone = this.copyHelper(node, cache);
Expand Down Expand Up @@ -1166,6 +1180,21 @@ export class ASTNodeFactory {
return clone;
}

/**
* Return the list of arguments (after `id` and `src`) that need to be
* passed to `node`'s constructor to recreate `node`.
*/
getNodeConstructorArgs<T extends ASTNode>(node: T): any[] {
const ctor = node.constructor as ASTNodeConstructor<T>;
const extractor = argExtractionMapping.get(ctor);

if (extractor === undefined) {
throw new Error(`Unable to find extractor for node constructor ${ctor.name}`);
}

return extractor(node);
}

private copyValue(value: any, cache: IDMap): any {
if (value === null || value === undefined) {
return value;
Expand Down

0 comments on commit 9a44005

Please sign in to comment.