Skip to content

Commit

Permalink
Expose copyWithMapping function that additionally returns a mapping f…
Browse files Browse the repository at this point in the history
…rom old ids to new ids
  • Loading branch information
cd1m0 committed Aug 21, 2024
1 parent 62ded84 commit 590e6bc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ast/ast_node_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Specific<Args extends any[]> = Args["length"] extends 0
? Rest
: [];

type IDMap = Map<number, number>;
export type IDMap = Map<number, number>;

const argExtractionMapping = new Map<ASTNodeConstructor<ASTNode>, (node: any) => any[]>([
[ASTNode, (node: ASTNode): Specific<ConstructorParameters<typeof ASTNode>> => [node.raw]],
Expand Down Expand Up @@ -1075,6 +1075,10 @@ export class ASTNodeFactory {
}

copy<T extends ASTNode>(node: T, remappings?: IDMap): T {
return this.copyWithMapping(node, remappings)[0];
}

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);
const context = this.context;
Expand All @@ -1086,7 +1090,7 @@ export class ASTNodeFactory {
postprocessor.processNode(child, context);
}

return clone;
return [clone, cache];
}

private patchIds(node: ASTNode, cache: IDMap): void {
Expand Down

0 comments on commit 590e6bc

Please sign in to comment.