From 9a440058655dae3a19dccaa9c50edaf1c065dcee Mon Sep 17 00:00:00 2001 From: Dimitar Bounov Date: Thu, 22 Aug 2024 08:25:00 -1000 Subject: [PATCH] Add public helper to AstNodeFactory returning the constructor arguments to recreate a given node --- src/ast/ast_node_factory.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ast/ast_node_factory.ts b/src/ast/ast_node_factory.ts index e031bbb5..c661125f 100644 --- a/src/ast/ast_node_factory.ts +++ b/src/ast/ast_node_factory.ts @@ -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(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(node: T, remappings?: IDMap): [T, IDMap] { const cache = new Map(remappings ? remappings.entries() : []); const clone = this.copyHelper(node, cache); @@ -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(node: T): any[] { + const ctor = node.constructor as ASTNodeConstructor; + 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;