Dart replacing the node in the abstract syntax tree
Here is the solution for that.
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/token.dart';
String src = """
void main(){
String a ="hey";
String b ="there";
print(\$a);
}
""";
AstNode ast;
main() async {
ast = parseCompilationUnit(src, parseFunctionBodies: true);
print(ast.toString());
Visitor v = new Visitor();
ast.visitChildren(v);
print(ast.toString());
}
class Visitor extends SimpleAstVisitor {
@override
visitFunctionDeclaration(FunctionDeclaration node) {
for (var cn in node.childEntities) {
if (cn.runtimeType.toString() == "FunctionExpressionImpl") {
Visitor v = new Visitor();
cn.visitChildren(v);
}
}
}
@override
visitBlockFunctionBody(BlockFunctionBody node) {
Visitor v = new Visitor();
node.visitChildren(v);
}
@override
visitBlock(Block node) {
for (var cn in node.childEntities) {
if (cn.runtimeType.toString() == "ExpressionStatementImpl") {
Visitor v = new Visitor();
cn.visitChildren(v);
}
}
}
@override
visitMethodInvocation(MethodInvocation node) {
String result = "print(\$a\$b)";
Token resultToken =
new StringToken(TokenType.STRING, result, node.beginToken.offset);
AstNode replacement = new SimpleStringLiteral(resultToken, result);
node.parent.accept(new NodeReplacer(node, replacement));
}
}

Given pointers to the head nodes of linked lists that merge together at some point, find the Node where the two lists merge. It is guaranteed that the two head Nodes will be different, and neither will be NULL.




