refactor(decorators): cleanup

This commit is contained in:
Victor Berchet 2016-07-16 11:05:44 -07:00
parent 8cd97c2054
commit b6746cce9c
3 changed files with 46 additions and 57 deletions

View File

@ -86,9 +86,7 @@ export var LIFECYCLE_HOOKS_VALUES = [
* ``` * ```
* @stable * @stable
*/ */
export abstract class OnChanges { export abstract class OnChanges { abstract ngOnChanges(changes: SimpleChanges): void; }
abstract ngOnChanges(changes: SimpleChanges): any /** TODO #9100 */;
}
/** /**
* Implement this interface to execute custom initialization logic after your directive's * Implement this interface to execute custom initialization logic after your directive's
@ -132,7 +130,7 @@ export abstract class OnChanges {
* ``` * ```
* @stable * @stable
*/ */
export abstract class OnInit { abstract ngOnInit(): any /** TODO #9100 */; } export abstract class OnInit { abstract ngOnInit(): void; }
/** /**
* Implement this interface to supplement the default change detection algorithm in your directive. * Implement this interface to supplement the default change detection algorithm in your directive.
@ -201,7 +199,7 @@ export abstract class OnInit { abstract ngOnInit(): any /** TODO #9100 */; }
* ``` * ```
* @stable * @stable
*/ */
export abstract class DoCheck { abstract ngDoCheck(): any /** TODO #9100 */; } export abstract class DoCheck { abstract ngDoCheck(): void; }
/** /**
* Implement this interface to get notified when your directive is destroyed. * Implement this interface to get notified when your directive is destroyed.
@ -293,7 +291,7 @@ export abstract class DoCheck { abstract ngDoCheck(): any /** TODO #9100 */; }
* *
* @stable * @stable
*/ */
export abstract class OnDestroy { abstract ngOnDestroy(): any /** TODO #9100 */; } export abstract class OnDestroy { abstract ngOnDestroy(): void; }
/** /**
* Implement this interface to get notified when your directive's content has been fully * Implement this interface to get notified when your directive's content has been fully
@ -347,7 +345,7 @@ export abstract class OnDestroy { abstract ngOnDestroy(): any /** TODO #9100 */;
* ``` * ```
* @stable * @stable
*/ */
export abstract class AfterContentInit { abstract ngAfterContentInit(): any /** TODO #9100 */; } export abstract class AfterContentInit { abstract ngAfterContentInit(): void; }
/** /**
* Implement this interface to get notified after every check of your directive's content. * Implement this interface to get notified after every check of your directive's content.
@ -396,9 +394,7 @@ export abstract class AfterContentInit { abstract ngAfterContentInit(): any /**
* ``` * ```
* @stable * @stable
*/ */
export abstract class AfterContentChecked { export abstract class AfterContentChecked { abstract ngAfterContentChecked(): void; }
abstract ngAfterContentChecked(): any /** TODO #9100 */;
}
/** /**
* Implement this interface to get notified when your component's view has been fully initialized. * Implement this interface to get notified when your component's view has been fully initialized.
@ -446,7 +442,7 @@ export abstract class AfterContentChecked {
* ``` * ```
* @stable * @stable
*/ */
export abstract class AfterViewInit { abstract ngAfterViewInit(): any /** TODO #9100 */; } export abstract class AfterViewInit { abstract ngAfterViewInit(): void; }
/** /**
* Implement this interface to get notified after every check of your component's view. * Implement this interface to get notified after every check of your component's view.
@ -497,4 +493,4 @@ export abstract class AfterViewInit { abstract ngAfterViewInit(): any /** TODO #
* ``` * ```
* @stable * @stable
*/ */
export abstract class AfterViewChecked { abstract ngAfterViewChecked(): any /** TODO #9100 */; } export abstract class AfterViewChecked { abstract ngAfterViewChecked(): void; }

View File

@ -103,24 +103,24 @@ function applyParams(fnOrArray: (Function | any[]), key: string): Function {
if (isFunction(fnOrArray)) { if (isFunction(fnOrArray)) {
return <Function>fnOrArray; return <Function>fnOrArray;
} else if (fnOrArray instanceof Array) { } else if (fnOrArray instanceof Array) {
var annotations: any[] = fnOrArray; const annotations: any[] = fnOrArray;
var fn: Function = fnOrArray[fnOrArray.length - 1]; const annoLength = annotations.length - 1;
const fn: Function = fnOrArray[annoLength];
if (!isFunction(fn)) { if (!isFunction(fn)) {
throw new Error( throw new Error(
`Last position of Class method array must be Function in key ${key} was '${stringify(fn)}'`); `Last position of Class method array must be Function in key ${key} was '${stringify(fn)}'`);
} }
var annoLength = annotations.length - 1;
if (annoLength != fn.length) { if (annoLength != fn.length) {
throw new Error( throw new Error(
`Number of annotations (${annoLength}) does not match number of arguments (${fn.length}) in the function: ${stringify(fn)}`); `Number of annotations (${annoLength}) does not match number of arguments (${fn.length}) in the function: ${stringify(fn)}`);
} }
var paramsAnnotations: any[][] = []; const paramsAnnotations: any[][] = [];
for (var i = 0, ii = annotations.length - 1; i < ii; i++) { for (let i = 0, ii = annotations.length - 1; i < ii; i++) {
var paramAnnotations: any[] = []; const paramAnnotations: any[] = [];
paramsAnnotations.push(paramAnnotations); paramsAnnotations.push(paramAnnotations);
var annotation = annotations[i]; const annotation = annotations[i];
if (annotation instanceof Array) { if (annotation instanceof Array) {
for (var j = 0; j < annotation.length; j++) { for (let j = 0; j < annotation.length; j++) {
paramAnnotations.push(extractAnnotation(annotation[j])); paramAnnotations.push(extractAnnotation(annotation[j]));
} }
} else if (isFunction(annotation)) { } else if (isFunction(annotation)) {
@ -220,9 +220,9 @@ function applyParams(fnOrArray: (Function | any[]), key: string): Function {
* @stable * @stable
*/ */
export function Class(clsDef: ClassDefinition): ConcreteType<any> { export function Class(clsDef: ClassDefinition): ConcreteType<any> {
var constructor = applyParams( const constructor = applyParams(
clsDef.hasOwnProperty('constructor') ? clsDef.constructor : undefined, 'constructor'); clsDef.hasOwnProperty('constructor') ? clsDef.constructor : undefined, 'constructor');
var proto = constructor.prototype; let proto = constructor.prototype;
if (clsDef.hasOwnProperty('extends')) { if (clsDef.hasOwnProperty('extends')) {
if (isFunction(clsDef.extends)) { if (isFunction(clsDef.extends)) {
(<Function>constructor).prototype = proto = (<Function>constructor).prototype = proto =
@ -232,7 +232,7 @@ export function Class(clsDef: ClassDefinition): ConcreteType<any> {
`Class definition 'extends' property must be a constructor function was: ${stringify(clsDef.extends)}`); `Class definition 'extends' property must be a constructor function was: ${stringify(clsDef.extends)}`);
} }
} }
for (var key in clsDef) { for (let key in clsDef) {
if (key != 'extends' && key != 'prototype' && clsDef.hasOwnProperty(key)) { if (key != 'extends' && key != 'prototype' && clsDef.hasOwnProperty(key)) {
proto[key] = applyParams(<any>clsDef[key], key); proto[key] = applyParams(<any>clsDef[key], key);
} }
@ -243,7 +243,7 @@ export function Class(clsDef: ClassDefinition): ConcreteType<any> {
} }
if (!constructor['name']) { if (!constructor['name']) {
(constructor as any /** TODO #9100 */)['overriddenName'] = `class${_nextClassId++}`; (constructor as any)['overriddenName'] = `class${_nextClassId++}`;
} }
return <ConcreteType<any>>constructor; return <ConcreteType<any>>constructor;
@ -258,25 +258,22 @@ var Reflect = global.Reflect;
} }
})(); })();
export function makeDecorator( export function makeDecorator(annotationCls: any, chainFn: (fn: Function) => void = null):
annotationCls: any /* TODO #9100 */, (...args: any[]) => (cls: any) => any {
chainFn: (fn: Function) => void = null): (...args: any[]) => (cls: any) => any { function DecoratorFactory(objOrType: any): (cls: any) => any {
function DecoratorFactory(objOrType: any /** TODO #9100 */): (cls: any) => any { const annotationInstance = new (<any>annotationCls)(objOrType);
var annotationInstance = new (<any>annotationCls)(objOrType);
if (this instanceof annotationCls) { if (this instanceof annotationCls) {
return annotationInstance; return annotationInstance;
} else { } else {
var chainAnnotation = const chainAnnotation =
isFunction(this) && this.annotations instanceof Array ? this.annotations : []; isFunction(this) && this.annotations instanceof Array ? this.annotations : [];
chainAnnotation.push(annotationInstance); chainAnnotation.push(annotationInstance);
var TypeDecorator: TypeDecorator = const TypeDecorator: TypeDecorator = <TypeDecorator>function TypeDecorator(cls: Type) {
<TypeDecorator>function TypeDecorator(cls: any /** TODO #9100 */) { const annotations = Reflect.getOwnMetadata('annotations', cls) || [];
var annotations = Reflect.getOwnMetadata('annotations', cls); annotations.push(annotationInstance);
annotations = annotations || []; Reflect.defineMetadata('annotations', annotations, cls);
annotations.push(annotationInstance); return cls;
Reflect.defineMetadata('annotations', annotations, cls); };
return cls;
};
TypeDecorator.annotations = chainAnnotation; TypeDecorator.annotations = chainAnnotation;
TypeDecorator.Class = Class; TypeDecorator.Class = Class;
if (chainFn) chainFn(TypeDecorator); if (chainFn) chainFn(TypeDecorator);
@ -288,8 +285,8 @@ export function makeDecorator(
return DecoratorFactory; return DecoratorFactory;
} }
export function makeParamDecorator(annotationCls: any /** TODO #9100 */): any { export function makeParamDecorator(annotationCls: any): any {
function ParamDecoratorFactory(...args: any[] /** TODO #9100 */): any { function ParamDecoratorFactory(...args: any[]): any {
var annotationInstance = Object.create(annotationCls.prototype); var annotationInstance = Object.create(annotationCls.prototype);
annotationCls.apply(annotationInstance, args); annotationCls.apply(annotationInstance, args);
if (this instanceof annotationCls) { if (this instanceof annotationCls) {
@ -300,11 +297,8 @@ export function makeParamDecorator(annotationCls: any /** TODO #9100 */): any {
} }
function ParamDecorator( function ParamDecorator(cls: any, unusedKey: any, index: number): any {
cls: any /** TODO #9100 */, unusedKey: any /** TODO #9100 */, const parameters: any[][] = Reflect.getMetadata('parameters', cls) || [];
index: any /** TODO #9100 */): any {
var parameters: any[][] = Reflect.getMetadata('parameters', cls);
parameters = parameters || [];
// there might be gaps if some in between parameters do not have annotations. // there might be gaps if some in between parameters do not have annotations.
// we pad with nulls. // we pad with nulls.
@ -325,8 +319,8 @@ export function makeParamDecorator(annotationCls: any /** TODO #9100 */): any {
return ParamDecoratorFactory; return ParamDecoratorFactory;
} }
export function makePropDecorator(annotationCls: any /** TODO #9100 */): any { export function makePropDecorator(annotationCls: any): any {
function PropDecoratorFactory(...args: any[] /** TODO #9100 */): any { function PropDecoratorFactory(...args: any[]): any {
var decoratorInstance = Object.create(annotationCls.prototype); var decoratorInstance = Object.create(annotationCls.prototype);
annotationCls.apply(decoratorInstance, args); annotationCls.apply(decoratorInstance, args);
@ -334,8 +328,7 @@ export function makePropDecorator(annotationCls: any /** TODO #9100 */): any {
return decoratorInstance; return decoratorInstance;
} else { } else {
return function PropDecorator(target: any, name: string) { return function PropDecorator(target: any, name: string) {
var meta = Reflect.getOwnMetadata('propMetadata', target.constructor); const meta = Reflect.getOwnMetadata('propMetadata', target.constructor) || {};
meta = meta || {};
meta[name] = meta[name] || []; meta[name] = meta[name] || [];
meta[name].unshift(decoratorInstance); meta[name].unshift(decoratorInstance);
Reflect.defineMetadata('propMetadata', meta, target.constructor); Reflect.defineMetadata('propMetadata', meta, target.constructor);

View File

@ -7,22 +7,22 @@ export declare class AbstractProviderError extends BaseException {
/** @stable */ /** @stable */
export declare abstract class AfterContentChecked { export declare abstract class AfterContentChecked {
abstract ngAfterContentChecked(): any; abstract ngAfterContentChecked(): void;
} }
/** @stable */ /** @stable */
export declare abstract class AfterContentInit { export declare abstract class AfterContentInit {
abstract ngAfterContentInit(): any; abstract ngAfterContentInit(): void;
} }
/** @stable */ /** @stable */
export declare abstract class AfterViewChecked { export declare abstract class AfterViewChecked {
abstract ngAfterViewChecked(): any; abstract ngAfterViewChecked(): void;
} }
/** @stable */ /** @stable */
export declare abstract class AfterViewInit { export declare abstract class AfterViewInit {
abstract ngAfterViewInit(): any; abstract ngAfterViewInit(): void;
} }
/** @experimental */ /** @experimental */
@ -686,7 +686,7 @@ export declare function disposePlatform(): void;
/** @stable */ /** @stable */
export declare abstract class DoCheck { export declare abstract class DoCheck {
abstract ngDoCheck(): any; abstract ngDoCheck(): void;
} }
/** @deprecated */ /** @deprecated */
@ -972,17 +972,17 @@ export declare class NoProviderError extends AbstractProviderError {
/** @stable */ /** @stable */
export declare abstract class OnChanges { export declare abstract class OnChanges {
abstract ngOnChanges(changes: SimpleChanges): any; abstract ngOnChanges(changes: SimpleChanges): void;
} }
/** @stable */ /** @stable */
export declare abstract class OnDestroy { export declare abstract class OnDestroy {
abstract ngOnDestroy(): any; abstract ngOnDestroy(): void;
} }
/** @stable */ /** @stable */
export declare abstract class OnInit { export declare abstract class OnInit {
abstract ngOnInit(): any; abstract ngOnInit(): void;
} }
/** @stable */ /** @stable */