From 90c87fa6ad4da8ae32b66c42349e54981ec57429 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 29 Mar 2016 17:15:07 -0700 Subject: [PATCH] fix(codegen): stringify using an opaque ID when toString contains parens. Using toString results in 'function (_arg1, arg2) {' when using closure compiler for 6-to-5. Closes #7825 --- .../angular2/src/compiler/runtime_metadata.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/angular2/src/compiler/runtime_metadata.ts b/modules/angular2/src/compiler/runtime_metadata.ts index d15d2a9229..6a48faac7a 100644 --- a/modules/angular2/src/compiler/runtime_metadata.ts +++ b/modules/angular2/src/compiler/runtime_metadata.ts @@ -27,12 +27,30 @@ import {getUrlScheme} from 'angular2/src/compiler/url_resolver'; export class RuntimeMetadataResolver { private _directiveCache = new Map(); private _pipeCache = new Map(); + private _anonymousTypes = new Map(); + private _anonymousTypeIndex = 0; constructor(private _directiveResolver: DirectiveResolver, private _pipeResolver: PipeResolver, private _viewResolver: ViewResolver, @Optional() @Inject(PLATFORM_DIRECTIVES) private _platformDirectives: Type[], @Optional() @Inject(PLATFORM_PIPES) private _platformPipes: Type[]) {} + /** + * Wrap the stringify method to avoid naming things `function (arg1...) {` + */ + private sanitizeName(obj: any): string { + let result = stringify(obj); + if (result.indexOf('(') < 0) { + return result; + } + let found = this._anonymousTypes.get(obj); + if (!found) { + this._anonymousTypes.set(obj, this._anonymousTypeIndex++); + found = this._anonymousTypes.get(obj); + } + return `anonymous_type_${found}_`; + } + getDirectiveMetadata(directiveType: Type): cpl.CompileDirectiveMetadata { var meta = this._directiveCache.get(directiveType); if (isBlank(meta)) { @@ -62,7 +80,7 @@ export class RuntimeMetadataResolver { isComponent: isPresent(templateMeta), dynamicLoadable: true, type: new cpl.CompileTypeMetadata( - {name: stringify(directiveType), moduleUrl: moduleUrl, runtime: directiveType}), + {name: this.sanitizeName(directiveType), moduleUrl: moduleUrl, runtime: directiveType}), template: templateMeta, changeDetection: changeDetectionStrategy, inputs: dirMeta.inputs, @@ -82,7 +100,7 @@ export class RuntimeMetadataResolver { var moduleUrl = reflector.importUri(pipeType); meta = new cpl.CompilePipeMetadata({ type: new cpl.CompileTypeMetadata( - {name: stringify(pipeType), moduleUrl: moduleUrl, runtime: pipeType}), + {name: this.sanitizeName(pipeType), moduleUrl: moduleUrl, runtime: pipeType}), name: pipeMeta.name, pure: pipeMeta.pure });