如何在dart中使用Analyzer编辑节点

时间:2018-06-04 11:15:28

标签: dart dart-analyzer

如何使用分析器在dart中编辑源文件。 这是我试过的

import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/scanner.dart';

String src = """
Dynamism d = new Dynamism(expert:true);
main(){
var o = new Object();
d.on(o).hi = 'bye';
}
""";
main() {
var ast = parseCompilationUnit(src, parseFunctionBodies: true);
print('initial value: ');
print(ast.toSource());
var v = new Visitor();
ast.visitChildren(v);
print('After modification:');
print(ast.toSource());
}
class Visitor extends RecursiveAstVisitor {
@override
 visitAssignmentExpression(AssignmentExpression node) {
//filter
var p = new RegExp(r'.*\.on\(\w\)');
if (!p.hasMatch(node.toString())) return;

//replace
SimpleStringLiteral ssl = _create_SimpleStringLiteral(node);
node.parent.accept(new NodeReplacer(node, ssl));
}
}

SimpleStringLiteral _create_SimpleStringLiteral(AstNode node) {
String new_string = modify(node.toString());
int line_num = node.offset;
//holds the position and type
StringToken st = new StringToken(TokenType.STRING, new_string, 
line_num);
return new SimpleStringLiteral(st, new_string);
}

String modify(String s) {
List parts = s.split('=');
var value = parts[1];
List l = parts[0].split('.');
String dynamism = l.sublist(0, l.length - 1).join('.');
String propertyName = l.last.trim();
return '${dynamism}.set("${propertyName}",${value})';
}

但它在SimpleStringLiteral上抛出错误,说它是一个抽象类。请帮我编辑再见'到了再见'。我认为以前版本的dart有它的实现,但现在它是抽象的。

1 个答案:

答案 0 :(得分:0)

而不是使用 返回新的SimpleStringLiteral(st,new_string)试试 返回astFactory.simpleStringLiteral(st,new_string);

import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/scanner.dart';

String src = """
Dynamism d = new Dynamism(expert:true);
main(){
var o = new Object();
d.on(o).hi = 'bye';
}
""";
main() {
var ast = parseCompilationUnit(src, parseFunctionBodies: true);
print('initial value: ');
print(ast.toSource());
var v = new Visitor();
ast.visitChildren(v);
print('After modification:');
print(ast.toSource());
}
class Visitor extends RecursiveAstVisitor {
@override
 visitAssignmentExpression(AssignmentExpression node) {
//filter
var p = new RegExp(r'.*\.on\(\w\)');
if (!p.hasMatch(node.toString())) return;

//replace
SimpleStringLiteral ssl = _create_SimpleStringLiteral(node);
node.parent.accept(new NodeReplacer(node, ssl));
}
}

SimpleStringLiteral _create_SimpleStringLiteral(AstNode node) {
String new_string = modify(node.toString());
int line_num = node.offset;
//holds the position and type
StringToken st = new StringToken(TokenType.STRING, new_string, 
line_num);
return astFactory.simpleStringLiteral(st, new_string);
}

String modify(String s) {
List parts = s.split('=');
var value = parts[1];
List l = parts[0].split('.');
String dynamism = l.sublist(0, l.length - 1).join('.');
String propertyName = l.last.trim();
return '${dynamism}.set("${propertyName}",${value})';
}