diff --git a/modules/@angular/core/src/reflection/reflection.ts b/modules/@angular/core/src/reflection/reflection.ts index 504dd060c4..6653c83a1c 100644 --- a/modules/@angular/core/src/reflection/reflection.ts +++ b/modules/@angular/core/src/reflection/reflection.ts @@ -16,4 +16,4 @@ export {ReflectionInfo, Reflector} from './reflector'; * The {@link Reflector} used internally in Angular to access metadata * about symbols. */ -export var reflector = new Reflector(new ReflectionCapabilities()); +export const reflector = new Reflector(new ReflectionCapabilities()); diff --git a/modules/@angular/core/src/reflection/reflection_capabilities.ts b/modules/@angular/core/src/reflection/reflection_capabilities.ts index e475ea190e..8374fbe1f6 100644 --- a/modules/@angular/core/src/reflection/reflection_capabilities.ts +++ b/modules/@angular/core/src/reflection/reflection_capabilities.ts @@ -19,19 +19,11 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { isReflectionEnabled(): boolean { return true; } - factory(t: Type): Function { - var prototype = t.prototype; - return function(...args: any[]) { - var instance = Object.create(prototype); - t.apply(instance, args); - return instance; - }; - } + factory(t: Type): (args: any[]) => T { return (...args: any[]) => new t(...args); } /** @internal */ - _zipTypesAndAnnotations( - paramTypes: any /** TODO #9100 */, paramAnnotations: any /** TODO #9100 */): any[][] { - var result: any /** TODO #9100 */; + _zipTypesAndAnnotations(paramTypes: any[], paramAnnotations: any[]): any[][] { + var result: any[][]; if (typeof paramTypes === 'undefined') { result = new Array(paramAnnotations.length); @@ -50,48 +42,45 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { } else { result[i] = []; } - if (isPresent(paramAnnotations) && isPresent(paramAnnotations[i])) { + if (paramAnnotations && isPresent(paramAnnotations[i])) { result[i] = result[i].concat(paramAnnotations[i]); } } return result; } - parameters(typeOrFunc: Type): any[][] { + parameters(type: Type): any[][] { // Prefer the direct API. - if (isPresent((typeOrFunc).parameters)) { - return (typeOrFunc).parameters; + if ((type).parameters) { + return (type).parameters; } // API of tsickle for lowering decorators to properties on the class. - if (isPresent((typeOrFunc).ctorParameters)) { - let ctorParameters = (typeOrFunc).ctorParameters; - let paramTypes = - ctorParameters.map((ctorParam: any /** TODO #9100 */) => ctorParam && ctorParam.type); - let paramAnnotations = ctorParameters.map( - (ctorParam: any /** TODO #9100 */) => + if ((type).ctorParameters) { + const ctorParameters = (type).ctorParameters; + const paramTypes = ctorParameters.map((ctorParam: any) => ctorParam && ctorParam.type); + const paramAnnotations = ctorParameters.map( + (ctorParam: any) => ctorParam && convertTsickleDecoratorIntoMetadata(ctorParam.decorators)); return this._zipTypesAndAnnotations(paramTypes, paramAnnotations); } // API for metadata created by invoking the decorators. if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) { - var paramAnnotations = this._reflect.getMetadata('parameters', typeOrFunc); - var paramTypes = this._reflect.getMetadata('design:paramtypes', typeOrFunc); - if (isPresent(paramTypes) || isPresent(paramAnnotations)) { + const paramAnnotations = this._reflect.getMetadata('parameters', type); + const paramTypes = this._reflect.getMetadata('design:paramtypes', type); + if (paramTypes || paramAnnotations) { return this._zipTypesAndAnnotations(paramTypes, paramAnnotations); } } // The array has to be filled with `undefined` because holes would be skipped by `some` - let parameters = new Array((typeOrFunc.length)); - parameters.fill(undefined); - return parameters; + return new Array((type.length)).fill(undefined); } annotations(typeOrFunc: Type): any[] { // Prefer the direct API. - if (isPresent((typeOrFunc).annotations)) { - var annotations = (typeOrFunc).annotations; + if ((typeOrFunc).annotations) { + let annotations = (typeOrFunc).annotations; if (isFunction(annotations) && annotations.annotations) { annotations = annotations.annotations; } @@ -99,22 +88,22 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { } // API of tsickle for lowering decorators to properties on the class. - if (isPresent((typeOrFunc).decorators)) { + if ((typeOrFunc).decorators) { return convertTsickleDecoratorIntoMetadata((typeOrFunc).decorators); } // API for metadata created by invoking the decorators. - if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) { - var annotations = this._reflect.getMetadata('annotations', typeOrFunc); - if (isPresent(annotations)) return annotations; + if (this._reflect && this._reflect.getMetadata) { + const annotations = this._reflect.getMetadata('annotations', typeOrFunc); + if (annotations) return annotations; } return []; } propMetadata(typeOrFunc: any): {[key: string]: any[]} { // Prefer the direct API. - if (isPresent((typeOrFunc).propMetadata)) { - var propMetadata = (typeOrFunc).propMetadata; + if ((typeOrFunc).propMetadata) { + let propMetadata = (typeOrFunc).propMetadata; if (isFunction(propMetadata) && propMetadata.propMetadata) { propMetadata = propMetadata.propMetadata; } @@ -122,9 +111,9 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { } // API of tsickle for lowering decorators to properties on the class. - if (isPresent((typeOrFunc).propDecorators)) { - let propDecorators = (typeOrFunc).propDecorators; - let propMetadata = <{[key: string]: any[]}>{}; + if ((typeOrFunc).propDecorators) { + const propDecorators = (typeOrFunc).propDecorators; + const propMetadata = <{[key: string]: any[]}>{}; Object.keys(propDecorators).forEach(prop => { propMetadata[prop] = convertTsickleDecoratorIntoMetadata(propDecorators[prop]); }); @@ -132,9 +121,9 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { } // API for metadata created by invoking the decorators. - if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) { - var propMetadata = this._reflect.getMetadata('propMetadata', typeOrFunc); - if (isPresent(propMetadata)) return propMetadata; + if (this._reflect && this._reflect.getMetadata) { + const propMetadata = this._reflect.getMetadata('propMetadata', typeOrFunc); + if (propMetadata) return propMetadata; } return {}; } @@ -147,7 +136,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { hasLifecycleHook(type: any, lcInterface: Type, lcProperty: string): boolean { if (!(type instanceof Type)) return false; - var proto = (type).prototype; + const proto = (type).prototype; return !!proto[lcProperty]; } @@ -158,7 +147,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { } method(name: string): MethodFn { - let functionBody = `if (!o.${name}) throw new Error('"${name}" is undefined'); + const functionBody = `if (!o.${name}) throw new Error('"${name}" is undefined'); return o.${name}.apply(o, args);`; return new Function('o', 'args', functionBody); } diff --git a/modules/@angular/core/src/reflection/reflector.ts b/modules/@angular/core/src/reflection/reflector.ts index cf593706b1..cb70798117 100644 --- a/modules/@angular/core/src/reflection/reflector.ts +++ b/modules/@angular/core/src/reflection/reflector.ts @@ -7,7 +7,6 @@ */ import {MapWrapper} from '../facade/collection'; -import {isPresent} from '../facade/lang'; import {Type} from '../type'; import {PlatformReflectionCapabilities} from './platform_reflection_capabilities'; import {ReflectorReader} from './reflector_reader'; @@ -60,10 +59,10 @@ export class Reflector extends ReflectorReader { * potential dead code. */ listUnusedKeys(): any[] { - if (this._usedKeys == null) { + if (!this._usedKeys) { throw new Error('Usage tracking is disabled'); } - var allTypes = MapWrapper.keys(this._injectableInfo); + const allTypes = MapWrapper.keys(this._injectableInfo); return allTypes.filter(key => !this._usedKeys.has(key)); } @@ -83,87 +82,73 @@ export class Reflector extends ReflectorReader { factory(type: Type): Function { if (this._containsReflectionInfo(type)) { - var res = this._getReflectionInfo(type).factory; - return isPresent(res) ? res : null; - } else { - return this.reflectionCapabilities.factory(type); + return this._getReflectionInfo(type).factory || null; } + + return this.reflectionCapabilities.factory(type); } parameters(typeOrFunc: Type): any[][] { if (this._injectableInfo.has(typeOrFunc)) { - var res = this._getReflectionInfo(typeOrFunc).parameters; - return isPresent(res) ? res : []; - } else { - return this.reflectionCapabilities.parameters(typeOrFunc); + return this._getReflectionInfo(typeOrFunc).parameters || []; } + + return this.reflectionCapabilities.parameters(typeOrFunc); } annotations(typeOrFunc: Type): any[] { if (this._injectableInfo.has(typeOrFunc)) { - var res = this._getReflectionInfo(typeOrFunc).annotations; - return isPresent(res) ? res : []; - } else { - return this.reflectionCapabilities.annotations(typeOrFunc); + return this._getReflectionInfo(typeOrFunc).annotations || []; } + + return this.reflectionCapabilities.annotations(typeOrFunc); } propMetadata(typeOrFunc: Type): {[key: string]: any[]} { if (this._injectableInfo.has(typeOrFunc)) { - var res = this._getReflectionInfo(typeOrFunc).propMetadata; - return isPresent(res) ? res : {}; - } else { - return this.reflectionCapabilities.propMetadata(typeOrFunc); + return this._getReflectionInfo(typeOrFunc).propMetadata || {}; } + + return this.reflectionCapabilities.propMetadata(typeOrFunc); } interfaces(type: Type): any[] { if (this._injectableInfo.has(type)) { - var res = this._getReflectionInfo(type).interfaces; - return isPresent(res) ? res : []; - } else { - return this.reflectionCapabilities.interfaces(type); + return this._getReflectionInfo(type).interfaces || []; } + + return this.reflectionCapabilities.interfaces(type); } hasLifecycleHook(type: any, lcInterface: Type, lcProperty: string): boolean { - var interfaces = this.interfaces(type); - if (interfaces.indexOf(lcInterface) !== -1) { + if (this.interfaces(type).indexOf(lcInterface) !== -1) { return true; - } else { - return this.reflectionCapabilities.hasLifecycleHook(type, lcInterface, lcProperty); } + + return this.reflectionCapabilities.hasLifecycleHook(type, lcInterface, lcProperty); } getter(name: string): GetterFn { - if (this._getters.has(name)) { - return this._getters.get(name); - } else { - return this.reflectionCapabilities.getter(name); - } + return this._getters.has(name) ? this._getters.get(name) : + this.reflectionCapabilities.getter(name); } setter(name: string): SetterFn { - if (this._setters.has(name)) { - return this._setters.get(name); - } else { - return this.reflectionCapabilities.setter(name); - } + return this._setters.has(name) ? this._setters.get(name) : + this.reflectionCapabilities.setter(name); } method(name: string): MethodFn { - if (this._methods.has(name)) { - return this._methods.get(name); - } else { - return this.reflectionCapabilities.method(name); - } + return this._methods.has(name) ? this._methods.get(name) : + this.reflectionCapabilities.method(name); } /** @internal */ _getReflectionInfo(typeOrFunc: any): ReflectionInfo { - if (isPresent(this._usedKeys)) { + if (this._usedKeys) { this._usedKeys.add(typeOrFunc); } + return this._injectableInfo.get(typeOrFunc); }