diff --git a/modules/@angular/compiler/src/app_module_compiler.ts b/modules/@angular/compiler/src/app_module_compiler.ts index 907c7fecdd..b0ac17ff1c 100644 --- a/modules/@angular/compiler/src/app_module_compiler.ts +++ b/modules/@angular/compiler/src/app_module_compiler.ts @@ -12,6 +12,7 @@ import {CompileAppModuleMetadata, CompileDiDependencyMetadata, CompileIdentifier import {isBlank, isPresent} from './facade/lang'; import {Identifiers, identifierToken} from './identifiers'; import * as o from './output/output_ast'; +import {convertValueToOutputAst} from './output/value_util'; import {ParseLocation, ParseSourceFile, ParseSourceSpan} from './parse_util'; import {AppModuleProviderParser} from './provider_parser'; import {ProviderAst, ProviderAstType} from './template_ast'; @@ -140,7 +141,7 @@ class _InjectorBuilder { result = o.importExpr(provider.useClass).instantiate(depsExpr, o.importType(provider.useClass)); } else { - result = o.literal(provider.useValue); + result = convertValueToOutputAst(provider.useValue); } return result; } diff --git a/modules/@angular/compiler/src/output/output_ast.ts b/modules/@angular/compiler/src/output/output_ast.ts index 7d51a8b911..b47c2f1e47 100644 --- a/modules/@angular/compiler/src/output/output_ast.ts +++ b/modules/@angular/compiler/src/output/output_ast.ts @@ -896,29 +896,6 @@ export function fn(params: FnParam[], body: Statement[], type: Type = null): Fun return new FunctionExpr(params, body, type); } -export function literal(value: any, type: Type = null): Expression { - return visitValue(value, new _ValueOutputAstTransformer(), type); -} - -class _ValueOutputAstTransformer implements ValueTransformer { - visitArray(arr: any[], type: Type): Expression { - return literalArr(arr.map(value => visitValue(value, this, null)), type); - } - visitStringMap(map: {[key: string]: any}, type: MapType): Expression { - var entries: Array[] = []; - StringMapWrapper.forEach(map, (value: any, key: string) => { - entries.push([key, visitValue(value, this, null)]); - }); - return literalMap(entries, type); - } - visitPrimitive(value: any, type: Type): Expression { return new LiteralExpr(value, type); } - visitOther(value: any, type: Type): Expression { - if (value instanceof CompileIdentifierMetadata) { - return importExpr(value); - } else if (value instanceof Expression) { - return value; - } else { - throw new BaseException(`Illegal state: Don't now how to compile value ${value}`); - } - } +export function literal(value: any, type: Type = null): LiteralExpr { + return new LiteralExpr(value, type); } diff --git a/modules/@angular/compiler/src/output/value_util.ts b/modules/@angular/compiler/src/output/value_util.ts new file mode 100644 index 0000000000..b09a3b1098 --- /dev/null +++ b/modules/@angular/compiler/src/output/value_util.ts @@ -0,0 +1,33 @@ +import {CompileIdentifierMetadata} from '../compile_metadata'; +import {StringMapWrapper} from '../facade/collection'; +import {BaseException} from '../facade/exceptions'; +import {ValueTransformer, visitValue} from '../util'; + +import * as o from './output_ast'; + +export function convertValueToOutputAst(value: any, type: o.Type = null): o.Expression { + return visitValue(value, new _ValueOutputAstTransformer(), type); +} + +class _ValueOutputAstTransformer implements ValueTransformer { + visitArray(arr: any[], type: o.Type): o.Expression { + return o.literalArr(arr.map(value => visitValue(value, this, null)), type); + } + visitStringMap(map: {[key: string]: any}, type: o.MapType): o.Expression { + var entries: Array[] = []; + StringMapWrapper.forEach(map, (value: any, key: string) => { + entries.push([key, visitValue(value, this, null)]); + }); + return o.literalMap(entries, type); + } + visitPrimitive(value: any, type: o.Type): o.Expression { return o.literal(value, type); } + visitOther(value: any, type: o.Type): o.Expression { + if (value instanceof CompileIdentifierMetadata) { + return o.importExpr(value); + } else if (value instanceof o.Expression) { + return value; + } else { + throw new BaseException(`Illegal state: Don't now how to compile value ${value}`); + } + } +} diff --git a/modules/@angular/compiler/src/view_compiler/compile_element.ts b/modules/@angular/compiler/src/view_compiler/compile_element.ts index 920969790b..c1fbae1972 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_element.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_element.ts @@ -12,6 +12,7 @@ import {ListWrapper, StringMapWrapper} from '../facade/collection'; import {isBlank, isPresent} from '../facade/lang'; import {Identifiers, identifierToken} from '../identifiers'; import * as o from '../output/output_ast'; +import {convertValueToOutputAst} from '../output/value_util'; import {ProviderAst, ProviderAstType, ReferenceAst, TemplateAst} from '../template_ast'; import {CompileView} from './compile_view'; @@ -165,7 +166,7 @@ export class CompileElement extends CompileNode { return o.importExpr(provider.useClass) .instantiate(depsExpr, o.importType(provider.useClass)); } else { - return o.literal(provider.useValue); + return convertValueToOutputAst(provider.useValue); } }); var propName = `_${resolvedProvider.token.name}_${this.nodeIndex}_${this._instances.size}`;