From 7501ad11ca20b92b589d24a46844b2c980944fa7 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 18 May 2015 16:09:09 -0700 Subject: [PATCH] chore: Define ReflectionCapabilities interface --- .../angular2/src/reflection/reflection.dart | 2 +- .../reflection/reflection_capabilities.dart | 2 +- .../src/reflection/reflection_capabilities.ts | 12 ++++++---- modules/angular2/src/reflection/reflector.ts | 7 +++--- modules/angular2/src/reflection/types.dart | 10 ++++++++ modules/angular2/src/reflection/types.ts | 24 ++++++++++++++++--- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/modules/angular2/src/reflection/reflection.dart b/modules/angular2/src/reflection/reflection.dart index 9222fcc255..8c81ee3671 100644 --- a/modules/angular2/src/reflection/reflection.dart +++ b/modules/angular2/src/reflection/reflection.dart @@ -5,7 +5,7 @@ import 'types.dart'; export 'reflector.dart'; import 'package:angular2/src/facade/lang.dart'; -class NoReflectionCapabilities { +class NoReflectionCapabilities implements IReflectionCapabilities { Function factory(Type type) { throw "Cannot find reflection information on ${stringify(type)}"; } diff --git a/modules/angular2/src/reflection/reflection_capabilities.dart b/modules/angular2/src/reflection/reflection_capabilities.dart index f4205b7a7d..7e28114042 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.dart +++ b/modules/angular2/src/reflection/reflection_capabilities.dart @@ -4,7 +4,7 @@ import 'package:angular2/src/facade/lang.dart'; import 'types.dart'; import 'dart:mirrors'; -class ReflectionCapabilities { +class ReflectionCapabilities implements IReflectionCapabilities { ReflectionCapabilities([metadataReader]) {} Function factory(Type type) { diff --git a/modules/angular2/src/reflection/reflection_capabilities.ts b/modules/angular2/src/reflection/reflection_capabilities.ts index 913edfba41..ecbe2cf69d 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.ts +++ b/modules/angular2/src/reflection/reflection_capabilities.ts @@ -1,8 +1,8 @@ import {Type, isPresent, global, stringify, BaseException} from 'angular2/src/facade/lang'; import {List, ListWrapper} from 'angular2/src/facade/collection'; -import {GetterFn, SetterFn, MethodFn} from './types'; +import {GetterFn, SetterFn, MethodFn, IReflectionCapabilities} from './types'; -export class ReflectionCapabilities { +export class ReflectionCapabilities implements IReflectionCapabilities { private _reflect: any; constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; } @@ -99,13 +99,15 @@ export class ReflectionCapabilities { interfaces(type): List { throw new BaseException("JavaScript does not support interfaces"); } - getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); } + getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); } - setter(name: string): SetterFn { return new Function('o', 'v', 'return o.' + name + ' = v;'); } + setter(name: string): SetterFn { + return new Function('o', 'v', 'return o.' + name + ' = v;'); + } method(name: string): MethodFn { let functionBody = `if (!o.${name}) throw new Error('"${name}" is undefined'); return o.${name}.apply(o, args);`; - return new Function('o', 'args', functionBody); + return new Function('o', 'args', functionBody); } } diff --git a/modules/angular2/src/reflection/reflector.ts b/modules/angular2/src/reflection/reflector.ts index 2bc48d885e..05c2fe6c9f 100644 --- a/modules/angular2/src/reflection/reflector.ts +++ b/modules/angular2/src/reflection/reflector.ts @@ -7,16 +7,17 @@ import { StringMap, StringMapWrapper } from 'angular2/src/facade/collection'; -import {SetterFn, GetterFn, MethodFn} from './types'; +import {SetterFn, GetterFn, MethodFn, IReflectionCapabilities} from './types'; +export {SetterFn, GetterFn, MethodFn, IReflectionCapabilities} from './types'; export class Reflector { _typeInfo: Map; _getters: Map; _setters: Map; _methods: Map; - reflectionCapabilities: any; + reflectionCapabilities: IReflectionCapabilities; - constructor(reflectionCapabilities) { + constructor(reflectionCapabilities: IReflectionCapabilities) { this._typeInfo = MapWrapper.create(); this._getters = MapWrapper.create(); this._setters = MapWrapper.create(); diff --git a/modules/angular2/src/reflection/types.dart b/modules/angular2/src/reflection/types.dart index d1bc7fc9a6..1e0b46b765 100644 --- a/modules/angular2/src/reflection/types.dart +++ b/modules/angular2/src/reflection/types.dart @@ -3,3 +3,13 @@ library reflection.types; typedef SetterFn(Object obj, value); typedef GetterFn(Object obj); typedef MethodFn(Object obj, List args); + +abstract class IReflectionCapabilities { + Function factory(Type type); + List> parameters(Type type); + List interfaces(Type type); + List annotations(Type type); + GetterFn getter(String name); + SetterFn setter(String name); + MethodFn method(String name); +} diff --git a/modules/angular2/src/reflection/types.ts b/modules/angular2/src/reflection/types.ts index c5445a2379..88c0fde461 100644 --- a/modules/angular2/src/reflection/types.ts +++ b/modules/angular2/src/reflection/types.ts @@ -1,3 +1,21 @@ -export {Function as SetterFn}; -export {Function as GetterFn}; -export {Function as MethodFn}; +import {Type} from 'angular2/src/facade/lang'; +import {List} from 'angular2/src/facade/collection'; + +// HACK: workaround for Traceur behavior. +// It expects all transpiled modules to contain this marker. +// TODO: remove this when we no longer use traceur +export var __esModule = true; + +export type SetterFn = (obj: any, value: any) => void; +export type GetterFn = (obj: any) => any; +export type MethodFn = (obj: any, args: List) => any; + +export interface IReflectionCapabilities { + factory(type: Type): Function; + parameters(type: Type): List>; + interfaces(type: Type): List; + annotations(type: Type): List; + getter(name: string): GetterFn; + setter(name: string): SetterFn; + method(name: string): MethodFn; +}