From 623edcd2d8e3b79363e575e31cdfbf936ad55d1f Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 24 Apr 2015 13:17:36 -0700 Subject: [PATCH] Copy a second package to TypeScript. --- gulpfile.js | 4 +- modules/angular2/src/facade/lang.ts | 2 + modules/angular2/src/reflection/reflection.ts | 12 +++ .../src/reflection/reflection_capabilities.ts | 66 ++++++++++++++ modules/angular2/src/reflection/reflector.ts | 85 +++++++++++++++++++ modules/angular2/src/reflection/types.ts | 8 ++ tools/broccoli/broccoli_builder.ts | 3 +- 7 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 modules/angular2/src/reflection/reflection.ts create mode 100644 modules/angular2/src/reflection/reflection_capabilities.ts create mode 100644 modules/angular2/src/reflection/reflector.ts create mode 100644 modules/angular2/src/reflection/types.ts diff --git a/gulpfile.js b/gulpfile.js index 6cf274f2a2..3facf107d8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -530,7 +530,7 @@ gulp.task('build/pure-packages.dart', function() { // `modules_dart`, so they have to walk up and into `dist`. // // Example: - // + // // dependency_overrides: // angular2: // path: ../../dist/dart/angular2 @@ -538,7 +538,7 @@ gulp.task('build/pure-packages.dart', function() { // When we copy a pure package into `dist` the relative path // must be updated. The code below replaces paths accordingly. // So the example above is turned into: - // + // // dependency_overrides: // angular2: // path: ../angular2 diff --git a/modules/angular2/src/facade/lang.ts b/modules/angular2/src/facade/lang.ts index 2b3e3d4204..dda14555ae 100644 --- a/modules/angular2/src/facade/lang.ts +++ b/modules/angular2/src/facade/lang.ts @@ -7,6 +7,8 @@ export {_global as global}; export var __esModule = true; export var Type = Function; +export type Type = typeof Function; + export var Math = _global.Math; export var Date = _global.Date; diff --git a/modules/angular2/src/reflection/reflection.ts b/modules/angular2/src/reflection/reflection.ts new file mode 100644 index 0000000000..9db2e73a9c --- /dev/null +++ b/modules/angular2/src/reflection/reflection.ts @@ -0,0 +1,12 @@ +import {Type, isPresent} from 'angular2/src/facade/lang'; +import {List, ListWrapper} from 'angular2/src/facade/collection'; +import {Reflector} from './reflector'; +export {Reflector} from './reflector'; +import {ReflectionCapabilities} from './reflection_capabilities'; + +// 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 var reflector = new Reflector(new ReflectionCapabilities()); diff --git a/modules/angular2/src/reflection/reflection_capabilities.ts b/modules/angular2/src/reflection/reflection_capabilities.ts new file mode 100644 index 0000000000..3bea3ac98d --- /dev/null +++ b/modules/angular2/src/reflection/reflection_capabilities.ts @@ -0,0 +1,66 @@ +import {Type, isPresent} from 'angular2/src/facade/lang'; +import {List, ListWrapper} from 'angular2/src/facade/collection'; +import {GetterFn, SetterFn, MethodFn} from './types'; + +// 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 class ReflectionCapabilities { + factory(type: Type): Function { + switch (type.length) { + case 0: + return function() { return new type(); }; + case 1: + return function(a1) { return new type(a1); }; + case 2: + return function(a1, a2) { return new type(a1, a2); }; + case 3: + return function(a1, a2, a3) { return new type(a1, a2, a3); }; + case 4: + return function(a1, a2, a3, a4) { return new type(a1, a2, a3, a4); }; + case 5: + return function(a1, a2, a3, a4, a5) { return new type(a1, a2, a3, a4, a5); }; + case 6: + return function(a1, a2, a3, a4, a5, a6) { return new type(a1, a2, a3, a4, a5, a6); }; + case 7: + return function(a1, a2, a3, a4, a5, a6, a7) { + return new type(a1, a2, a3, a4, a5, a6, a7); + }; + case 8: + return function(a1, a2, a3, a4, a5, a6, a7, a8) { + return new type(a1, a2, a3, a4, a5, a6, a7, a8); + }; + case 9: + return function(a1, a2, a3, a4, a5, a6, a7, a8, a9) { + return new type(a1, a2, a3, a4, a5, a6, a7, a8, a9); + }; + case 10: + return function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { + return new type(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); + }; + }; + + throw new Error("Factory cannot take more than 10 arguments"); + } + + parameters(typeOfFunc): List> { + return isPresent(typeOfFunc.parameters) ? typeOfFunc.parameters : + ListWrapper.createFixedSize(typeOfFunc.length); + } + + annotations(typeOfFunc): List { + return isPresent(typeOfFunc.annotations) ? typeOfFunc.annotations : []; + } + + getter(name: string): GetterFn { return new Function('o', 'return o.' + name + ';'); } + + 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); + } +} diff --git a/modules/angular2/src/reflection/reflector.ts b/modules/angular2/src/reflection/reflector.ts new file mode 100644 index 0000000000..757cd431ce --- /dev/null +++ b/modules/angular2/src/reflection/reflector.ts @@ -0,0 +1,85 @@ +import {Type, isPresent, stringify, BaseException} from 'angular2/src/facade/lang'; +import {List, ListWrapper, Map, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection'; +import {SetterFn, GetterFn, MethodFn} from './types'; +export {SetterFn, GetterFn, MethodFn} from './types'; + +// 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 class Reflector { + _typeInfo: Map; + _getters: Map; + _setters: Map; + _methods: Map; + reflectionCapabilities: any; + + constructor(reflectionCapabilities) { + this._typeInfo = MapWrapper.create(); + this._getters = MapWrapper.create(); + this._setters = MapWrapper.create(); + this._methods = MapWrapper.create(); + this.reflectionCapabilities = reflectionCapabilities; + } + + registerType(type, typeInfo) { MapWrapper.set(this._typeInfo, type, typeInfo); } + + registerGetters(getters) { _mergeMaps(this._getters, getters); } + + registerSetters(setters) { _mergeMaps(this._setters, setters); } + + registerMethods(methods) { _mergeMaps(this._methods, methods); } + + factory(type: Type): Function { + if (MapWrapper.contains(this._typeInfo, type)) { + return MapWrapper.get(this._typeInfo, type)["factory"]; + } else { + return this.reflectionCapabilities.factory(type); + } + } + + parameters(typeOfFunc): List { + if (MapWrapper.contains(this._typeInfo, typeOfFunc)) { + return MapWrapper.get(this._typeInfo, typeOfFunc)["parameters"]; + } else { + return this.reflectionCapabilities.parameters(typeOfFunc); + } + } + + annotations(typeOfFunc): List { + if (MapWrapper.contains(this._typeInfo, typeOfFunc)) { + return MapWrapper.get(this._typeInfo, typeOfFunc)["annotations"]; + } else { + return this.reflectionCapabilities.annotations(typeOfFunc); + } + } + + getter(name: string): GetterFn { + if (MapWrapper.contains(this._getters, name)) { + return MapWrapper.get(this._getters, name); + } else { + return this.reflectionCapabilities.getter(name); + } + } + + setter(name: string): SetterFn { + if (MapWrapper.contains(this._setters, name)) { + return MapWrapper.get(this._setters, name); + } else { + return this.reflectionCapabilities.setter(name); + } + } + + method(name: string): MethodFn { + if (MapWrapper.contains(this._methods, name)) { + return MapWrapper.get(this._methods, name); + } else { + return this.reflectionCapabilities.method(name); + } + } +} + +function _mergeMaps(target: Map, config) { + StringMapWrapper.forEach(config, (v, k) => MapWrapper.set(target, k, v)); +} diff --git a/modules/angular2/src/reflection/types.ts b/modules/angular2/src/reflection/types.ts new file mode 100644 index 0000000000..1a7f4e83da --- /dev/null +++ b/modules/angular2/src/reflection/types.ts @@ -0,0 +1,8 @@ +// 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 {Function as SetterFn}; +export {Function as GetterFn}; +export {Function as MethodFn}; diff --git a/tools/broccoli/broccoli_builder.ts b/tools/broccoli/broccoli_builder.ts index 3d11899111..e332152744 100644 --- a/tools/broccoli/broccoli_builder.ts +++ b/tools/broccoli/broccoli_builder.ts @@ -54,14 +54,13 @@ export class BroccoliBuilder { printSlowTrees(hash.graph); }) .catch(err => { + console.log(err.toString()); // Should show file and line/col if present if (err.file) { console.error('File: ' + err.file); } if (err.stack) { console.error(err.stack); - } else { - console.error(err); } throw err; });