11library angular2.src.compiler.view_compiler.compile_element;
22
3+ import "package:angular2/src/facade/exceptions.dart" show BaseException;
34import "../output/output_ast.dart" as o;
45import "../identifiers.dart" show Identifiers, identifierToken;
56import "constants.dart" show InjectMethodVars;
@@ -27,6 +28,7 @@ import "util.dart"
2728import "compile_query.dart"
2829 show CompileQuery, createQueryList, addQueryToTokenMap;
2930import "compile_method.dart" show CompileMethod;
31+ import "../util.dart" show ValueTransformer, visitValue;
3032
3133class CompileNode {
3234 CompileElement parent;
@@ -104,6 +106,7 @@ class CompileElement extends CompileNode {
104106 _createAppElement () {
105107 var fieldName = '''_appEl_${ this . nodeIndex }''' ;
106108 var parentNodeIndex = this .isRootElement () ? null : this .parent.nodeIndex;
109+ // private is fine here as no child view will reference an AppElement
107110 this .view.fields.add (new o.ClassField (fieldName,
108111 o.importType (Identifiers .AppElement ), [o.StmtModifier .Private ]));
109112 var statement = o.THIS_EXPR
@@ -187,13 +190,7 @@ class CompileElement extends CompileNode {
187190 .importExpr (provider.useClass)
188191 .instantiate (depsExpr, o.importType (provider.useClass));
189192 } else {
190- if (provider.useValue is CompileIdentifierMetadata ) {
191- return o.importExpr (provider.useValue);
192- } else if (provider.useValue is o.Expression ) {
193- return provider.useValue;
194- } else {
195- return o.literal (provider.useValue);
196- }
193+ return _convertValueToOutputAst (provider.useValue);
197194 }
198195 }).toList ();
199196 var propName =
@@ -468,13 +465,12 @@ o.Expression createProviderProperty(
468465 type = o.DYNAMIC_TYPE ;
469466 }
470467 if (isEager) {
471- view.fields.add (new o.ClassField (propName, type, [o. StmtModifier . Private ] ));
468+ view.fields.add (new o.ClassField (propName, type));
472469 view.createMethod.addStmt (
473470 o.THIS_EXPR .prop (propName).set (resolvedProviderValueExpr).toStmt ());
474471 } else {
475472 var internalField = '''_${ propName }''' ;
476- view.fields
477- .add (new o.ClassField (internalField, type, [o.StmtModifier .Private ]));
473+ view.fields.add (new o.ClassField (internalField, type));
478474 var getter = new CompileMethod (view);
479475 getter.resetDebugInfo (compileElement.nodeIndex, compileElement.sourceAst);
480476 // Note: Equals is important for JS so that it also checks the undefined case!
@@ -494,3 +490,37 @@ class _QueryWithRead {
494490 this .read = isPresent (query.meta.read) ? query.meta.read : match;
495491 }
496492}
493+
494+ o.Expression _convertValueToOutputAst (dynamic value) {
495+ return visitValue (value, new _ValueOutputAstTransformer (), null );
496+ }
497+
498+ class _ValueOutputAstTransformer extends ValueTransformer {
499+ o.Expression visitArray (List <dynamic > arr, dynamic context) {
500+ return o.literalArr (
501+ arr.map ((value) => visitValue (value, this , context)).toList ());
502+ }
503+
504+ o.Expression visitStringMap (Map <String , dynamic > map, dynamic context) {
505+ var entries = [];
506+ StringMapWrapper .forEach (map, (value, key) {
507+ entries.add ([key, visitValue (value, this , context)]);
508+ });
509+ return o.literalMap (entries);
510+ }
511+
512+ o.Expression visitPrimitive (dynamic value, dynamic context) {
513+ return o.literal (value);
514+ }
515+
516+ o.Expression visitOther (dynamic value, dynamic context) {
517+ if (value is CompileIdentifierMetadata ) {
518+ return o.importExpr (value);
519+ } else if (value is o.Expression ) {
520+ return value;
521+ } else {
522+ throw new BaseException (
523+ '''Illegal state: Don\' t now how to compile value ${ value }''' );
524+ }
525+ }
526+ }
0 commit comments