Skip to content

Commit 9a44005

Browse files
committed
Add public helper to AstNodeFactory returning the constructor arguments to recreate a given node
1 parent 3ef6bf2 commit 9a44005

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: src/ast/ast_node_factory.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,24 @@ export class ASTNodeFactory {
10741074
return node;
10751075
}
10761076

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

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

1183+
/**
1184+
* Return the list of arguments (after `id` and `src`) that need to be
1185+
* passed to `node`'s constructor to recreate `node`.
1186+
*/
1187+
getNodeConstructorArgs<T extends ASTNode>(node: T): any[] {
1188+
const ctor = node.constructor as ASTNodeConstructor<T>;
1189+
const extractor = argExtractionMapping.get(ctor);
1190+
1191+
if (extractor === undefined) {
1192+
throw new Error(`Unable to find extractor for node constructor ${ctor.name}`);
1193+
}
1194+
1195+
return extractor(node);
1196+
}
1197+
11691198
private copyValue(value: any, cache: IDMap): any {
11701199
if (value === null || value === undefined) {
11711200
return value;

0 commit comments

Comments
 (0)