UNPKG

@akala/core

Version:
37 lines 1.2 kB
import { ExpressionVisitor } from "./expression-visitor.js"; /** * Replaces occurrences of a specific expression with another during traversal. * This visitor updates an expression tree by substituting the original expression with the new one. */ export class ExpressionUpdater extends ExpressionVisitor { original; newExp; /** * Creates an instance of ExpressionUpdater. * @param original - The expression to be replaced in the tree. * @param newExp - The new expression that replaces the original. */ constructor(original, newExp) { super(); this.original = original; this.newExp = newExp; } rootCall = true; visit(expression) { if (this.rootCall) { const originalAccept = this.original.accept; this.original.accept = () => { return this.newExp; }; try { this.rootCall = false; return super.visit(expression); } finally { this.original.accept = originalAccept; } } return super.visit(expression); } } //# sourceMappingURL=expression-updater.js.map