feat(dart/transform): Improve constant evaluation

Use `package:analyzer`'s `ConstantEvaluator` to read from the AST.
This cleanly builds values for us from adjacent strings, interpolations,
etc.
This commit is contained in:
Tim Blasi
2015-05-26 16:53:14 -07:00
parent a9be2ebf1b
commit 5d2af54730
11 changed files with 114 additions and 82 deletions

View File

@ -210,6 +210,7 @@ bool _isViewAnnotation(Annotation node) => '${node.name}' == 'View';
class AnnotationsTransformVisitor extends ToSourceVisitor {
final AsyncStringWriter writer;
final XHR _xhr;
final ConstantEvaluator _evaluator = new ConstantEvaluator();
bool _processingView = false;
AnnotationsTransformVisitor(AsyncStringWriter writer, this._xhr)
@ -257,14 +258,15 @@ class AnnotationsTransformVisitor extends ToSourceVisitor {
return super.visitNamedExpression(node);
}
var keyString = '${node.name.label}';
if (keyString == 'templateUrl' && node.expression is SimpleStringLiteral) {
var url = stringLiteralToString(node.expression);
writer.print("template: r'''");
writer.asyncPrint(_xhr.get(url));
writer.print("'''");
return null;
} else {
return super.visitNamedExpression(node);
if (keyString == 'templateUrl') {
var url = node.expression.accept(_evaluator);
if (url is String) {
writer.print("template: r'''");
writer.asyncPrint(_xhr.get(url));
writer.print("'''");
return null;
}
}
return super.visitNamedExpression(node);
}
}