diff --git a/modules/@angular/compiler-cli/src/static_reflection_capabilities.ts b/modules/@angular/compiler-cli/src/static_reflection_capabilities.ts index 0b31a70b26433a..c56bc957238bc3 100644 --- a/modules/@angular/compiler-cli/src/static_reflection_capabilities.ts +++ b/modules/@angular/compiler-cli/src/static_reflection_capabilities.ts @@ -42,6 +42,16 @@ export class StaticAndDynamicReflectionCapabilities { setter(name: string) { return this.dynamicDelegate.setter(name); } method(name: string) { return this.dynamicDelegate.method(name); } importUri(type: any): string { return this.staticDelegate.importUri(type); } + resolveType(name: string, moduleUrl: string) { + return this.staticDelegate.resolveType(name, moduleUrl); + } + resolveEnum(enumType: any, name: string): any { + if (isStaticType(enumType)) { + return this.staticDelegate.resolveEnum(enumType, name); + } else { + return null; + } + } } function isStaticType(type: any): boolean { diff --git a/modules/@angular/compiler-cli/src/static_reflector.ts b/modules/@angular/compiler-cli/src/static_reflector.ts index d2115def621892..e3212e84befcea 100644 --- a/modules/@angular/compiler-cli/src/static_reflector.ts +++ b/modules/@angular/compiler-cli/src/static_reflector.ts @@ -74,6 +74,16 @@ export class StaticReflector implements ReflectorReader { return staticSymbol ? staticSymbol.filePath : null; } + resolveType(name: string, moduleUrl: string): any { + const result = this.host.findDeclaration(moduleUrl, name, ''); + return result; + } + + resolveEnum(enumType: any, name: string): any { + const staticSymbol: StaticSymbol = enumType; + return this.host.getStaticSymbol(staticSymbol.filePath, staticSymbol.name, [name]); + } + public annotations(type: StaticSymbol): any[] { let annotations = this.annotationCache.get(type); if (!annotations) { diff --git a/modules/@angular/compiler/src/animation/animation_compiler.ts b/modules/@angular/compiler/src/animation/animation_compiler.ts index 8c8fd5a28466af..08877ff689356f 100644 --- a/modules/@angular/compiler/src/animation/animation_compiler.ts +++ b/modules/@angular/compiler/src/animation/animation_compiler.ts @@ -10,7 +10,7 @@ import {ANY_STATE, AnimationOutput, DEFAULT_STATE, EMPTY_STATE} from '../../core import {CompileDirectiveMetadata} from '../compile_metadata'; import {StringMapWrapper} from '../facade/collection'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import * as t from '../template_parser/template_ast'; @@ -114,8 +114,8 @@ class _AnimationBuilder implements AnimationAstVisitor { o.literalMap(StringMapWrapper.keys(entry).map(key => [key, o.literal(entry[key])]))); }); - return o.importExpr(Identifiers.AnimationStyles).instantiate([ - o.importExpr(Identifiers.collectAndResolveStyles).callFn([ + return o.importExpr(resolveIdentifier(Identifiers.AnimationStyles)).instantiate([ + o.importExpr(resolveIdentifier(Identifiers.collectAndResolveStyles)).callFn([ _ANIMATION_COLLECTED_STYLES, o.literalArr(stylesArr) ]) ]); @@ -123,7 +123,7 @@ class _AnimationBuilder implements AnimationAstVisitor { visitAnimationKeyframe(ast: AnimationKeyframeAst, context: _AnimationBuilderContext): o.Expression { - return o.importExpr(Identifiers.AnimationKeyframe).instantiate([ + return o.importExpr(resolveIdentifier(Identifiers.AnimationKeyframe)).instantiate([ o.literal(ast.offset), ast.styles.visit(this, context) ]); } @@ -144,10 +144,11 @@ class _AnimationBuilder implements AnimationAstVisitor { _visitEndStateAnimation(ast: AnimationStepAst, context: _AnimationBuilderContext): o.Expression { var startingStylesExpr = ast.startingStyles.visit(this, context); var keyframeExpressions = ast.keyframes.map(keyframe => keyframe.visit(this, context)); - var keyframesExpr = o.importExpr(Identifiers.balanceAnimationKeyframes).callFn([ - _ANIMATION_COLLECTED_STYLES, _ANIMATION_END_STATE_STYLES_VAR, - o.literalArr(keyframeExpressions) - ]); + var keyframesExpr = + o.importExpr(resolveIdentifier(Identifiers.balanceAnimationKeyframes)).callFn([ + _ANIMATION_COLLECTED_STYLES, _ANIMATION_END_STATE_STYLES_VAR, + o.literalArr(keyframeExpressions) + ]); return this._callAnimateMethod(ast, startingStylesExpr, keyframesExpr, context); } @@ -166,13 +167,16 @@ class _AnimationBuilder implements AnimationAstVisitor { visitAnimationSequence(ast: AnimationSequenceAst, context: _AnimationBuilderContext): o.Expression { var playerExprs = ast.steps.map(step => step.visit(this, context)); - return o.importExpr(Identifiers.AnimationSequencePlayer).instantiate([o.literalArr( - playerExprs)]); + return o.importExpr(resolveIdentifier(Identifiers.AnimationSequencePlayer)).instantiate([ + o.literalArr(playerExprs) + ]); } visitAnimationGroup(ast: AnimationGroupAst, context: _AnimationBuilderContext): o.Expression { var playerExprs = ast.steps.map(step => step.visit(this, context)); - return o.importExpr(Identifiers.AnimationGroupPlayer).instantiate([o.literalArr(playerExprs)]); + return o.importExpr(resolveIdentifier(Identifiers.AnimationGroupPlayer)).instantiate([ + o.literalArr(playerExprs) + ]); } visitAnimationStateDeclaration( @@ -265,18 +269,18 @@ class _AnimationBuilder implements AnimationAstVisitor { _ANIMATION_END_STATE_STYLES_VAR.equals(o.NULL_EXPR), [_ANIMATION_END_STATE_STYLES_VAR.set(_ANIMATION_DEFAULT_STATE_VAR).toStmt()])); - var RENDER_STYLES_FN = o.importExpr(Identifiers.renderStyles); + var RENDER_STYLES_FN = o.importExpr(resolveIdentifier(Identifiers.renderStyles)); // before we start any animation we want to clear out the starting // styles from the element's style property (since they were placed // there at the end of the last animation - statements.push( - RENDER_STYLES_FN - .callFn([ - _ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR, - o.importExpr(Identifiers.clearStyles).callFn([_ANIMATION_START_STATE_STYLES_VAR]) - ]) - .toStmt()); + statements.push(RENDER_STYLES_FN + .callFn([ + _ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR, + o.importExpr(resolveIdentifier(Identifiers.clearStyles)) + .callFn([_ANIMATION_START_STATE_STYLES_VAR]) + ]) + .toStmt()); ast.stateTransitions.forEach(transAst => statements.push(transAst.visit(this, context))); @@ -284,7 +288,8 @@ class _AnimationBuilder implements AnimationAstVisitor { // so that the onDone callback can be used for tracking statements.push(new o.IfStmt( _ANIMATION_PLAYER_VAR.equals(o.NULL_EXPR), - [_ANIMATION_PLAYER_VAR.set(o.importExpr(Identifiers.NoOpAnimationPlayer).instantiate([])) + [_ANIMATION_PLAYER_VAR + .set(o.importExpr(resolveIdentifier(Identifiers.NoOpAnimationPlayer)).instantiate([])) .toStmt()])); // once complete we want to apply the styles on the element @@ -295,14 +300,16 @@ class _AnimationBuilder implements AnimationAstVisitor { .callMethod( 'onDone', [o.fn( - [], [RENDER_STYLES_FN - .callFn([ - _ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR, - o.importExpr(Identifiers.prepareFinalAnimationStyles).callFn([ + [], + [RENDER_STYLES_FN + .callFn([ + _ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_FACTORY_RENDERER_VAR, + o.importExpr(resolveIdentifier(Identifiers.prepareFinalAnimationStyles)) + .callFn([ _ANIMATION_START_STATE_STYLES_VAR, _ANIMATION_END_STATE_STYLES_VAR ]) - ]) - .toStmt()])]) + ]) + .toStmt()])]) .toStmt()); statements.push(_ANIMATION_FACTORY_VIEW_VAR @@ -319,7 +326,7 @@ class _AnimationBuilder implements AnimationAstVisitor { [ new o.FnParam( _ANIMATION_FACTORY_VIEW_VAR.name, - o.importType(Identifiers.AppView, [o.DYNAMIC_TYPE])), + o.importType(resolveIdentifier(Identifiers.AppView), [o.DYNAMIC_TYPE])), new o.FnParam(_ANIMATION_FACTORY_ELEMENT_VAR.name, o.DYNAMIC_TYPE), new o.FnParam(_ANIMATION_CURRENT_STATE_VAR.name, o.DYNAMIC_TYPE), new o.FnParam(_ANIMATION_NEXT_STATE_VAR.name, o.DYNAMIC_TYPE) diff --git a/modules/@angular/compiler/src/compile_metadata.ts b/modules/@angular/compiler/src/compile_metadata.ts index 9fbefde6790bdf..d87d450fc8ace9 100644 --- a/modules/@angular/compiler/src/compile_metadata.ts +++ b/modules/@angular/compiler/src/compile_metadata.ts @@ -32,8 +32,6 @@ export abstract class CompileMetadataWithIdentifier { get runtimeCacheKey(): any { return unimplemented(); } - get assetCacheKey(): any { return unimplemented(); } - equalsTo(id2: CompileMetadataWithIdentifier): boolean { return unimplemented(); } } @@ -93,7 +91,6 @@ export class CompileIdentifierMetadata implements CompileMetadataWithIdentifier prefix: string; moduleUrl: string; value: any; - private _assetCacheKey: any = UNDEFINED; constructor( {runtime, name, moduleUrl, prefix, value}: @@ -109,23 +106,9 @@ export class CompileIdentifierMetadata implements CompileMetadataWithIdentifier get runtimeCacheKey(): any { return this.identifier.runtime; } - get assetCacheKey(): any { - if (this._assetCacheKey === UNDEFINED) { - if (isPresent(this.moduleUrl) && isPresent(getUrlScheme(this.moduleUrl))) { - var uri = reflector.importUri({'filePath': this.moduleUrl, 'name': this.name}); - this._assetCacheKey = `${this.name}|${uri}`; - } else { - this._assetCacheKey = null; - } - } - return this._assetCacheKey; - } - equalsTo(id2: CompileIdentifierMetadata): boolean { var rk = this.runtimeCacheKey; - var ak = this.assetCacheKey; - return (isPresent(rk) && rk == id2.runtimeCacheKey) || - (isPresent(ak) && ak == id2.assetCacheKey); + return isPresent(rk) && rk == id2.runtimeCacheKey; } } @@ -233,19 +216,9 @@ export class CompileTokenMetadata implements CompileMetadataWithIdentifier { } } - get assetCacheKey(): any { - if (isPresent(this.identifier)) { - return this.identifier.assetCacheKey; - } else { - return this.value; - } - } - equalsTo(token2: CompileTokenMetadata): boolean { var rk = this.runtimeCacheKey; - var ak = this.assetCacheKey; - return (isPresent(rk) && rk == token2.runtimeCacheKey) || - (isPresent(ak) && ak == token2.assetCacheKey); + return isPresent(rk) && rk == token2.runtimeCacheKey; } get name(): string { @@ -275,24 +248,17 @@ export class CompileIdentifierMapngModuleType); let appCompileResult = this._ngModuleCompiler.compile(ngModule, [ + new CompileProviderMetadata( + {token: resolveIdentifierToken(Identifiers.LOCALE_ID), useValue: this._localeId}), new CompileProviderMetadata({ - token: new CompileTokenMetadata({identifier: Identifiers.LOCALE_ID}), - useValue: this._localeId - }), - new CompileProviderMetadata({ - token: new CompileTokenMetadata({identifier: Identifiers.TRANSLATIONS_FORMAT}), + token: resolveIdentifierToken(Identifiers.TRANSLATIONS_FORMAT), useValue: this._translationFormat }) ]); @@ -134,15 +132,16 @@ export class OfflineCompiler { var compFactoryVar = _componentFactoryName(compMeta.type); targetStatements.push( o.variable(compFactoryVar) - .set(o.importExpr(Identifiers.ComponentFactory, [o.importType(compMeta.type)]) + .set(o.importExpr(resolveIdentifier(Identifiers.ComponentFactory), [o.importType( + compMeta.type)]) .instantiate( [ o.literal(compMeta.selector), o.variable(hostViewFactoryVar), o.importExpr(compMeta.type) ], o.importType( - Identifiers.ComponentFactory, [o.importType(compMeta.type)], - [o.TypeModifier.Const]))) + resolveIdentifier(Identifiers.ComponentFactory), + [o.importType(compMeta.type)], [o.TypeModifier.Const]))) .toDeclStmt(null, [o.StmtModifier.Final])); return compFactoryVar; } diff --git a/modules/@angular/compiler/src/provider_analyzer.ts b/modules/@angular/compiler/src/provider_analyzer.ts index ed09287918fb99..40b5e26b7ff0d7 100644 --- a/modules/@angular/compiler/src/provider_analyzer.ts +++ b/modules/@angular/compiler/src/provider_analyzer.ts @@ -10,7 +10,7 @@ import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileIdentifierMap, CompileNgModuleMetadata, CompileProviderMetadata, CompileQueryMetadata, CompileTokenMetadata, CompileTypeMetadata} from './compile_metadata'; import {ListWrapper} from './facade/collection'; import {isArray, isBlank, isPresent, normalizeBlank} from './facade/lang'; -import {Identifiers, identifierToken} from './identifiers'; +import {Identifiers, resolveIdentifierToken} from './identifiers'; import {ParseError, ParseSourceSpan} from './parse_util'; import {AttrAst, DirectiveAst, ProviderAst, ProviderAstType, ReferenceAst, VariableAst} from './template_parser/template_ast'; @@ -65,7 +65,7 @@ export class ProviderElementContext { refs.forEach((refAst) => { this._addQueryReadsTo(new CompileTokenMetadata({value: refAst.name}), queriedTokens); }); - if (isPresent(queriedTokens.get(identifierToken(Identifiers.ViewContainerRef)))) { + if (isPresent(queriedTokens.get(resolveIdentifierToken(Identifiers.ViewContainerRef)))) { this._hasViewContainer = true; } @@ -206,18 +206,18 @@ export class ProviderElementContext { // access builtints if ((requestingProviderType === ProviderAstType.Directive || requestingProviderType === ProviderAstType.Component)) { - if (dep.token.equalsTo(identifierToken(Identifiers.Renderer)) || - dep.token.equalsTo(identifierToken(Identifiers.ElementRef)) || - dep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef)) || - dep.token.equalsTo(identifierToken(Identifiers.TemplateRef))) { + if (dep.token.equalsTo(resolveIdentifierToken(Identifiers.Renderer)) || + dep.token.equalsTo(resolveIdentifierToken(Identifiers.ElementRef)) || + dep.token.equalsTo(resolveIdentifierToken(Identifiers.ChangeDetectorRef)) || + dep.token.equalsTo(resolveIdentifierToken(Identifiers.TemplateRef))) { return dep; } - if (dep.token.equalsTo(identifierToken(Identifiers.ViewContainerRef))) { + if (dep.token.equalsTo(resolveIdentifierToken(Identifiers.ViewContainerRef))) { this._hasViewContainer = true; } } // access the injector - if (dep.token.equalsTo(identifierToken(Identifiers.Injector))) { + if (dep.token.equalsTo(resolveIdentifierToken(Identifiers.Injector))) { return dep; } // access providers @@ -254,7 +254,7 @@ export class ProviderElementContext { // check @Host restriction if (isBlank(result)) { if (!dep.isHost || this._viewContext.component.type.isHost || - identifierToken(this._viewContext.component.type).equalsTo(dep.token) || + resolveIdentifierToken(this._viewContext.component.type).equalsTo(dep.token) || isPresent(this._viewContext.viewProviders.get(dep.token))) { result = dep; } else { @@ -363,8 +363,8 @@ export class NgModuleProviderAnalyzer { var foundLocal = false; if (!dep.isSkipSelf && isPresent(dep.token)) { // access the injector - if (dep.token.equalsTo(identifierToken(Identifiers.Injector)) || - dep.token.equalsTo(identifierToken(Identifiers.ComponentFactoryResolver))) { + if (dep.token.equalsTo(resolveIdentifierToken(Identifiers.Injector)) || + dep.token.equalsTo(resolveIdentifierToken(Identifiers.ComponentFactoryResolver))) { foundLocal = true; // access providers } else if (isPresent(this._getOrCreateLocalProvider(dep.token, eager))) { diff --git a/modules/@angular/compiler/src/template_parser/template_parser.ts b/modules/@angular/compiler/src/template_parser/template_parser.ts index 160cbb120e048e..fc9b9ff17ad917 100644 --- a/modules/@angular/compiler/src/template_parser/template_parser.ts +++ b/modules/@angular/compiler/src/template_parser/template_parser.ts @@ -15,7 +15,7 @@ import {Parser} from '../expression_parser/parser'; import {ListWrapper, SetWrapper, StringMapWrapper} from '../facade/collection'; import {isBlank, isPresent, isString} from '../facade/lang'; import {HtmlParser} from '../i18n/html_parser'; -import {Identifiers, identifierToken} from '../identifiers'; +import {Identifiers, identifierToken, resolveIdentifierToken} from '../identifiers'; import * as html from '../ml_parser/ast'; import {ParseTreeResult} from '../ml_parser/html_parser'; import {expandNodes} from '../ml_parser/icu_ast_expander'; @@ -732,7 +732,7 @@ class TemplateParseVisitor implements html.Visitor { } else if (isBlank(component)) { let refToken: CompileTokenMetadata = null; if (isTemplateElement) { - refToken = identifierToken(Identifiers.TemplateRef); + refToken = resolveIdentifierToken(Identifiers.TemplateRef); } targetReferences.push(new ReferenceAst(elOrDirRef.name, refToken, elOrDirRef.sourceSpan)); } diff --git a/modules/@angular/compiler/src/view_compiler/compile_element.ts b/modules/@angular/compiler/src/view_compiler/compile_element.ts index 005ffad164cd6a..b7c8f85fb796f9 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_element.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_element.ts @@ -10,7 +10,7 @@ import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileIdentifierMap, CompileIdentifierMetadata, CompileProviderMetadata, CompileQueryMetadata, CompileTokenMetadata} from '../compile_metadata'; import {ListWrapper, StringMapWrapper} from '../facade/collection'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers, identifierToken} from '../identifiers'; +import {Identifiers, identifierToken, resolveIdentifier, resolveIdentifierToken} from '../identifiers'; import * as o from '../output/output_ast'; import {convertValueToOutputAst} from '../output/value_util'; import {ProviderAst, ProviderAstType, ReferenceAst, TemplateAst} from '../template_parser/template_ast'; @@ -62,11 +62,12 @@ export class CompileElement extends CompileNode { this.referenceTokens = {}; references.forEach(ref => this.referenceTokens[ref.name] = ref.value); - this.elementRef = o.importExpr(Identifiers.ElementRef).instantiate([this.renderNode]); - this.instances.add(identifierToken(Identifiers.ElementRef), this.elementRef); + this.elementRef = + o.importExpr(resolveIdentifier(Identifiers.ElementRef)).instantiate([this.renderNode]); + this.instances.add(resolveIdentifierToken(Identifiers.ElementRef), this.elementRef); this.injector = o.THIS_EXPR.callMethod('injector', [o.literal(this.nodeIndex)]); - this.instances.add(identifierToken(Identifiers.Injector), this.injector); - this.instances.add(identifierToken(Identifiers.Renderer), o.THIS_EXPR.prop('renderer')); + this.instances.add(resolveIdentifierToken(Identifiers.Injector), this.injector); + this.instances.add(resolveIdentifierToken(Identifiers.Renderer), o.THIS_EXPR.prop('renderer')); if (this.hasViewContainer || this.hasEmbeddedView || isPresent(this.component)) { this._createAppElement(); } @@ -77,16 +78,17 @@ export class CompileElement extends CompileNode { var parentNodeIndex = this.isRootElement() ? null : this.parent.nodeIndex; // private is fine here as no child view will reference an AppElement this.view.fields.push(new o.ClassField( - fieldName, o.importType(Identifiers.AppElement), [o.StmtModifier.Private])); + fieldName, o.importType(resolveIdentifier(Identifiers.AppElement)), + [o.StmtModifier.Private])); var statement = o.THIS_EXPR.prop(fieldName) - .set(o.importExpr(Identifiers.AppElement).instantiate([ + .set(o.importExpr(resolveIdentifier(Identifiers.AppElement)).instantiate([ o.literal(this.nodeIndex), o.literal(parentNodeIndex), o.THIS_EXPR, this.renderNode ])) .toStmt(); this.view.createMethod.addStmt(statement); this.appElement = o.THIS_EXPR.prop(fieldName); - this.instances.add(identifierToken(Identifiers.AppElement), this.appElement); + this.instances.add(resolveIdentifierToken(Identifiers.AppElement), this.appElement); } public createComponentFactoryResolver(entryComponents: CompileIdentifierMetadata[]) { @@ -94,12 +96,13 @@ export class CompileElement extends CompileNode { return; } var createComponentFactoryResolverExpr = - o.importExpr(Identifiers.CodegenComponentFactoryResolver).instantiate([ + o.importExpr(resolveIdentifier(Identifiers.CodegenComponentFactoryResolver)).instantiate([ o.literalArr(entryComponents.map((entryComponent) => o.importExpr(entryComponent))), - injectFromViewParentInjector(identifierToken(Identifiers.ComponentFactoryResolver), false) + injectFromViewParentInjector( + resolveIdentifierToken(Identifiers.ComponentFactoryResolver), false) ]); var provider = new CompileProviderMetadata({ - token: identifierToken(Identifiers.ComponentFactoryResolver), + token: resolveIdentifierToken(Identifiers.ComponentFactoryResolver), useValue: createComponentFactoryResolverExpr }); // Add ComponentFactoryResolver as first provider as it does not have deps on other providers @@ -122,11 +125,14 @@ export class CompileElement extends CompileNode { setEmbeddedView(embeddedView: CompileView) { this.embeddedView = embeddedView; if (isPresent(embeddedView)) { - var createTemplateRefExpr = o.importExpr(Identifiers.TemplateRef_).instantiate([ - this.appElement, this.embeddedView.viewFactory - ]); - var provider = new CompileProviderMetadata( - {token: identifierToken(Identifiers.TemplateRef), useValue: createTemplateRefExpr}); + var createTemplateRefExpr = + o.importExpr(resolveIdentifier(Identifiers.TemplateRef_)).instantiate([ + this.appElement, this.embeddedView.viewFactory + ]); + var provider = new CompileProviderMetadata({ + token: resolveIdentifierToken(Identifiers.TemplateRef), + useValue: createTemplateRefExpr + }); // Add TemplateRef as first provider as it does not have deps on other providers this._resolvedProvidersArray.unshift(new ProviderAst( provider.token, false, true, [provider], ProviderAstType.Builtin, [], @@ -137,7 +143,7 @@ export class CompileElement extends CompileNode { beforeChildren(): void { if (this.hasViewContainer) { this.instances.add( - identifierToken(Identifiers.ViewContainerRef), this.appElement.prop('vcRef')); + resolveIdentifierToken(Identifiers.ViewContainerRef), this.appElement.prop('vcRef')); } this._resolvedProviders = new CompileIdentifierMap(); @@ -319,7 +325,7 @@ export class CompileElement extends CompileNode { if (isPresent(dep.token)) { // access builtins with special visibility if (isBlank(result)) { - if (dep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef))) { + if (dep.token.equalsTo(resolveIdentifierToken(Identifiers.ChangeDetectorRef))) { if (requestingProviderType === ProviderAstType.Component) { return this._compViewExpr.prop('ref'); } else { diff --git a/modules/@angular/compiler/src/view_compiler/compile_pipe.ts b/modules/@angular/compiler/src/view_compiler/compile_pipe.ts index 940a3ed5048c57..55f8e6c1c8ee1a 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_pipe.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_pipe.ts @@ -9,7 +9,7 @@ import {CompilePipeMetadata} from '../compile_metadata'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers, identifierToken} from '../identifiers'; +import {Identifiers, identifierToken, resolveIdentifier, resolveIdentifierToken} from '../identifiers'; import * as o from '../output/output_ast'; import {CompileView} from './compile_view'; @@ -42,7 +42,7 @@ export class CompilePipe { constructor(public view: CompileView, public meta: CompilePipeMetadata) { this.instance = o.THIS_EXPR.prop(`_pipe_${meta.name}_${view.pipeCount++}`); var deps = this.meta.type.diDeps.map((diDep) => { - if (diDep.token.equalsTo(identifierToken(Identifiers.ChangeDetectorRef))) { + if (diDep.token.equalsTo(resolveIdentifierToken(Identifiers.ChangeDetectorRef))) { return getPropertyInView(o.THIS_EXPR.prop('ref'), this.view, this.view.componentView); } return injectFromViewParentInjector(diDep.token, false); @@ -66,7 +66,7 @@ export class CompilePipe { pipeInstanceSeenFromPureProxy.prop('transform') .callMethod(o.BuiltinMethod.Bind, [pipeInstanceSeenFromPureProxy]), args.length, purePipeProxyInstance, callingView); - return o.importExpr(Identifiers.castByValue) + return o.importExpr(resolveIdentifier(Identifiers.castByValue)) .callFn([purePipeProxyInstance, pipeInstanceSeenFromPureProxy.prop('transform')]) .callFn(args); } else { diff --git a/modules/@angular/compiler/src/view_compiler/compile_query.ts b/modules/@angular/compiler/src/view_compiler/compile_query.ts index f847ff0f4b60f6..24027f1734532b 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_query.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_query.ts @@ -9,7 +9,7 @@ import {CompileIdentifierMap, CompileQueryMetadata, CompileTokenMetadata} from '../compile_metadata'; import {ListWrapper} from '../facade/collection'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import {CompileElement} from './compile_element'; @@ -115,12 +115,13 @@ function mapNestedViews( export function createQueryList( query: CompileQueryMetadata, directiveInstance: o.Expression, propertyName: string, compileView: CompileView): o.Expression { - compileView.fields.push( - new o.ClassField(propertyName, o.importType(Identifiers.QueryList, [o.DYNAMIC_TYPE]))); + compileView.fields.push(new o.ClassField( + propertyName, o.importType(resolveIdentifier(Identifiers.QueryList), [o.DYNAMIC_TYPE]))); var expr = o.THIS_EXPR.prop(propertyName); compileView.createMethod.addStmt( o.THIS_EXPR.prop(propertyName) - .set(o.importExpr(Identifiers.QueryList, [o.DYNAMIC_TYPE]).instantiate([])) + .set(o.importExpr(resolveIdentifier(Identifiers.QueryList), [o.DYNAMIC_TYPE]) + .instantiate([])) .toStmt()); return expr; } diff --git a/modules/@angular/compiler/src/view_compiler/compile_view.ts b/modules/@angular/compiler/src/view_compiler/compile_view.ts index b34e22bcf6d63d..ae17456f05f3dd 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_view.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_view.ts @@ -12,7 +12,7 @@ import {CompileDirectiveMetadata, CompileIdentifierMap, CompileIdentifierMetadat import {CompilerConfig} from '../config'; import {ListWrapper} from '../facade/collection'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import {createDiTokenExpression} from '../util'; @@ -150,7 +150,7 @@ export class CompileView implements NameResolver { createLiteralArray(values: o.Expression[]): o.Expression { if (values.length === 0) { - return o.importExpr(Identifiers.EMPTY_ARRAY); + return o.importExpr(resolveIdentifier(Identifiers.EMPTY_ARRAY)); } var proxyExpr = o.THIS_EXPR.prop(`_arr_${this.literalArrayCount++}`); var proxyParams: o.FnParam[] = []; @@ -170,7 +170,7 @@ export class CompileView implements NameResolver { createLiteralMap(entries: Array>): o.Expression { if (entries.length === 0) { - return o.importExpr(Identifiers.EMPTY_MAP); + return o.importExpr(resolveIdentifier(Identifiers.EMPTY_MAP)); } var proxyExpr = o.THIS_EXPR.prop(`_map_${this.literalMapCount++}`); var proxyParams: o.FnParam[] = []; diff --git a/modules/@angular/compiler/src/view_compiler/constants.ts b/modules/@angular/compiler/src/view_compiler/constants.ts index d9294736d6ac58..5211a0aa3f2da0 100644 --- a/modules/@angular/compiler/src/view_compiler/constants.ts +++ b/modules/@angular/compiler/src/view_compiler/constants.ts @@ -10,56 +10,80 @@ import {ChangeDetectionStrategy, ViewEncapsulation} from '@angular/core'; import {ChangeDetectorStatus, ViewType} from '../../core_private'; import {CompileIdentifierMetadata} from '../compile_metadata'; -import {isBlank, resolveEnumToken} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {isBlank} from '../facade/lang'; +import {Identifiers, resolveEnumIdentifier, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; -function _enumExpression(classIdentifier: CompileIdentifierMetadata, value: any): o.Expression { - if (isBlank(value)) return o.NULL_EXPR; - var name = resolveEnumToken(classIdentifier.runtime, value); - return o.importExpr(new CompileIdentifierMetadata({ - name: `${classIdentifier.name}.${name}`, - moduleUrl: classIdentifier.moduleUrl, - runtime: value - })); +function _enumExpression(classIdentifier: CompileIdentifierMetadata, name: string): o.Expression { + return o.importExpr(resolveEnumIdentifier(classIdentifier, name)); } export class ViewTypeEnum { static fromValue(value: ViewType): o.Expression { - return _enumExpression(Identifiers.ViewType, value); + const viewType = resolveIdentifier(Identifiers.ViewType); + switch (value) { + case ViewType.HOST: + return _enumExpression(viewType, 'HOST'); + case ViewType.COMPONENT: + return _enumExpression(viewType, 'COMPONENT'); + case ViewType.EMBEDDED: + return _enumExpression(viewType, 'EMBEDDED'); + default: + throw Error(`Inavlid ViewType value: ${value}`); + } } - static HOST = ViewTypeEnum.fromValue(ViewType.HOST); - static COMPONENT = ViewTypeEnum.fromValue(ViewType.COMPONENT); - static EMBEDDED = ViewTypeEnum.fromValue(ViewType.EMBEDDED); } export class ViewEncapsulationEnum { static fromValue(value: ViewEncapsulation): o.Expression { - return _enumExpression(Identifiers.ViewEncapsulation, value); + const viewEncapsulation = resolveIdentifier(Identifiers.ViewEncapsulation); + switch (value) { + case ViewEncapsulation.Emulated: + return _enumExpression(viewEncapsulation, 'Emulated'); + case ViewEncapsulation.Native: + return _enumExpression(viewEncapsulation, 'Native'); + case ViewEncapsulation.None: + return _enumExpression(viewEncapsulation, 'None'); + default: + throw Error(`Inavlid ViewEncapsulation value: ${value}`); + } } - static Emulated = ViewEncapsulationEnum.fromValue(ViewEncapsulation.Emulated); - static Native = ViewEncapsulationEnum.fromValue(ViewEncapsulation.Native); - static None = ViewEncapsulationEnum.fromValue(ViewEncapsulation.None); } export class ChangeDetectionStrategyEnum { static fromValue(value: ChangeDetectionStrategy): o.Expression { - return _enumExpression(Identifiers.ChangeDetectionStrategy, value); + const changeDetectionStrategy = resolveIdentifier(Identifiers.ChangeDetectionStrategy); + switch (value) { + case ChangeDetectionStrategy.OnPush: + return _enumExpression(changeDetectionStrategy, 'OnPush'); + case ChangeDetectionStrategy.Default: + return _enumExpression(changeDetectionStrategy, 'Default'); + default: + throw Error(`Inavlid ChangeDetectionStrategy value: ${value}`); + } } - static OnPush = ChangeDetectionStrategyEnum.fromValue(ChangeDetectionStrategy.OnPush); - static Default = ChangeDetectionStrategyEnum.fromValue(ChangeDetectionStrategy.Default); } export class ChangeDetectorStatusEnum { static fromValue(value: ChangeDetectorStatusEnum): o.Expression { - return _enumExpression(Identifiers.ChangeDetectorStatus, value); + const changeDetectorStatus = resolveIdentifier(Identifiers.ChangeDetectorStatus); + switch (value) { + case ChangeDetectorStatus.CheckOnce: + return _enumExpression(changeDetectorStatus, 'CheckOnce'); + case ChangeDetectorStatus.Checked: + return _enumExpression(changeDetectorStatus, 'Checked'); + case ChangeDetectorStatus.CheckAlways: + return _enumExpression(changeDetectorStatus, 'CheckAlways'); + case ChangeDetectorStatus.Detached: + return _enumExpression(changeDetectorStatus, 'Detached'); + case ChangeDetectorStatus.Errored: + return _enumExpression(changeDetectorStatus, 'Errored'); + case ChangeDetectorStatus.Destroyed: + return _enumExpression(changeDetectorStatus, 'Destroyed'); + default: + throw Error(`Inavlid ChangeDetectorStatus value: ${value}`); + } } - static CheckOnce = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.CheckOnce); - static Checked = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.Checked); - static CheckAlways = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.CheckAlways); - static Detached = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.Detached); - static Errored = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.Errored); - static Destroyed = ChangeDetectorStatusEnum.fromValue(ChangeDetectorStatus.Destroyed); } export class ViewConstructorVars { diff --git a/modules/@angular/compiler/src/view_compiler/event_binder.ts b/modules/@angular/compiler/src/view_compiler/event_binder.ts index 1ee6ac7b595fd9..97a2e37fd9b165 100644 --- a/modules/@angular/compiler/src/view_compiler/event_binder.ts +++ b/modules/@angular/compiler/src/view_compiler/event_binder.ts @@ -10,7 +10,7 @@ import {AnimationOutput} from '../../core_private'; import {CompileDirectiveMetadata} from '../compile_metadata'; import {ListWrapper, StringMapWrapper} from '../facade/collection'; import {StringWrapper, isBlank, isPresent} from '../facade/lang'; -import {Identifiers, identifierToken} from '../identifiers'; +import {Identifiers, identifierToken, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import {BoundEventAst, DirectiveAst} from '../template_parser/template_ast'; @@ -130,7 +130,7 @@ export class CompileEventListener { 'registerAnimationOutput', [ this.compileElement.renderNode, - o.importExpr(Identifiers.AnimationOutput).instantiate([ + o.importExpr(resolveIdentifier(Identifiers.AnimationOutput)).instantiate([ o.literal(output.name), o.literal(output.phase) ]), outputListener diff --git a/modules/@angular/compiler/src/view_compiler/expression_converter.ts b/modules/@angular/compiler/src/view_compiler/expression_converter.ts index 62af382d283749..fd44f95452ed2c 100644 --- a/modules/@angular/compiler/src/view_compiler/expression_converter.ts +++ b/modules/@angular/compiler/src/view_compiler/expression_converter.ts @@ -9,7 +9,7 @@ import * as cdAst from '../expression_parser/ast'; import {isArray, isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; export interface NameResolver { @@ -193,7 +193,7 @@ class _AstToIrVisitor implements cdAst.AstVisitor { args.push(this.visit(ast.expressions[i], _Mode.Expression)); } args.push(o.literal(ast.strings[ast.strings.length - 1])); - return o.importExpr(Identifiers.interpolate).callFn(args); + return o.importExpr(resolveIdentifier(Identifiers.interpolate)).callFn(args); } visitKeyedRead(ast: cdAst.KeyedRead, mode: _Mode): any { diff --git a/modules/@angular/compiler/src/view_compiler/property_binder.ts b/modules/@angular/compiler/src/view_compiler/property_binder.ts index f4248e51a240d4..d2856a60ef25e8 100644 --- a/modules/@angular/compiler/src/view_compiler/property_binder.ts +++ b/modules/@angular/compiler/src/view_compiler/property_binder.ts @@ -11,7 +11,7 @@ import {SecurityContext} from '@angular/core'; import {EMPTY_STATE as EMPTY_ANIMATION_STATE, LifecycleHooks, isDefaultChangeDetectionStrategy} from '../../core_private'; import * as cdAst from '../expression_parser/ast'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import {BoundElementPropertyAst, BoundTextAst, DirectiveAst, PropertyBindingType} from '../template_parser/template_ast'; import {camelCaseToDashCase} from '../util'; @@ -52,8 +52,9 @@ function bind( // private is fine here as no child view will reference the cached value... view.fields.push(new o.ClassField(fieldExpr.name, null, [o.StmtModifier.Private])); - view.createMethod.addStmt( - o.THIS_EXPR.prop(fieldExpr.name).set(o.importExpr(Identifiers.UNINITIALIZED)).toStmt()); + view.createMethod.addStmt(o.THIS_EXPR.prop(fieldExpr.name) + .set(o.importExpr(resolveIdentifier(Identifiers.UNINITIALIZED))) + .toStmt()); if (checkExpression.needsValueUnwrapper) { var initValueUnwrapperStmt = DetectChangesVars.valUnwrapper.callMethod('reset', []).toStmt(); @@ -62,7 +63,7 @@ function bind( method.addStmt( currValExpr.set(checkExpression.expression).toDeclStmt(null, [o.StmtModifier.Final])); - var condition: o.Expression = o.importExpr(Identifiers.checkBinding).callFn([ + var condition: o.Expression = o.importExpr(resolveIdentifier(Identifiers.checkBinding)).callFn([ DetectChangesVars.throwOnChange, fieldExpr, currValExpr ]); if (checkExpression.needsValueUnwrapper) { @@ -160,14 +161,14 @@ function bindAndWriteToRenderer( var oldRenderVar = o.variable('oldRenderVar'); updateStmts.push(oldRenderVar.set(oldRenderValue).toDeclStmt()); updateStmts.push(new o.IfStmt( - oldRenderVar.equals(o.importExpr(Identifiers.UNINITIALIZED)), + oldRenderVar.equals(o.importExpr(resolveIdentifier(Identifiers.UNINITIALIZED))), [oldRenderVar.set(emptyStateValue).toStmt()])); // ... => void var newRenderVar = o.variable('newRenderVar'); updateStmts.push(newRenderVar.set(renderValue).toDeclStmt()); updateStmts.push(new o.IfStmt( - newRenderVar.equals(o.importExpr(Identifiers.UNINITIALIZED)), + newRenderVar.equals(o.importExpr(resolveIdentifier(Identifiers.UNINITIALIZED))), [newRenderVar.set(emptyStateValue).toStmt()])); updateStmts.push( @@ -218,7 +219,8 @@ function sanitizedValue( throw new Error(`internal error, unexpected SecurityContext ${boundProp.securityContext}.`); } let ctx = ViewProperties.viewUtils.prop('sanitizer'); - let args = [o.importExpr(Identifiers.SecurityContext).prop(enumValue), renderValue]; + let args = + [o.importExpr(resolveIdentifier(Identifiers.SecurityContext)).prop(enumValue), renderValue]; return ctx.callMethod('sanitize', args); } @@ -264,12 +266,13 @@ export function bindDirectiveInputs( statements.push(new o.IfStmt( DetectChangesVars.changes.identical(o.NULL_EXPR), [DetectChangesVars.changes - .set(o.literalMap([], new o.MapType(o.importType(Identifiers.SimpleChange)))) + .set(o.literalMap( + [], new o.MapType(o.importType(resolveIdentifier(Identifiers.SimpleChange))))) .toStmt()])); - statements.push( - DetectChangesVars.changes.key(o.literal(input.directiveName)) - .set(o.importExpr(Identifiers.SimpleChange).instantiate([fieldExpr, currValExpr])) - .toStmt()); + statements.push(DetectChangesVars.changes.key(o.literal(input.directiveName)) + .set(o.importExpr(resolveIdentifier(Identifiers.SimpleChange)) + .instantiate([fieldExpr, currValExpr])) + .toStmt()); } if (isOnPushComp) { statements.push(DetectChangesVars.changed.set(o.literal(true)).toStmt()); diff --git a/modules/@angular/compiler/src/view_compiler/util.ts b/modules/@angular/compiler/src/view_compiler/util.ts index add5c81fa22373..cb422b6e949a60 100644 --- a/modules/@angular/compiler/src/view_compiler/util.ts +++ b/modules/@angular/compiler/src/view_compiler/util.ts @@ -9,7 +9,7 @@ import {CompileDirectiveMetadata, CompileTokenMetadata} from '../compile_metadata'; import {isBlank, isPresent} from '../facade/lang'; -import {Identifiers} from '../identifiers'; +import {Identifiers, resolveIdentifier} from '../identifiers'; import * as o from '../output/output_ast'; import {createDiTokenExpression} from '../util'; @@ -87,6 +87,7 @@ export function createPureProxy( if (isBlank(pureProxyId)) { throw new Error(`Unsupported number of argument for pure functions: ${argCount}`); } - view.createMethod.addStmt( - o.THIS_EXPR.prop(pureProxyProp.name).set(o.importExpr(pureProxyId).callFn([fn])).toStmt()); + view.createMethod.addStmt(o.THIS_EXPR.prop(pureProxyProp.name) + .set(o.importExpr(resolveIdentifier(pureProxyId)).callFn([fn])) + .toStmt()); } diff --git a/modules/@angular/compiler/src/view_compiler/view_builder.ts b/modules/@angular/compiler/src/view_compiler/view_builder.ts index 7025428d296da3..ebf0bdf5fbcd68 100644 --- a/modules/@angular/compiler/src/view_compiler/view_builder.ts +++ b/modules/@angular/compiler/src/view_compiler/view_builder.ts @@ -13,7 +13,7 @@ import {AnimationCompiler} from '../animation/animation_compiler'; import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileTokenMetadata, CompileTypeMetadata} from '../compile_metadata'; import {ListWrapper, SetWrapper, StringMapWrapper} from '../facade/collection'; import {StringWrapper, isPresent} from '../facade/lang'; -import {Identifiers, identifierToken} from '../identifiers'; +import {Identifiers, identifierToken, resolveIdentifier, resolveIdentifierToken} from '../identifiers'; import * as o from '../output/output_ast'; import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, ProviderAst, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast'; import {createDiTokenExpression} from '../util'; @@ -148,7 +148,8 @@ class ViewBuilderVisitor implements TemplateAstVisitor { 'projectNodes', [ parentRenderNode, - o.importExpr(Identifiers.flattenNestedViewRenderNodes).callFn([nodesExpression]) + o.importExpr(resolveIdentifier(Identifiers.flattenNestedViewRenderNodes)) + .callFn([nodesExpression]) ]) .toStmt()); } else if (this._isRootNode(parent)) { @@ -396,7 +397,8 @@ function createViewTopLevelStmts(view: CompileView, targetStatements: o.Statemen .set(o.literalArr( view.nodes.map(createStaticNodeDebugInfo), new o.ArrayType( - new o.ExternalType(Identifiers.StaticNodeDebugInfo), [o.TypeModifier.Const]))) + new o.ExternalType(resolveIdentifier(Identifiers.StaticNodeDebugInfo)), + [o.TypeModifier.Const]))) .toDeclStmt(null, [o.StmtModifier.Final])); } @@ -404,8 +406,9 @@ function createViewTopLevelStmts(view: CompileView, targetStatements: o.Statemen var renderCompTypeVar: o.ReadVarExpr = o.variable(`renderType_${view.component.type.name}`); // fix highlighting: ` if (view.viewIndex === 0) { - targetStatements.push(renderCompTypeVar.set(o.NULL_EXPR) - .toDeclStmt(o.importType(Identifiers.RenderComponentType))); + targetStatements.push( + renderCompTypeVar.set(o.NULL_EXPR) + .toDeclStmt(o.importType(resolveIdentifier(Identifiers.RenderComponentType)))); } var viewClass = createViewClass(view, renderCompTypeVar, nodeDebugInfosVar); @@ -429,23 +432,29 @@ function createStaticNodeDebugInfo(node: CompileNode): o.Expression { [varName, isPresent(token) ? createDiTokenExpression(token) : o.NULL_EXPR]); }); } - return o.importExpr(Identifiers.StaticNodeDebugInfo) + return o.importExpr(resolveIdentifier(Identifiers.StaticNodeDebugInfo)) .instantiate( [ o.literalArr(providerTokens, new o.ArrayType(o.DYNAMIC_TYPE, [o.TypeModifier.Const])), componentToken, o.literalMap(varTokenEntries, new o.MapType(o.DYNAMIC_TYPE, [o.TypeModifier.Const])) ], - o.importType(Identifiers.StaticNodeDebugInfo, null, [o.TypeModifier.Const])); + o.importType( + resolveIdentifier(Identifiers.StaticNodeDebugInfo), null, [o.TypeModifier.Const])); } function createViewClass( view: CompileView, renderCompTypeVar: o.ReadVarExpr, nodeDebugInfosVar: o.Expression): o.ClassStmt { var viewConstructorArgs = [ - new o.FnParam(ViewConstructorVars.viewUtils.name, o.importType(Identifiers.ViewUtils)), - new o.FnParam(ViewConstructorVars.parentInjector.name, o.importType(Identifiers.Injector)), - new o.FnParam(ViewConstructorVars.declarationEl.name, o.importType(Identifiers.AppElement)) + new o.FnParam( + ViewConstructorVars.viewUtils.name, o.importType(resolveIdentifier(Identifiers.ViewUtils))), + new o.FnParam( + ViewConstructorVars.parentInjector.name, + o.importType(resolveIdentifier(Identifiers.Injector))), + new o.FnParam( + ViewConstructorVars.declarationEl.name, + o.importType(resolveIdentifier(Identifiers.AppElement))) ]; var superConstructorArgs = [ o.variable(view.className), renderCompTypeVar, ViewTypeEnum.fromValue(view.viewType), @@ -462,7 +471,7 @@ function createViewClass( var viewMethods = [ new o.ClassMethod( 'createInternal', [new o.FnParam(rootSelectorVar.name, o.STRING_TYPE)], - generateCreateMethod(view), o.importType(Identifiers.AppElement)), + generateCreateMethod(view), o.importType(resolveIdentifier(Identifiers.AppElement))), new o.ClassMethod( 'injectorGetInternal', [ @@ -482,17 +491,23 @@ function createViewClass( ].concat(view.eventHandlerMethods); var superClass = view.genConfig.genDebugInfo ? Identifiers.DebugAppView : Identifiers.AppView; var viewClass = new o.ClassStmt( - view.className, o.importExpr(superClass, [getContextType(view)]), view.fields, view.getters, - viewConstructor, viewMethods.filter((method) => method.body.length > 0)); + view.className, o.importExpr(resolveIdentifier(superClass), [getContextType(view)]), + view.fields, view.getters, viewConstructor, + viewMethods.filter((method) => method.body.length > 0)); return viewClass; } function createViewFactory( view: CompileView, viewClass: o.ClassStmt, renderCompTypeVar: o.ReadVarExpr): o.Statement { var viewFactoryArgs = [ - new o.FnParam(ViewConstructorVars.viewUtils.name, o.importType(Identifiers.ViewUtils)), - new o.FnParam(ViewConstructorVars.parentInjector.name, o.importType(Identifiers.Injector)), - new o.FnParam(ViewConstructorVars.declarationEl.name, o.importType(Identifiers.AppElement)) + new o.FnParam( + ViewConstructorVars.viewUtils.name, o.importType(resolveIdentifier(Identifiers.ViewUtils))), + new o.FnParam( + ViewConstructorVars.parentInjector.name, + o.importType(resolveIdentifier(Identifiers.Injector))), + new o.FnParam( + ViewConstructorVars.declarationEl.name, + o.importType(resolveIdentifier(Identifiers.AppElement))) ]; var initRenderCompTypeStmts: any[] = []; var templateUrlInfo: string; @@ -522,7 +537,7 @@ function createViewFactory( o.variable(viewClass.name) .instantiate(viewClass.constructorMethod.params.map( (param) => o.variable(param.name))))]), - o.importType(Identifiers.AppView, [getContextType(view)])) + o.importType(resolveIdentifier(Identifiers.AppView), [getContextType(view)])) .toDeclStmt(view.viewFactory.name, [o.StmtModifier.Final]); } @@ -589,12 +604,14 @@ function generateDetectChangesMethod(view: CompileView): o.Statement[] { varStmts.push(DetectChangesVars.changed.set(o.literal(true)).toDeclStmt(o.BOOL_TYPE)); } if (SetWrapper.has(readVars, DetectChangesVars.changes.name)) { - varStmts.push(DetectChangesVars.changes.set(o.NULL_EXPR) - .toDeclStmt(new o.MapType(o.importType(Identifiers.SimpleChange)))); + varStmts.push( + DetectChangesVars.changes.set(o.NULL_EXPR) + .toDeclStmt(new o.MapType(o.importType(resolveIdentifier(Identifiers.SimpleChange))))); } if (SetWrapper.has(readVars, DetectChangesVars.valUnwrapper.name)) { varStmts.push( - DetectChangesVars.valUnwrapper.set(o.importExpr(Identifiers.ValueUnwrapper).instantiate([])) + DetectChangesVars.valUnwrapper + .set(o.importExpr(resolveIdentifier(Identifiers.ValueUnwrapper)).instantiate([])) .toDeclStmt(null, [o.StmtModifier.Final])); } return varStmts.concat(stmts); diff --git a/modules/@angular/compiler/test/compiler_spec.ts b/modules/@angular/compiler/test/compiler_spec.ts new file mode 100644 index 00000000000000..3b270ec4d4f996 --- /dev/null +++ b/modules/@angular/compiler/test/compiler_spec.ts @@ -0,0 +1,34 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Component} from '@angular/core'; +import {TestBed} from '@angular/core/testing'; +import {expect} from '@angular/platform-browser/testing/matchers'; + +export function main() { + describe('Compiler', () => { + it('should generate the correct output when constructors have the same name', () => { + function ComponentFactory(selector: string, template: string) { + @Component({selector, template}) + class MyComponent { + } + return MyComponent; + } + const HeroComponent = ComponentFactory('my-hero', 'my hero'); + const VillianComponent = ComponentFactory('a-villian', 'a villian'); + const MainComponent = ComponentFactory( + 'my-app', 'I was saved by from .'); + + TestBed.configureTestingModule( + {declarations: [HeroComponent, VillianComponent, MainComponent]}); + const fixture = TestBed.createComponent(MainComponent); + expect(fixture.debugElement.nativeElement) + .toHaveText('I was saved by my hero from a villian.'); + }); + }); +} \ No newline at end of file diff --git a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts index ef0ebaec41f50b..062f53930edcd7 100644 --- a/modules/@angular/compiler/test/template_parser/template_parser_spec.ts +++ b/modules/@angular/compiler/test/template_parser/template_parser_spec.ts @@ -13,12 +13,12 @@ import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventA import {TEMPLATE_TRANSFORMS, TemplateParser, splitClasses} from '@angular/compiler/src/template_parser/template_parser'; import {MockSchemaRegistry} from '@angular/compiler/testing'; import {TEST_COMPILER_PROVIDERS} from '@angular/compiler/testing/test_bindings'; -import {SchemaMetadata, SecurityContext} from '@angular/core'; +import {SchemaMetadata, SecurityContext, Type} from '@angular/core'; import {Console} from '@angular/core/src/console'; import {TestBed} from '@angular/core/testing'; import {afterEach, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal'; -import {Identifiers, identifierToken} from '../../src/identifiers'; +import {Identifiers, identifierToken, resolveIdentifierToken} from '../../src/identifiers'; import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../src/ml_parser/interpolation_config'; import {unparse} from '../expression_parser/unparser'; @@ -44,12 +44,14 @@ export function main() { beforeEach(inject([TemplateParser], (parser: TemplateParser) => { var component = CompileDirectiveMetadata.create({ selector: 'root', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'Root'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'Root', runtime: {} as Type}), isComponent: true }); ngIf = CompileDirectiveMetadata.create({ selector: '[ngIf]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'NgIf'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'NgIf', runtime: {} as Type}), inputs: ['ngIf'] }); @@ -176,7 +178,8 @@ export function main() { inject([TemplateParser], (parser: TemplateParser) => { const component = CompileDirectiveMetadata.create({ selector: 'test', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'Test'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'Test', runtime: {} as Type}), isComponent: true, template: new CompileTemplateMetadata({interpolation: ['{%', '%}']}) }); @@ -305,7 +308,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), host: {'[@prop]': 'expr'} }); @@ -390,7 +394,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. var dirA = CompileDirectiveMetadata.create({ selector: 'template', outputs: ['e'], - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}) }); expect(humanizeTplAst(parse('', [dirA]))).toEqual([ [EmbeddedTemplateAst], @@ -426,15 +431,18 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. () => { var dirA = CompileDirectiveMetadata.create({ selector: '[a]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}) }); var dirB = CompileDirectiveMetadata.create({ selector: '[b]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirB'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirB', runtime: {} as Type}) }); var dirC = CompileDirectiveMetadata.create({ selector: '[c]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirC'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirC', runtime: {} as Type}) }); expect(humanizeTplAst(parse('
', [dirA, dirB, dirC]))).toEqual([ [ElementAst, 'div'], [AttrAst, 'a', ''], [AttrAst, 'c', ''], [AttrAst, 'b', ''], @@ -446,11 +454,13 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should locate directives in property bindings', () => { var dirA = CompileDirectiveMetadata.create({ selector: '[a=b]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}) }); var dirB = CompileDirectiveMetadata.create({ selector: '[b]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirB'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirB', runtime: {} as Type}) }); expect(humanizeTplAst(parse('
', [dirA, dirB]))).toEqual([ [ElementAst, 'div'], @@ -462,7 +472,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should locate directives in event bindings', () => { var dirA = CompileDirectiveMetadata.create({ selector: '[a]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirB'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirB', runtime: {} as Type}) }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -473,7 +484,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should parse directive host properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), host: {'[a]': 'expr'} }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -485,7 +497,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should parse directive host listeners', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), host: {'(a)': 'expr'} }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -496,7 +509,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should parse directive properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), inputs: ['aProp'] }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -508,7 +522,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should parse renamed directive properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), inputs: ['b:a'] }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -519,7 +534,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should parse literal directive properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), inputs: ['a'] }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -531,7 +547,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should favor explicit bound properties over literal properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), inputs: ['a'] }); expect(humanizeTplAst(parse('
', [dirA]))) @@ -544,7 +561,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should support optional directive properties', () => { var dirA = CompileDirectiveMetadata.create({ selector: 'div', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), inputs: ['a'] }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -560,9 +578,10 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. function createToken(value: string): CompileTokenMetadata { let token: CompileTokenMetadata; if (value.startsWith('type:')) { + const name = value.substring(5); token = new CompileTokenMetadata({ - identifier: - new CompileTypeMetadata({moduleUrl: someModuleUrl, name: value.substring(5)}) + identifier: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name, runtime: name as any as Type}) }); } else { token = new CompileTokenMetadata({value: value}); @@ -593,10 +612,11 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. function createProvider( token: string, {multi = false, deps = []}: {multi?: boolean, deps?: string[]} = {}): CompileProviderMetadata { + const name = `provider${nextProviderId++}`; return new CompileProviderMetadata({ token: createToken(token), multi: multi, - useClass: new CompileTypeMetadata({name: `provider${nextProviderId++}`}), + useClass: new CompileTypeMetadata({name, runtime: name as any as Type}), deps: deps.map(createDep) }); } @@ -611,8 +631,12 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. var isComponent = !selector.startsWith('['); return CompileDirectiveMetadata.create({ selector: selector, - type: new CompileTypeMetadata( - {moduleUrl: someModuleUrl, name: selector, diDeps: deps.map(createDep)}), + type: new CompileTypeMetadata({ + moduleUrl: someModuleUrl, + name: selector, + diDeps: deps.map(createDep), + runtime: selector as any as Type + }), isComponent: isComponent, template: new CompileTemplateMetadata({ngContentSelectors: []}), providers: providers, @@ -860,7 +884,8 @@ Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. it('should assign references to directives via exportAs', () => { var dirA = CompileDirectiveMetadata.create({ selector: '[a]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), exportAs: 'dirA' }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ @@ -904,7 +929,8 @@ Reference "#a" is defined several times ("
]#a>
var dirA = CompileDirectiveMetadata.create({ selector: '[a]', isComponent: true, - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}), + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}), exportAs: 'dirA', template: new CompileTemplateMetadata({ngContentSelectors: []}) }); @@ -919,7 +945,8 @@ Reference "#a" is defined several times ("
]#a>
it('should not locate directives in references', () => { var dirA = CompileDirectiveMetadata.create({ selector: '[a]', - type: new CompileTypeMetadata({moduleUrl: someModuleUrl, name: 'DirA'}) + type: new CompileTypeMetadata( + {moduleUrl: someModuleUrl, name: 'DirA', runtime: {} as Type}) }); expect(humanizeTplAst(parse('
', [dirA]))).toEqual([ [ElementAst, 'div'], [ReferenceAst, 'a', null] @@ -945,13 +972,15 @@ Reference "#a" is defined several times ("
]#a>
it('should support references via #...', () => { expect(humanizeTplAst(parse('