refactor(core): simplify & cleanup reflection
This commit is contained in:
parent
27d76776b8
commit
f7db0668d1
|
@ -8,33 +8,51 @@
|
||||||
|
|
||||||
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core';
|
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core';
|
||||||
|
|
||||||
import {MapWrapper} from './facade/collection';
|
|
||||||
import {LifecycleHooks, reflector} from './private_import_core';
|
import {LifecycleHooks, reflector} from './private_import_core';
|
||||||
|
|
||||||
const LIFECYCLE_INTERFACES: Map<any, Type<any>> = MapWrapper.createFromPairs([
|
|
||||||
[LifecycleHooks.OnInit, OnInit],
|
|
||||||
[LifecycleHooks.OnDestroy, OnDestroy],
|
|
||||||
[LifecycleHooks.DoCheck, DoCheck],
|
|
||||||
[LifecycleHooks.OnChanges, OnChanges],
|
|
||||||
[LifecycleHooks.AfterContentInit, AfterContentInit],
|
|
||||||
[LifecycleHooks.AfterContentChecked, AfterContentChecked],
|
|
||||||
[LifecycleHooks.AfterViewInit, AfterViewInit],
|
|
||||||
[LifecycleHooks.AfterViewChecked, AfterViewChecked],
|
|
||||||
]);
|
|
||||||
|
|
||||||
const LIFECYCLE_PROPS: Map<any, string> = MapWrapper.createFromPairs([
|
|
||||||
[LifecycleHooks.OnInit, 'ngOnInit'],
|
|
||||||
[LifecycleHooks.OnDestroy, 'ngOnDestroy'],
|
|
||||||
[LifecycleHooks.DoCheck, 'ngDoCheck'],
|
|
||||||
[LifecycleHooks.OnChanges, 'ngOnChanges'],
|
|
||||||
[LifecycleHooks.AfterContentInit, 'ngAfterContentInit'],
|
|
||||||
[LifecycleHooks.AfterContentChecked, 'ngAfterContentChecked'],
|
|
||||||
[LifecycleHooks.AfterViewInit, 'ngAfterViewInit'],
|
|
||||||
[LifecycleHooks.AfterViewChecked, 'ngAfterViewChecked'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean {
|
export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean {
|
||||||
var lcInterface = LIFECYCLE_INTERFACES.get(hook);
|
return reflector.hasLifecycleHook(token, getInterface(hook), getHookName(hook));
|
||||||
var lcProp = LIFECYCLE_PROPS.get(hook);
|
}
|
||||||
return reflector.hasLifecycleHook(token, lcInterface, lcProp);
|
|
||||||
|
function getHookName(hook: LifecycleHooks): string {
|
||||||
|
switch (hook) {
|
||||||
|
case LifecycleHooks.OnInit:
|
||||||
|
return 'ngOnInit';
|
||||||
|
case LifecycleHooks.OnDestroy:
|
||||||
|
return 'ngOnDestroy';
|
||||||
|
case LifecycleHooks.DoCheck:
|
||||||
|
return 'ngDoCheck';
|
||||||
|
case LifecycleHooks.OnChanges:
|
||||||
|
return 'ngOnChanges';
|
||||||
|
case LifecycleHooks.AfterContentInit:
|
||||||
|
return 'ngAfterContentInit';
|
||||||
|
case LifecycleHooks.AfterContentChecked:
|
||||||
|
return 'ngAfterContentChecked';
|
||||||
|
case LifecycleHooks.AfterViewInit:
|
||||||
|
return 'ngAfterViewInit';
|
||||||
|
case LifecycleHooks.AfterViewChecked:
|
||||||
|
return 'ngAfterViewChecked';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInterface(hook: LifecycleHooks): any {
|
||||||
|
switch (hook) {
|
||||||
|
case LifecycleHooks.OnInit:
|
||||||
|
return OnInit;
|
||||||
|
case LifecycleHooks.OnDestroy:
|
||||||
|
return OnDestroy;
|
||||||
|
case LifecycleHooks.DoCheck:
|
||||||
|
return DoCheck;
|
||||||
|
case LifecycleHooks.OnChanges:
|
||||||
|
return OnChanges;
|
||||||
|
case LifecycleHooks.AfterContentInit:
|
||||||
|
return AfterContentInit;
|
||||||
|
case LifecycleHooks.AfterContentChecked:
|
||||||
|
return AfterContentChecked;
|
||||||
|
case LifecycleHooks.AfterViewInit:
|
||||||
|
return AfterViewInit;
|
||||||
|
case LifecycleHooks.AfterViewChecked:
|
||||||
|
return AfterViewChecked;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,10 +134,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||||
interfaces(type: Type<any>): any[] { return []; }
|
interfaces(type: Type<any>): any[] { return []; }
|
||||||
|
|
||||||
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
|
hasLifecycleHook(type: any, lcInterface: Type<any>, lcProperty: string): boolean {
|
||||||
if (!(type instanceof Type)) return false;
|
return type instanceof Type && lcProperty in type.prototype;
|
||||||
|
|
||||||
const proto = (<any>type).prototype;
|
|
||||||
return !!proto[lcProperty];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }
|
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }
|
||||||
|
|
|
@ -15,14 +15,13 @@ import {GetterFn, MethodFn, SetterFn} from './types';
|
||||||
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
||||||
export {GetterFn, MethodFn, SetterFn} from './types';
|
export {GetterFn, MethodFn, SetterFn} from './types';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reflective information about a symbol, including annotations, interfaces, and other metadata.
|
* Reflective information about a symbol, including annotations, interfaces, and other metadata.
|
||||||
*/
|
*/
|
||||||
export class ReflectionInfo {
|
export class ReflectionInfo {
|
||||||
constructor(
|
constructor(
|
||||||
public annotations?: any[], public parameters?: any[][], public factory?: Function,
|
public annotations?: any[], public parameters?: any[][], public factory?: Function,
|
||||||
public interfaces?: any[], public propMetadata?: {[key: string]: any[]}) {}
|
public interfaces?: any[], public propMetadata?: {[name: string]: any[]}) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,8 +44,6 @@ export class Reflector extends ReflectorReader {
|
||||||
|
|
||||||
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
|
updateCapabilities(caps: PlatformReflectionCapabilities) { this.reflectionCapabilities = caps; }
|
||||||
|
|
||||||
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Causes `this` reflector to track keys used to access
|
* Causes `this` reflector to track keys used to access
|
||||||
* {@link ReflectionInfo} objects.
|
* {@link ReflectionInfo} objects.
|
||||||
|
@ -66,10 +63,6 @@ export class Reflector extends ReflectorReader {
|
||||||
return allTypes.filter(key => !this._usedKeys.has(key));
|
return allTypes.filter(key => !this._usedKeys.has(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
|
|
||||||
this._injectableInfo.set(func, funcInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
|
registerType(type: Type<any>, typeInfo: ReflectionInfo): void {
|
||||||
this._injectableInfo.set(type, typeInfo);
|
this._injectableInfo.set(type, typeInfo);
|
||||||
}
|
}
|
||||||
|
@ -160,6 +153,7 @@ export class Reflector extends ReflectorReader {
|
||||||
resolveIdentifier(name: string, moduleUrl: string, runtime: any): any {
|
resolveIdentifier(name: string, moduleUrl: string, runtime: any): any {
|
||||||
return this.reflectionCapabilities.resolveIdentifier(name, moduleUrl, runtime);
|
return this.reflectionCapabilities.resolveIdentifier(name, moduleUrl, runtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveEnum(identifier: any, name: string): any {
|
resolveEnum(identifier: any, name: string): any {
|
||||||
return this.reflectionCapabilities.resolveEnum(identifier, name);
|
return this.reflectionCapabilities.resolveEnum(identifier, name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
/**
|
|
||||||
* @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 {makeDecorator, makeParamDecorator, makePropDecorator} from '@angular/core/src/util/decorators';
|
|
||||||
|
|
||||||
export interface ClassDecoratorFactory {
|
|
||||||
(data: ClassDecorator): any;
|
|
||||||
new (data: ClassDecorator): ClassDecorator;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ClassDecorator { value: any; }
|
|
||||||
|
|
||||||
export interface ParamDecorator { value: any; }
|
|
||||||
|
|
||||||
export interface PropDecorator { value: any; }
|
|
||||||
|
|
||||||
export function classDecorator(value: any /** TODO #9100 */) {
|
|
||||||
return new ClassDecorator({value: value});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function paramDecorator(value: any /** TODO #9100 */) {
|
|
||||||
return new ParamDecorator(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function propDecorator(value: any /** TODO #9100 */) {
|
|
||||||
return new PropDecorator(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @Annotation */ export const ClassDecorator =
|
|
||||||
<ClassDecoratorFactory>makeDecorator('ClassDecorator', {value: undefined});
|
|
||||||
/** @Annotation */ export const ParamDecorator =
|
|
||||||
makeParamDecorator('ParamDecorator', [['value', undefined]]);
|
|
||||||
/** @Annotation */ export const PropDecorator =
|
|
||||||
makePropDecorator('PropDecorator', [['value', undefined]]);
|
|
||||||
|
|
||||||
// used only in Dart
|
|
||||||
export class HasGetterAndSetterDecorators {}
|
|
|
@ -6,26 +6,45 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {OnInit} from '@angular/core';
|
|
||||||
import {ReflectionInfo, Reflector} from '@angular/core/src/reflection/reflection';
|
import {ReflectionInfo, Reflector} from '@angular/core/src/reflection/reflection';
|
||||||
import {ReflectionCapabilities} from '@angular/core/src/reflection/reflection_capabilities';
|
import {ReflectionCapabilities} from '@angular/core/src/reflection/reflection_capabilities';
|
||||||
import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal';
|
import {makeDecorator, makeParamDecorator, makePropDecorator} from '@angular/core/src/util/decorators';
|
||||||
|
|
||||||
import {ClassDecorator, ParamDecorator, PropDecorator, classDecorator, paramDecorator, propDecorator} from './reflector_common';
|
interface ClassDecoratorFactory {
|
||||||
|
(data: ClassDecorator): any;
|
||||||
|
new (data: ClassDecorator): ClassDecorator;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ClassDecorator {
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ParamDecorator {
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PropDecorator {
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @Annotation */ const ClassDecorator =
|
||||||
|
<ClassDecoratorFactory>makeDecorator('ClassDecorator', {value: undefined});
|
||||||
|
/** @Annotation */ const ParamDecorator =
|
||||||
|
makeParamDecorator('ParamDecorator', [['value', undefined]]);
|
||||||
|
/** @Annotation */ const PropDecorator = makePropDecorator('PropDecorator', [['value', undefined]]);
|
||||||
|
|
||||||
class AType {
|
class AType {
|
||||||
value: any /** TODO #9100 */;
|
constructor(public value: any) {}
|
||||||
|
|
||||||
constructor(value: any /** TODO #9100 */) { this.value = value; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ClassDecorator({value: 'class'})
|
@ClassDecorator({value: 'class'})
|
||||||
class ClassWithDecorators {
|
class ClassWithDecorators {
|
||||||
@PropDecorator('p1') @PropDecorator('p2') a: any /** TODO #9100 */;
|
@PropDecorator('p1') @PropDecorator('p2') a: AType;
|
||||||
b: any /** TODO #9100 */;
|
|
||||||
|
b: AType;
|
||||||
|
|
||||||
@PropDecorator('p3')
|
@PropDecorator('p3')
|
||||||
set c(value: any /** TODO #9100 */) {}
|
set c(value: any) {}
|
||||||
|
|
||||||
@PropDecorator('p4')
|
@PropDecorator('p4')
|
||||||
someMethod() {}
|
someMethod() {}
|
||||||
|
@ -37,45 +56,18 @@ class ClassWithDecorators {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ClassWithoutDecorators {
|
class ClassWithoutDecorators {
|
||||||
constructor(a: any /** TODO #9100 */, b: any /** TODO #9100 */) {}
|
constructor(a: any, b: any) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestObj {
|
class TestObj {
|
||||||
a: any /** TODO #9100 */;
|
constructor(public a: any, public b: any) {}
|
||||||
b: any /** TODO #9100 */;
|
|
||||||
|
|
||||||
constructor(a: any /** TODO #9100 */, b: any /** TODO #9100 */) {
|
identity(arg: any) { return arg; }
|
||||||
this.a = a;
|
|
||||||
this.b = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
identity(arg: any /** TODO #9100 */) { return arg; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Interface {}
|
|
||||||
|
|
||||||
class Interface2 {}
|
|
||||||
|
|
||||||
class SuperClassImplementingInterface implements Interface2 {}
|
|
||||||
|
|
||||||
class ClassImplementingInterface extends SuperClassImplementingInterface implements Interface {}
|
|
||||||
|
|
||||||
// Classes used to test our runtime check for classes that implement lifecycle interfaces but do not
|
|
||||||
// declare them.
|
|
||||||
// See https://github.com/angular/angular/pull/6879 and https://goo.gl/b07Kii for details.
|
|
||||||
class ClassDoesNotDeclareOnInit {
|
|
||||||
ngOnInit() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SuperClassImplementingOnInit implements OnInit {
|
|
||||||
ngOnInit() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SubClassDoesNotDeclareOnInit extends SuperClassImplementingOnInit {}
|
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('Reflector', () => {
|
describe('Reflector', () => {
|
||||||
var reflector: any /** TODO #9100 */;
|
var reflector: Reflector;
|
||||||
|
|
||||||
beforeEach(() => { reflector = new Reflector(new ReflectionCapabilities()); });
|
beforeEach(() => { reflector = new Reflector(new ReflectionCapabilities()); });
|
||||||
|
|
||||||
|
@ -104,42 +96,11 @@ export function main() {
|
||||||
|
|
||||||
describe('factory', () => {
|
describe('factory', () => {
|
||||||
it('should create a factory for the given type', () => {
|
it('should create a factory for the given type', () => {
|
||||||
var obj = reflector.factory(TestObj)(1, 2);
|
const obj = reflector.factory(TestObj)(1, 2);
|
||||||
|
|
||||||
expect(obj.a).toEqual(1);
|
expect(obj.a).toEqual(1);
|
||||||
expect(obj.b).toEqual(2);
|
expect(obj.b).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should check args from no to max', () => {
|
|
||||||
var f = (t: any /** TODO #9100 */) => reflector.factory(t);
|
|
||||||
var checkArgs = (obj: any /** TODO #9100 */, args: any /** TODO #9100 */) =>
|
|
||||||
expect(obj.args).toEqual(args);
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
checkArgs(f(TestObjWith00Args)(), []);
|
|
||||||
checkArgs(f(TestObjWith01Args)(1), [1]);
|
|
||||||
checkArgs(f(TestObjWith02Args)(1, 2), [1, 2]);
|
|
||||||
checkArgs(f(TestObjWith03Args)(1, 2, 3), [1, 2, 3]);
|
|
||||||
checkArgs(f(TestObjWith04Args)(1, 2, 3, 4), [1, 2, 3, 4]);
|
|
||||||
checkArgs(f(TestObjWith05Args)(1, 2, 3, 4, 5), [1, 2, 3, 4, 5]);
|
|
||||||
checkArgs(f(TestObjWith06Args)(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5, 6]);
|
|
||||||
checkArgs(f(TestObjWith07Args)(1, 2, 3, 4, 5, 6, 7), [1, 2, 3, 4, 5, 6, 7]);
|
|
||||||
checkArgs(f(TestObjWith08Args)(1, 2, 3, 4, 5, 6, 7, 8), [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
||||||
checkArgs(f(TestObjWith09Args)(1, 2, 3, 4, 5, 6, 7, 8, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
||||||
checkArgs(f(TestObjWith10Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
||||||
checkArgs(f(TestObjWith11Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
|
|
||||||
checkArgs(f(TestObjWith12Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
|
|
||||||
checkArgs(f(TestObjWith13Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
|
|
||||||
checkArgs(f(TestObjWith14Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]);
|
|
||||||
checkArgs(f(TestObjWith15Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
|
|
||||||
checkArgs(f(TestObjWith16Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
|
|
||||||
checkArgs(f(TestObjWith17Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]);
|
|
||||||
checkArgs(f(TestObjWith18Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]);
|
|
||||||
checkArgs(f(TestObjWith19Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
|
|
||||||
checkArgs(f(TestObjWith20Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]);
|
|
||||||
// clang-format on
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return a registered factory if available', () => {
|
it('should return a registered factory if available', () => {
|
||||||
reflector.registerType(TestObj, new ReflectionInfo(null, null, () => 'fake'));
|
reflector.registerType(TestObj, new ReflectionInfo(null, null, () => 'fake'));
|
||||||
expect(reflector.factory(TestObj)()).toEqual('fake');
|
expect(reflector.factory(TestObj)()).toEqual('fake');
|
||||||
|
@ -148,12 +109,12 @@ export function main() {
|
||||||
|
|
||||||
describe('parameters', () => {
|
describe('parameters', () => {
|
||||||
it('should return an array of parameters for a type', () => {
|
it('should return an array of parameters for a type', () => {
|
||||||
var p = reflector.parameters(ClassWithDecorators);
|
const p = reflector.parameters(ClassWithDecorators);
|
||||||
expect(p).toEqual([[AType, paramDecorator('a')], [AType, paramDecorator('b')]]);
|
expect(p).toEqual([[AType, new ParamDecorator('a')], [AType, new ParamDecorator('b')]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work for a class without annotations', () => {
|
it('should work for a class without annotations', () => {
|
||||||
var p = reflector.parameters(ClassWithoutDecorators);
|
const p = reflector.parameters(ClassWithoutDecorators);
|
||||||
expect(p.length).toEqual(2);
|
expect(p.length).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -170,10 +131,10 @@ export function main() {
|
||||||
|
|
||||||
describe('propMetadata', () => {
|
describe('propMetadata', () => {
|
||||||
it('should return a string map of prop metadata for the given class', () => {
|
it('should return a string map of prop metadata for the given class', () => {
|
||||||
var p = reflector.propMetadata(ClassWithDecorators);
|
const p = reflector.propMetadata(ClassWithDecorators);
|
||||||
expect(p['a']).toEqual([propDecorator('p1'), propDecorator('p2')]);
|
expect(p['a']).toEqual([new PropDecorator('p1'), new PropDecorator('p2')]);
|
||||||
expect(p['c']).toEqual([propDecorator('p3')]);
|
expect(p['c']).toEqual([new PropDecorator('p3')]);
|
||||||
expect(p['someMethod']).toEqual([propDecorator('p4')]);
|
expect(p['someMethod']).toEqual([new PropDecorator('p4')]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return registered meta if available', () => {
|
it('should return registered meta if available', () => {
|
||||||
|
@ -184,8 +145,8 @@ export function main() {
|
||||||
|
|
||||||
describe('annotations', () => {
|
describe('annotations', () => {
|
||||||
it('should return an array of annotations for a type', () => {
|
it('should return an array of annotations for a type', () => {
|
||||||
var p = reflector.annotations(ClassWithDecorators);
|
const p = reflector.annotations(ClassWithDecorators);
|
||||||
expect(p).toEqual([classDecorator('class')]);
|
expect(p).toEqual([new ClassDecorator({value: 'class'})]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return registered annotations if available', () => {
|
it('should return registered annotations if available', () => {
|
||||||
|
@ -194,36 +155,35 @@ export function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work for a class without annotations', () => {
|
it('should work for a class without annotations', () => {
|
||||||
var p = reflector.annotations(ClassWithoutDecorators);
|
const p = reflector.annotations(ClassWithoutDecorators);
|
||||||
expect(p).toEqual([]);
|
expect(p).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getter', () => {
|
describe('getter', () => {
|
||||||
it('returns a function reading a property', () => {
|
it('returns a function reading a property', () => {
|
||||||
var getA = reflector.getter('a');
|
const getA = reflector.getter('a');
|
||||||
expect(getA(new TestObj(1, 2))).toEqual(1);
|
expect(getA(new TestObj(1, 2))).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a registered getter if available', () => {
|
it('should return a registered getter if available', () => {
|
||||||
reflector.registerGetters({'abc': (obj: any /** TODO #9100 */) => 'fake'});
|
reflector.registerGetters({'abc': (obj: any) => 'fake'});
|
||||||
expect(reflector.getter('abc')('anything')).toEqual('fake');
|
expect(reflector.getter('abc')('anything')).toEqual('fake');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('setter', () => {
|
describe('setter', () => {
|
||||||
it('returns a function setting a property', () => {
|
it('returns a function setting a property', () => {
|
||||||
var setA = reflector.setter('a');
|
const setA = reflector.setter('a');
|
||||||
var obj = new TestObj(1, 2);
|
const obj = new TestObj(1, 2);
|
||||||
setA(obj, 100);
|
setA(obj, 100);
|
||||||
expect(obj.a).toEqual(100);
|
expect(obj.a).toEqual(100);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a registered setter if available', () => {
|
it('should return a registered setter if available', () => {
|
||||||
var updateMe: any /** TODO #9100 */;
|
let updateMe: any;
|
||||||
reflector.registerSetters({
|
|
||||||
'abc': (obj: any /** TODO #9100 */, value: any /** TODO #9100 */) => { updateMe = value; }
|
reflector.registerSetters({'abc': (obj: any, value: any) => { updateMe = value; }});
|
||||||
});
|
|
||||||
reflector.setter('abc')('anything', 'fake');
|
reflector.setter('abc')('anything', 'fake');
|
||||||
|
|
||||||
expect(updateMe).toEqual('fake');
|
expect(updateMe).toEqual('fake');
|
||||||
|
@ -232,189 +192,15 @@ export function main() {
|
||||||
|
|
||||||
describe('method', () => {
|
describe('method', () => {
|
||||||
it('returns a function invoking a method', () => {
|
it('returns a function invoking a method', () => {
|
||||||
var func = reflector.method('identity');
|
const func = reflector.method('identity');
|
||||||
var obj = new TestObj(1, 2);
|
const obj = new TestObj(1, 2);
|
||||||
expect(func(obj, ['value'])).toEqual('value');
|
expect(func(obj, ['value'])).toEqual('value');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a registered method if available', () => {
|
it('should return a registered method if available', () => {
|
||||||
reflector.registerMethods(
|
reflector.registerMethods({'abc': (obj: any, args: any) => args});
|
||||||
{'abc': (obj: any /** TODO #9100 */, args: any /** TODO #9100 */) => args});
|
|
||||||
expect(reflector.method('abc')('anything', ['fake'])).toEqual(['fake']);
|
expect(reflector.method('abc')('anything', ['fake'])).toEqual(['fake']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TestObjWith00Args {
|
|
||||||
args: any[];
|
|
||||||
constructor() { this.args = []; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith01Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any) { this.args = [a1]; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith02Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any) { this.args = [a1, a2]; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith03Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any) { this.args = [a1, a2, a3]; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith04Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any) { this.args = [a1, a2, a3, a4]; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith05Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any, a5: any) { this.args = [a1, a2, a3, a4, a5]; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith06Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any, a5: any, a6: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith07Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith08Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith09Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith10Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith11Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith12Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith13Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith14Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith15Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith16Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith17Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith18Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any, a18: any) {
|
|
||||||
this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith19Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any, a18: any, a19: any) {
|
|
||||||
this.args =
|
|
||||||
[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith20Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any, a18: any, a19: any,
|
|
||||||
a20: any) {
|
|
||||||
this.args =
|
|
||||||
[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestObjWith21Args {
|
|
||||||
args: any[];
|
|
||||||
constructor(
|
|
||||||
a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, a10: any,
|
|
||||||
a11: any, a12: any, a13: any, a14: any, a15: any, a16: any, a17: any, a18: any, a19: any,
|
|
||||||
a20: any, a21: any) {
|
|
||||||
this.args = [
|
|
||||||
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
|
|
||||||
a12, a13, a14, a15, a16, a17, a18, a19, a20, a21
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue