replace method

bool replace (
  1. AstNode oldNode,
  2. AstNode newNode

Replace the oldNode with the newNode in the AST structure containing the old node. Return true if the replacement was successful.

Throws an ArgumentError if either node is null, if the old node does not have a parent node, or if the AST structure has been corrupted.


static bool replace(AstNode oldNode, AstNode newNode) {
  if (oldNode == null || newNode == null) {
    throw ArgumentError("The old and new nodes must be non-null");
  } else if (identical(oldNode, newNode)) {
    return true;
  AstNode parent = oldNode.parent;
  if (parent == null) {
    throw ArgumentError("The old node is not a child of another node");
  NodeReplacer replacer = NodeReplacer(oldNode, newNode);
  return parent.accept(replacer);