refactor(dart/transform): Create common dumbEval function
This commit is contained in:
parent
150d3686c3
commit
b318945680
|
@ -12,6 +12,8 @@ import 'package:angular2/src/transform/common/annotation_matcher.dart';
|
|||
import 'package:angular2/src/transform/common/interface_matcher.dart';
|
||||
import 'package:barback/barback.dart' show AssetId;
|
||||
|
||||
import 'dumb_eval.dart';
|
||||
|
||||
class DirectiveMetadataReader {
|
||||
final _DirectiveMetadataVisitor _visitor;
|
||||
final TemplateCompiler _templateCompiler;
|
||||
|
@ -53,22 +55,11 @@ class DirectiveMetadataReader {
|
|||
}
|
||||
}
|
||||
|
||||
/// Visitor that attempts to evaluate a provided `node` syntactically.
|
||||
///
|
||||
/// This lack of semantic information means it cannot do much - for
|
||||
/// example, it can create a list from a list literal and combine adjacent
|
||||
/// strings but cannot determine that an identifier is a constant string,
|
||||
/// even if that identifier is defined in the same [CompilationUnit].
|
||||
///
|
||||
/// Returns the result of evaluation or [ConstantEvaluator.NOT_A_CONSTANT]
|
||||
/// where appropriate.
|
||||
final ConstantEvaluator _evaluator = new ConstantEvaluator();
|
||||
|
||||
/// Evaluates the [Map] represented by `expression` and adds all `key`,
|
||||
/// `value` pairs to `map`. If `expression` does not evaluate to a [Map],
|
||||
/// throws a descriptive [FormatException].
|
||||
void _populateMap(Expression expression, Map map, String propertyName) {
|
||||
var evaluated = expression.accept(_evaluator);
|
||||
var evaluated = dumbEval(expression);
|
||||
if (evaluated is! Map) {
|
||||
throw new FormatException(
|
||||
'Angular 2 expects a Map but could not understand the value for '
|
||||
|
@ -87,7 +78,7 @@ void _populateMap(Expression expression, Map map, String propertyName) {
|
|||
/// descriptive [FormatException].
|
||||
void _populateList(
|
||||
Expression expression, List<String> list, String propertyName) {
|
||||
var evaluated = expression.accept(_evaluator);
|
||||
var evaluated = dumbEval(expression);
|
||||
if (evaluated is! List) {
|
||||
throw new FormatException(
|
||||
'Angular 2 expects a List but could not understand the value for '
|
||||
|
@ -100,7 +91,7 @@ void _populateList(
|
|||
/// Evaluates `node` and expects that the result will be a string. If not,
|
||||
/// throws a [FormatException].
|
||||
String _expressionToString(Expression node, String nodeDescription) {
|
||||
var value = node.accept(_evaluator);
|
||||
var value = dumbEval(node);
|
||||
if (value is! String) {
|
||||
throw new FormatException(
|
||||
'Angular 2 could not understand the value '
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
library angular2.transform.common.dumb_eval;
|
||||
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
|
||||
final _constantEvaluator = new ConstantEvaluator();
|
||||
|
||||
/// The value returned if the result of `dumbEval` is not a constant.
|
||||
final NOT_A_CONSTANT = ConstantEvaluator.NOT_A_CONSTANT;
|
||||
|
||||
/// Performs a very limited syntactic evaluation of `expr`.
|
||||
///
|
||||
/// This lack of semantic information means this method cannot do much - for
|
||||
/// example, it can create a list from a list literal and combine adjacent
|
||||
/// strings but cannot determine that an identifier is a constant string,
|
||||
/// even if that identifier is defined in the same [CompilationUnit].
|
||||
///
|
||||
/// Returns the result of evaluation or [NOT_A_CONSTANT] where appropriate.
|
||||
dynamic dumbEval(Expression expr) {
|
||||
var val;
|
||||
if (expr is SimpleStringLiteral) {
|
||||
val = stringLiteralToString(expr);
|
||||
} else {
|
||||
val = expr.accept(_constantEvaluator);
|
||||
}
|
||||
return val != NOT_A_CONSTANT ? val : null;
|
||||
}
|
|
@ -7,6 +7,7 @@ import 'package:analyzer/src/generated/ast.dart';
|
|||
import 'package:angular2/src/core/compiler/xhr.dart' show XHR;
|
||||
import 'package:angular2/src/transform/common/annotation_matcher.dart';
|
||||
import 'package:angular2/src/transform/common/asset_reader.dart';
|
||||
import 'package:angular2/src/transform/common/dumb_eval.dart';
|
||||
import 'package:angular2/src/transform/common/async_string_writer.dart';
|
||||
import 'package:angular2/src/transform/common/logging.dart';
|
||||
import 'package:angular2/src/transform/common/options.dart';
|
||||
|
@ -119,7 +120,7 @@ class _ViewPropInliner extends RecursiveAstVisitor<Object> {
|
|||
|
||||
@override
|
||||
Object visitNamedExpression(NamedExpression node) {
|
||||
if (_isInlining && node is NamedExpression) {
|
||||
if (_isInlining) {
|
||||
if (node.name is! Label || node.name.label is! SimpleIdentifier) {
|
||||
throw new FormatException(
|
||||
'Angular 2 currently only supports simple identifiers in directives.',
|
||||
|
@ -141,7 +142,7 @@ class _ViewPropInliner extends RecursiveAstVisitor<Object> {
|
|||
}
|
||||
|
||||
void _populateStyleUrls(NamedExpression node) {
|
||||
var urls = _dumbEval(node.expression);
|
||||
var urls = dumbEval(node.expression);
|
||||
if (urls is! List) {
|
||||
logger.warning('styleUrls is not a List of Strings (${node.expression})');
|
||||
return;
|
||||
|
@ -162,7 +163,7 @@ class _ViewPropInliner extends RecursiveAstVisitor<Object> {
|
|||
}
|
||||
|
||||
void _populateTemplateUrl(NamedExpression node) {
|
||||
var url = _dumbEval(node.expression);
|
||||
var url = dumbEval(node.expression);
|
||||
if (url is! String) {
|
||||
logger.warning('template url is not a String (${node.expression})');
|
||||
return;
|
||||
|
@ -185,15 +186,3 @@ class _ViewPropInliner extends RecursiveAstVisitor<Object> {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
final _constantEvaluator = new ConstantEvaluator();
|
||||
|
||||
dynamic _dumbEval(Expression expr) {
|
||||
var val;
|
||||
if (expr is SimpleStringLiteral) {
|
||||
val = stringLiteralToString(expr);
|
||||
} else {
|
||||
val = expr.accept(_constantEvaluator);
|
||||
}
|
||||
return val != ConstantEvaluator.NOT_A_CONSTANT ? val : null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue