1+ import { BaseException } from 'angular2/src/facade/exceptions' ;
12import * as o from '../output/output_ast' ;
23import { Identifiers , identifierToken } from '../identifiers' ;
34import { InjectMethodVars } from './constants' ;
@@ -18,6 +19,7 @@ import {
1819import { getPropertyInView , createDiTokenExpression , injectFromViewParentInjector } from './util' ;
1920import { CompileQuery , createQueryList , addQueryToTokenMap } from './compile_query' ;
2021import { CompileMethod } from './compile_method' ;
22+ import { ValueTransformer , visitValue } from '../util' ;
2123
2224export class CompileNode {
2325 constructor ( public parent : CompileElement , public view : CompileView , public nodeIndex : number ,
@@ -72,6 +74,7 @@ export class CompileElement extends CompileNode {
7274 private _createAppElement ( ) {
7375 var fieldName = `_appEl_${ this . nodeIndex } ` ;
7476 var parentNodeIndex = this . isRootElement ( ) ? null : this . parent . nodeIndex ;
77+ // private is fine here as no child view will reference an AppElement
7578 this . view . fields . push ( new o . ClassField ( fieldName , o . importType ( Identifiers . AppElement ) ,
7679 [ o . StmtModifier . Private ] ) ) ;
7780 var statement = o . THIS_EXPR . prop ( fieldName )
@@ -140,13 +143,7 @@ export class CompileElement extends CompileNode {
140143 return o . importExpr ( provider . useClass )
141144 . instantiate ( depsExpr , o . importType ( provider . useClass ) ) ;
142145 } else {
143- if ( provider . useValue instanceof CompileIdentifierMetadata ) {
144- return o . importExpr ( provider . useValue ) ;
145- } else if ( provider . useValue instanceof o . Expression ) {
146- return provider . useValue ;
147- } else {
148- return o . literal ( provider . useValue ) ;
149- }
146+ return _convertValueToOutputAst ( provider . useValue ) ;
150147 }
151148 } ) ;
152149 var propName = `_${ resolvedProvider . token . name } _${ this . nodeIndex } _${ this . _instances . size } ` ;
@@ -379,11 +376,11 @@ function createProviderProperty(propName: string, provider: ProviderAst,
379376 type = o . DYNAMIC_TYPE ;
380377 }
381378 if ( isEager ) {
382- view . fields . push ( new o . ClassField ( propName , type , [ o . StmtModifier . Private ] ) ) ;
379+ view . fields . push ( new o . ClassField ( propName , type ) ) ;
383380 view . createMethod . addStmt ( o . THIS_EXPR . prop ( propName ) . set ( resolvedProviderValueExpr ) . toStmt ( ) ) ;
384381 } else {
385382 var internalField = `_${ propName } ` ;
386- view . fields . push ( new o . ClassField ( internalField , type , [ o . StmtModifier . Private ] ) ) ;
383+ view . fields . push ( new o . ClassField ( internalField , type ) ) ;
387384 var getter = new CompileMethod ( view ) ;
388385 getter . resetDebugInfo ( compileElement . nodeIndex , compileElement . sourceAst ) ;
389386 // Note: Equals is important for JS so that it also checks the undefined case!
@@ -402,3 +399,29 @@ class _QueryWithRead {
402399 this . read = isPresent ( query . meta . read ) ? query . meta . read : match ;
403400 }
404401}
402+
403+ function _convertValueToOutputAst ( value : any ) : o . Expression {
404+ return visitValue ( value , new _ValueOutputAstTransformer ( ) , null ) ;
405+ }
406+
407+ class _ValueOutputAstTransformer extends ValueTransformer {
408+ visitArray ( arr : any [ ] , context : any ) : o . Expression {
409+ return o . literalArr ( arr . map ( value => visitValue ( value , this , context ) ) ) ;
410+ }
411+ visitStringMap ( map : { [ key : string ] : any } , context : any ) : o . Expression {
412+ var entries = [ ] ;
413+ StringMapWrapper . forEach (
414+ map , ( value , key ) => { entries . push ( [ key , visitValue ( value , this , context ) ] ) ; } ) ;
415+ return o . literalMap ( entries ) ;
416+ }
417+ visitPrimitive ( value : any , context : any ) : o . Expression { return o . literal ( value ) ; }
418+ visitOther ( value : any , context : any ) : o . Expression {
419+ if ( value instanceof CompileIdentifierMetadata ) {
420+ return o . importExpr ( value ) ;
421+ } else if ( value instanceof o . Expression ) {
422+ return value ;
423+ } else {
424+ throw new BaseException ( `Illegal state: Don't now how to compile value ${ value } ` ) ;
425+ }
426+ }
427+ }
0 commit comments