From f23b22a0f48b6fd832694423bf23017a1241d630 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 20 Sep 2016 14:14:57 -0700 Subject: [PATCH] refactor: misc cleanup --- .../compiler/src/directive_normalizer.ts | 23 ++++----- .../@angular/core/src/application_module.ts | 1 - modules/@angular/core/src/application_ref.ts | 50 +++++++++--------- .../@angular/core/src/application_tokens.ts | 2 +- .../@angular/core/src/linker/view_utils.ts | 1 - modules/@angular/core/src/metadata.ts | 2 - modules/@angular/core/src/metadata/view.ts | 51 ++++--------------- .../core/src/platform_core_providers.ts | 9 ++-- modules/@angular/core/src/type.ts | 2 +- tools/public_api_guard/core/index.d.ts | 2 +- 10 files changed, 55 insertions(+), 88 deletions(-) diff --git a/modules/@angular/compiler/src/directive_normalizer.ts b/modules/@angular/compiler/src/directive_normalizer.ts index a8e067d89c..729b55f372 100644 --- a/modules/@angular/compiler/src/directive_normalizer.ts +++ b/modules/@angular/compiler/src/directive_normalizer.ts @@ -115,27 +115,27 @@ export class DirectiveNormalizer { const templateStyles = this.normalizeStylesheet(new CompileStylesheetMetadata( {styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl})); - const allStyles = templateMetadataStyles.styles.concat(templateStyles.styles); - const allStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls); - let encapsulation = templateMeta.encapsulation; if (isBlank(encapsulation)) { encapsulation = this._config.defaultEncapsulation; } - if (encapsulation === ViewEncapsulation.Emulated && allStyles.length === 0 && - allStyleUrls.length === 0) { + + const styles = templateMetadataStyles.styles.concat(templateStyles.styles); + const styleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls); + + if (encapsulation === ViewEncapsulation.Emulated && styles.length === 0 && + styleUrls.length === 0) { encapsulation = ViewEncapsulation.None; } + return new CompileTemplateMetadata({ encapsulation, - template: template, - templateUrl: templateAbsUrl, - styles: allStyles, - styleUrls: allStyleUrls, + template, + templateUrl: templateAbsUrl, styles, styleUrls, externalStylesheets: templateMeta.externalStylesheets, ngContentSelectors: visitor.ngContentSelectors, animations: templateMeta.animations, - interpolation: templateMeta.interpolation + interpolation: templateMeta.interpolation, }); } @@ -251,7 +251,6 @@ function _cloneDirectiveWithTemplate( viewProviders: directive.viewProviders, queries: directive.queries, viewQueries: directive.viewQueries, - entryComponents: directive.entryComponents, - template: template + entryComponents: directive.entryComponents, template, }); } diff --git a/modules/@angular/core/src/application_module.ts b/modules/@angular/core/src/application_module.ts index 123b716043..507e105b04 100644 --- a/modules/@angular/core/src/application_module.ts +++ b/modules/@angular/core/src/application_module.ts @@ -14,7 +14,6 @@ import {LOCALE_ID} from './i18n/tokens'; import {Compiler} from './linker/compiler'; import {ViewUtils} from './linker/view_utils'; import {NgModule} from './metadata'; -import {Type} from './type'; export function _iterableDiffersFactory() { return defaultIterableDiffers; diff --git a/modules/@angular/core/src/application_ref.ts b/modules/@angular/core/src/application_ref.ts index c1ac48631d..8a62db4928 100644 --- a/modules/@angular/core/src/application_ref.ts +++ b/modules/@angular/core/src/application_ref.ts @@ -9,7 +9,7 @@ import {ErrorHandler} from '../src/error_handler'; import {ListWrapper} from '../src/facade/collection'; import {unimplemented} from '../src/facade/errors'; -import {isBlank, isPresent, stringify} from '../src/facade/lang'; +import {stringify} from '../src/facade/lang'; import {isPromise} from '../src/util/lang'; import {ApplicationInitStatus} from './application_init'; @@ -26,9 +26,9 @@ import {Testability, TestabilityRegistry} from './testability/testability'; import {Type} from './type'; import {NgZone} from './zone/ng_zone'; -var _devMode: boolean = true; -var _runModeLocked: boolean = false; -var _platform: PlatformRef; +let _devMode: boolean = true; +let _runModeLocked: boolean = false; +let _platform: PlatformRef; /** * Disable Angular's development mode, which turns off assertions and other @@ -67,13 +67,13 @@ export function isDevMode(): boolean { * @experimental APIs related to application bootstrap are currently under review. */ export function createPlatform(injector: Injector): PlatformRef { - if (isPresent(_platform) && !_platform.destroyed) { + if (_platform && !_platform.destroyed) { throw new Error( 'There can be only one platform. Destroy the previous one to create a new one.'); } _platform = injector.get(PlatformRef); const inits: Function[] = injector.get(PLATFORM_INITIALIZER, null); - if (isPresent(inits)) inits.forEach(init => init()); + if (inits) inits.forEach(init => init()); return _platform; } @@ -107,14 +107,17 @@ export function createPlatformFactory( * @experimental APIs related to application bootstrap are currently under review. */ export function assertPlatform(requiredToken: any): PlatformRef { - var platform = getPlatform(); - if (isBlank(platform)) { + const platform = getPlatform(); + + if (!platform) { throw new Error('No platform exists!'); } - if (isPresent(platform) && isBlank(platform.injector.get(requiredToken, null))) { + + if (!platform.injector.get(requiredToken, null)) { throw new Error( 'A platform with a different configuration has been created. Please destroy it first.'); } + return platform; } @@ -124,7 +127,7 @@ export function assertPlatform(requiredToken: any): PlatformRef { * @experimental APIs related to application bootstrap are currently under review. */ export function destroyPlatform(): void { - if (isPresent(_platform) && !_platform.destroyed) { + if (_platform && !_platform.destroyed) { _platform.destroy(); } } @@ -135,7 +138,7 @@ export function destroyPlatform(): void { * @experimental APIs related to application bootstrap are currently under review. */ export function getPlatform(): PlatformRef { - return isPresent(_platform) && !_platform.destroyed ? _platform : null; + return _platform && !_platform.destroyed ? _platform : null; } /** @@ -224,9 +227,9 @@ function _callAndReportToErrorHandler(errorHandler: ErrorHandler, callback: () = // rethrow as the exception handler might not do it throw e; }); - } else { - return result; } + + return result; } catch (e) { errorHandler.handleError(e); // rethrow as the exception handler might not do it @@ -238,7 +241,6 @@ function _callAndReportToErrorHandler(errorHandler: ErrorHandler, callback: () = export class PlatformRef_ extends PlatformRef { private _modules: NgModuleRef[] = []; private _destroyListeners: Function[] = []; - private _destroyed: boolean = false; constructor(private _injector: Injector) { super(); } @@ -253,8 +255,8 @@ export class PlatformRef_ extends PlatformRef { if (this._destroyed) { throw new Error('The platform has already been destroyed!'); } - ListWrapper.clone(this._modules).forEach((app) => app.destroy()); - this._destroyListeners.forEach((dispose) => dispose()); + this._modules.slice().forEach(module => module.destroy()); + this._destroyListeners.forEach(listener => listener()); this._destroyed = true; } @@ -301,7 +303,7 @@ export class PlatformRef_ extends PlatformRef { componentFactoryCallback?: any): Promise> { const compilerFactory: CompilerFactory = this.injector.get(CompilerFactory); const compiler = compilerFactory.createCompiler( - compilerOptions instanceof Array ? compilerOptions : [compilerOptions]); + Array.isArray(compilerOptions) ? compilerOptions : [compilerOptions]); // ugly internal api hack: generate host component factories for all declared components and // pass the factories into the callback - this is used by UpdateAdapter to get hold of all @@ -424,10 +426,10 @@ export class ApplicationRef_ extends ApplicationRef { componentFactory = this._componentFactoryResolver.resolveComponentFactory(componentOrFactory); } this._rootComponentTypes.push(componentFactory.componentType); - var compRef = componentFactory.create(this._injector, [], componentFactory.selector); + const compRef = componentFactory.create(this._injector, [], componentFactory.selector); compRef.onDestroy(() => { this._unloadComponent(compRef); }); - var testability = compRef.injector.get(Testability, null); - if (isPresent(testability)) { + const testability = compRef.injector.get(Testability, null); + if (testability) { compRef.injector.get(TestabilityRegistry) .registerApplication(compRef.location.nativeElement, testability); } @@ -454,7 +456,7 @@ export class ApplicationRef_ extends ApplicationRef { /** @internal */ _unloadComponent(componentRef: ComponentRef): void { - if (!ListWrapper.contains(this._rootComponents, componentRef)) { + if (this._rootComponents.indexOf(componentRef) == -1) { return; } this.unregisterChangeDetector(componentRef.changeDetectorRef); @@ -466,7 +468,7 @@ export class ApplicationRef_ extends ApplicationRef { throw new Error('ApplicationRef.tick is called recursively'); } - var s = ApplicationRef_._tickScope(); + const scope = ApplicationRef_._tickScope(); try { this._runningTick = true; this._changeDetectorRefs.forEach((detector) => detector.detectChanges()); @@ -475,13 +477,13 @@ export class ApplicationRef_ extends ApplicationRef { } } finally { this._runningTick = false; - wtfLeave(s); + wtfLeave(scope); } } ngOnDestroy() { // TODO(alxhub): Dispose of the NgZone. - ListWrapper.clone(this._rootComponents).forEach((ref) => ref.destroy()); + this._rootComponents.slice().forEach((component) => component.destroy()); } get componentTypes(): Type[] { return this._rootComponentTypes; } diff --git a/modules/@angular/core/src/application_tokens.ts b/modules/@angular/core/src/application_tokens.ts index 8b8a0f5ac9..12a7be59ce 100644 --- a/modules/@angular/core/src/application_tokens.ts +++ b/modules/@angular/core/src/application_tokens.ts @@ -34,7 +34,7 @@ export function _appIdRandomProviderFactory() { export const APP_ID_RANDOM_PROVIDER = { provide: APP_ID, useFactory: _appIdRandomProviderFactory, - deps: [] + deps: [], }; function _randomChar(): string { diff --git a/modules/@angular/core/src/linker/view_utils.ts b/modules/@angular/core/src/linker/view_utils.ts index 520039ab06..c10b575a5a 100644 --- a/modules/@angular/core/src/linker/view_utils.ts +++ b/modules/@angular/core/src/linker/view_utils.ts @@ -10,7 +10,6 @@ import {APP_ID} from '../application_tokens'; import {devModeEqual} from '../change_detection/change_detection'; import {UNINITIALIZED} from '../change_detection/change_detection_util'; import {Inject, Injectable} from '../di'; -import {ListWrapper} from '../facade/collection'; import {isBlank, isPresent, looseIdentical} from '../facade/lang'; import {ViewEncapsulation} from '../metadata/view'; import {RenderComponentType, Renderer, RootRenderer} from '../render/api'; diff --git a/modules/@angular/core/src/metadata.ts b/modules/@angular/core/src/metadata.ts index f8c4ca9a2a..e606c12fa4 100644 --- a/modules/@angular/core/src/metadata.ts +++ b/modules/@angular/core/src/metadata.ts @@ -15,8 +15,6 @@ import {Attribute, ContentChild, ContentChildren, Query, ViewChild, ViewChildren import {Component, Directive, HostBinding, HostListener, Input, Output, Pipe} from './metadata/directives'; import {ModuleWithProviders, NgModule, SchemaMetadata} from './metadata/ng_module'; import {ViewEncapsulation} from './metadata/view'; -import {Type} from './type'; -import {TypeDecorator, makeParamDecorator, makePropDecorator} from './util/decorators'; export {ANALYZE_FOR_ENTRY_COMPONENTS, Attribute, ContentChild, ContentChildDecorator, ContentChildren, ContentChildrenDecorator, Query, ViewChild, ViewChildDecorator, ViewChildren, ViewChildrenDecorator} from './metadata/di'; export {Component, ComponentDecorator, Directive, DirectiveDecorator, HostBinding, HostListener, Input, Output, Pipe} from './metadata/directives'; diff --git a/modules/@angular/core/src/metadata/view.ts b/modules/@angular/core/src/metadata/view.ts index f917112eac..c1e28b8ddd 100644 --- a/modules/@angular/core/src/metadata/view.ts +++ b/modules/@angular/core/src/metadata/view.ts @@ -7,8 +7,6 @@ */ import {AnimationEntryMetadata} from '../animation/metadata'; -import {Type} from '../type'; - /** * Defines template and style encapsulation options available for Component's {@link Component}. @@ -46,13 +44,6 @@ export var VIEW_ENCAPSULATION_VALUES = /** * Metadata properties available for configuring Views. * - * Each Angular component requires a single `@Component` and at least one `@View` annotation. The - * `@View` annotation specifies the HTML template to use, and lists the directives that are active - * within the template. - * - * When a component is instantiated, the template is loaded into the component's shadow root, and - * the expressions and statements in the template are evaluated against the component. - * * For details on the `@Component` annotation, see {@link Component}. * * ### Example @@ -61,7 +52,6 @@ export var VIEW_ENCAPSULATION_VALUES = * @Component({ * selector: 'greet', * template: 'Hello {{name}}!', - * directives: [GreetUser, Bold] * }) * class Greet { * name: string; @@ -73,46 +63,23 @@ export var VIEW_ENCAPSULATION_VALUES = * ``` * * @deprecated Use Component instead. + * + * {@link Component} */ export class ViewMetadata { - /** - * Specifies a template URL for an Angular component. - * - * NOTE: Only one of `templateUrl` or `template` can be defined per View. - * - * - */ + /** {@link Component.templateUrl} */ templateUrl: string; - - /** - * Specifies an inline template for an Angular component. - * - * NOTE: Only one of `templateUrl` or `template` can be defined per View. - */ + /** {@link Component.template} */ template: string; - - /** - * Specifies stylesheet URLs for an Angular component. - * - * - */ + /** {@link Component.stylesUrl} */ styleUrls: string[]; - - /** - * Specifies an inline stylesheet for an Angular component. - */ + /** {@link Component.styles} */ styles: string[]; - - /** - * Specify how the template and the styles should be encapsulated. - * The default is {@link ViewEncapsulation#Emulated `ViewEncapsulation.Emulated`} if the view - * has styles, - * otherwise {@link ViewEncapsulation#None `ViewEncapsulation.None`}. - */ + /** {@link Component.encapsulation} */ encapsulation: ViewEncapsulation; - + /** {@link Component.animation} */ animations: AnimationEntryMetadata[]; - + /** {@link Component.interpolation} */ interpolation: [string, string]; constructor( diff --git a/modules/@angular/core/src/platform_core_providers.ts b/modules/@angular/core/src/platform_core_providers.ts index 5b977201c7..e5a71a0154 100644 --- a/modules/@angular/core/src/platform_core_providers.ts +++ b/modules/@angular/core/src/platform_core_providers.ts @@ -8,7 +8,7 @@ import {PlatformRef, PlatformRef_, createPlatformFactory} from './application_ref'; import {Console} from './console'; -import {ClassProvider, ExistingProvider, FactoryProvider, Provider, TypeProvider, ValueProvider} from './di'; +import {Provider} from './di'; import {Reflector, reflector} from './reflection/reflection'; import {ReflectorReader} from './reflection/reflector_reader'; import {TestabilityRegistry} from './testability/testability'; @@ -18,9 +18,12 @@ function _reflector(): Reflector { } const _CORE_PLATFORM_PROVIDERS: Provider[] = [ - PlatformRef_, {provide: PlatformRef, useExisting: PlatformRef_}, + PlatformRef_, + {provide: PlatformRef, useExisting: PlatformRef_}, {provide: Reflector, useFactory: _reflector, deps: []}, - {provide: ReflectorReader, useExisting: Reflector}, TestabilityRegistry, Console + {provide: ReflectorReader, useExisting: Reflector}, + TestabilityRegistry, + Console, ]; /** diff --git a/modules/@angular/core/src/type.ts b/modules/@angular/core/src/type.ts index 2ea9323a05..21e6e29fad 100644 --- a/modules/@angular/core/src/type.ts +++ b/modules/@angular/core/src/type.ts @@ -16,7 +16,7 @@ * * @stable */ -export var Type = Function; +export const Type = Function; export interface Type extends Function { new (...args: any[]): T; } diff --git a/tools/public_api_guard/core/index.d.ts b/tools/public_api_guard/core/index.d.ts index d798eb2daf..49f6ed19fc 100644 --- a/tools/public_api_guard/core/index.d.ts +++ b/tools/public_api_guard/core/index.d.ts @@ -916,7 +916,7 @@ export declare const TRANSLATIONS_FORMAT: OpaqueToken; export declare function trigger(name: string, animation: AnimationMetadata[]): AnimationEntryMetadata; /** @stable */ -export declare var Type: FunctionConstructor; +export declare const Type: FunctionConstructor; /** @stable */ export interface TypeDecorator {