1
+ import { BaseException } from 'angular2/src/facade/exceptions' ;
1
2
import * as o from '../output/output_ast' ;
2
3
import { Identifiers , identifierToken } from '../identifiers' ;
3
4
import { InjectMethodVars } from './constants' ;
@@ -18,6 +19,7 @@ import {
18
19
import { getPropertyInView , createDiTokenExpression , injectFromViewParentInjector } from './util' ;
19
20
import { CompileQuery , createQueryList , addQueryToTokenMap } from './compile_query' ;
20
21
import { CompileMethod } from './compile_method' ;
22
+ import { ValueTransformer , visitValue } from '../util' ;
21
23
22
24
export class CompileNode {
23
25
constructor ( public parent : CompileElement , public view : CompileView , public nodeIndex : number ,
@@ -72,6 +74,7 @@ export class CompileElement extends CompileNode {
72
74
private _createAppElement ( ) {
73
75
var fieldName = `_appEl_${ this . nodeIndex } ` ;
74
76
var parentNodeIndex = this . isRootElement ( ) ? null : this . parent . nodeIndex ;
77
+ // private is fine here as no child view will reference an AppElement
75
78
this . view . fields . push ( new o . ClassField ( fieldName , o . importType ( Identifiers . AppElement ) ,
76
79
[ o . StmtModifier . Private ] ) ) ;
77
80
var statement = o . THIS_EXPR . prop ( fieldName )
@@ -140,13 +143,7 @@ export class CompileElement extends CompileNode {
140
143
return o . importExpr ( provider . useClass )
141
144
. instantiate ( depsExpr , o . importType ( provider . useClass ) ) ;
142
145
} 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 ) ;
150
147
}
151
148
} ) ;
152
149
var propName = `_${ resolvedProvider . token . name } _${ this . nodeIndex } _${ this . _instances . size } ` ;
@@ -379,11 +376,11 @@ function createProviderProperty(propName: string, provider: ProviderAst,
379
376
type = o . DYNAMIC_TYPE ;
380
377
}
381
378
if ( isEager ) {
382
- view . fields . push ( new o . ClassField ( propName , type , [ o . StmtModifier . Private ] ) ) ;
379
+ view . fields . push ( new o . ClassField ( propName , type ) ) ;
383
380
view . createMethod . addStmt ( o . THIS_EXPR . prop ( propName ) . set ( resolvedProviderValueExpr ) . toStmt ( ) ) ;
384
381
} else {
385
382
var internalField = `_${ propName } ` ;
386
- view . fields . push ( new o . ClassField ( internalField , type , [ o . StmtModifier . Private ] ) ) ;
383
+ view . fields . push ( new o . ClassField ( internalField , type ) ) ;
387
384
var getter = new CompileMethod ( view ) ;
388
385
getter . resetDebugInfo ( compileElement . nodeIndex , compileElement . sourceAst ) ;
389
386
// Note: Equals is important for JS so that it also checks the undefined case!
@@ -402,3 +399,29 @@ class _QueryWithRead {
402
399
this . read = isPresent ( query . meta . read ) ? query . meta . read : match ;
403
400
}
404
401
}
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