Skip to content

Commit

Permalink
Make it work with decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Apr 26, 2015
1 parent 0d6599c commit f9b3d93
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 44 deletions.
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"typeAssertionModule": "assert",
"annotations": true,
"sourceMaps": "file"
},
"babel": {
"sourceMaps": "inline",
"stage": 0
}
}
41 changes: 41 additions & 0 deletions karma.conf.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Karma configuration
// Generated on Fri Mar 14 2014 15:01:19 GMT-0700 (PDT)

var babelOptions = require('./config').babel;

module.exports = function(config) {

var files;

files = [
'test/**/*.js'
];

config.set({
frameworks: ['jasmine', 'browserify', 'sourcemaps'],

files: [
'node_modules/karma-babel-preprocessor/node_modules/babel-core/browser-polyfill.js'
// The entry point that dynamically imports all the specs.
//{pattern: 'test-help/main.js', included: true},

// The runtime assertion library.
//'node_modules/rtts-assert/dist/cjs/assert.js',
//'node_modules/angular/angular.js',
//'node_modules/angular-mocks/angular-mocks.js'
].concat(files),

preprocessors: {
'test/**/*.js': ['browserify']
},

browsers: ['Chrome'],

browserify: {
transform: [ ['babelify', babelOptions] ]
}

});

config.plugins.push(require('./karma_sourcemaps'));
};
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
"traceur": "^0.0.86"
},
"devDependencies": {
"angular": "^1.3.15",
"angular-mocks": "^1.3.15",
"assert": "angular/assert#dist",
"babelify": "^6.0.2",
"gulp": "^3.8.9",
"gulp-connect": "^2.0.6",
"gulp-rename": "^1.2.0",
"gulp-traceur": "vojtajina/gulp-traceur#fix-sourcemaps",
"karma": "^0.12.24",
"karma-babel-preprocessor": "^5.1.0",
"karma-browserify": "^4.1.2",
"karma-chrome-launcher": "^0.1.5",
"karma-commonjs": "0.0.13",
"karma-firefox-launcher": "^0.1.4",
"karma-jasmine": "^0.2.2",
"karma-requirejs": "^0.2.2",
Expand Down
4 changes: 3 additions & 1 deletion src/a1atscript/DirectiveObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {ListInjector} from './injectorTypes.js';
import {registerInjector} from './Injector.js';
import ToAnnotation from './ToAnnotation.js';

@ToAnnotation
export class DirectiveObject {
constructor(token, dependencies = []) {
this.dependencies = dependencies;
Expand All @@ -10,7 +12,7 @@ export class DirectiveObject {

class DirectiveObjectInjector extends ListInjector {
get annotationClass() {
return DirectiveObject;
return DirectiveObject.originalClass || DirectiveObject;
}

_createFactoryArray(ConstructorFn) {
Expand Down
6 changes: 3 additions & 3 deletions src/a1atscript/Injector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from './annotations.js';
import { AsModule, Module } from './annotations.js';

import {
ConfigInjector,
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Injector {
}

_getModuleAnnotation(dependency) {
return dependency.annotations.find((annotation) => annotation instanceof Module);
return dependency.annotations.find((annotation) => annotation instanceof Module || annotation instanceof AsModule);
}

_mergeSortedDependencies(sorted1, sorted2) {
Expand Down Expand Up @@ -154,7 +154,7 @@ export class Injector {
}

_moduleMetadata(moduleClass) {
return moduleClass.annotations.find((value) => value instanceof Module);
return moduleClass.annotations.find((value) => value instanceof Module || value instanceof AsModule);
}

_instantiateModuleDependencies(moduleDependencies) {
Expand Down
25 changes: 25 additions & 0 deletions src/a1atscript/ToAnnotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function ToAnnotation(AnnotationClass) {
var decorator = function(...callParams) {
callParams.unshift(null);
return function(targetClass) {
var oldAnnotation = Object.getOwnPropertyDescriptor(targetClass, 'annotations');
if (oldAnnotation) {
var oldGetter = oldAnnotation.get
Object.defineProperty(targetClass, 'annotations', {
configurable: true,
get: function() {
return oldGetter().concat([new (Function.prototype.bind.apply(AnnotationClass, callParams))]);
}});
} else {
Object.defineProperty(targetClass, 'annotations', {
configurable: true,
get: function() {
return [new (Function.prototype.bind.apply(AnnotationClass, callParams))];
}});
}
return targetClass;
};
};
decorator.originalClass = AnnotationClass;
return decorator;
}
18 changes: 18 additions & 0 deletions src/a1atscript/annotations.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ToAnnotation from './ToAnnotation.js';

class NgAnnotation {
constructor(...dependencies) {
this.dependencies = dependencies;
Expand All @@ -11,50 +13,66 @@ class NgNamedAnnotation {
}
}

@ToAnnotation
export class Config extends NgAnnotation {

}

@ToAnnotation
export class Run extends NgAnnotation {

}

@ToAnnotation
export class Controller extends NgNamedAnnotation {

}

@ToAnnotation
export class Directive extends NgNamedAnnotation {

}

@ToAnnotation
export class Service extends NgNamedAnnotation {

}

@ToAnnotation
export class Factory extends NgNamedAnnotation {

}

@ToAnnotation
export class Provider extends NgNamedAnnotation {

}

@ToAnnotation
export class Value extends NgNamedAnnotation {

}

@ToAnnotation
export class Constant extends NgNamedAnnotation {

}

@ToAnnotation
export class Filter extends NgNamedAnnotation {

}

@ToAnnotation
export class Animation extends NgNamedAnnotation {

}

export class Module extends NgNamedAnnotation {

}

@ToAnnotation
export class AsModule extends Module {

}
22 changes: 11 additions & 11 deletions src/a1atscript/injectorTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ListInjector {
export class ConfigInjector extends ListInjector {

get annotationClass() {
return Config;
return Config.originalClass || Config;
}

instantiateOne(module, config, metadata) {
Expand All @@ -35,7 +35,7 @@ export class ConfigInjector extends ListInjector {
export class RunInjector extends ListInjector {

get annotationClass() {
return Run;
return Run.originalClass || Run;
}

instantiateOne(module, run, metadata) {
Expand All @@ -47,7 +47,7 @@ export class RunInjector extends ListInjector {
export class ControllerInjector extends ListInjector {

get annotationClass() {
return Controller;
return Controller.originalClass || Controller;
}

instantiateOne(module, controller, metadata) {
Expand All @@ -60,7 +60,7 @@ export class ControllerInjector extends ListInjector {
export class DirectiveInjector extends ListInjector {

get annotationClass() {
return Directive;
return Directive.originalClass || Directive;
}

instantiateOne(module, directive, metadata) {
Expand All @@ -73,7 +73,7 @@ export class DirectiveInjector extends ListInjector {
export class ServiceInjector extends ListInjector {

get annotationClass() {
return Service;
return Service.originalClass || Service;
}

instantiateOne(module, service, metadata) {
Expand All @@ -86,7 +86,7 @@ export class ServiceInjector extends ListInjector {
export class FactoryInjector extends ListInjector {

get annotationClass() {
return Factory;
return Factory.originalClass || Factory;
}

instantiateOne(module, factory, metadata) {
Expand All @@ -99,7 +99,7 @@ export class FactoryInjector extends ListInjector {
export class ProviderInjector extends ListInjector {

get annotationClass() {
return Provider;
return Provider.originalClass || Provider;
}

instantiateOne(module, provider, metadata) {
Expand All @@ -112,7 +112,7 @@ export class ProviderInjector extends ListInjector {
export class ValueInjector extends ListInjector {

get annotationClass() {
return Value;
return Value.originalClass || Value;
}

instantiateOne(module, value, metadata) {
Expand All @@ -125,7 +125,7 @@ export class ValueInjector extends ListInjector {
export class ConstantInjector extends ListInjector {

get annotationClass() {
return Constant;
return Constant.originalClass || Constant;
}

instantiateOne(module, constant, metadata) {
Expand All @@ -138,7 +138,7 @@ export class ConstantInjector extends ListInjector {
export class AnimationInjector extends ListInjector {

get annotationClass() {
return Animation;
return Animation.originalClass || Animation;
}

instantiateOne(module, animation, metadata) {
Expand All @@ -151,7 +151,7 @@ export class AnimationInjector extends ListInjector {
export class FilterInjector extends ListInjector {

get annotationClass() {
return Filter;
return Filter.originalClass || Filter;
}

instantiateOne(module, filter, metadata) {
Expand Down
3 changes: 3 additions & 0 deletions src/a1atscript/ng2Directives/Component.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import Ng2Directive from './Ng2Directive.js';
import ToAnnotation from '../ToAnnotation.js';

@ToAnnotation
export class Component extends Ng2Directive {
constructor(descriptor) {
super(descriptor)
this.services = descriptor.services;
}
}

@ToAnnotation
export class Template {
constructor(descriptor) {
this.url = descriptor.url;
Expand Down
5 changes: 3 additions & 2 deletions src/a1atscript/ng2Directives/ComponentInjector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import BindBuilder from "./BindBuilder.js";

class ComponentInjector extends ListInjector {
get annotationClass() {
return Component;
return Component.originalClass || Component;
}

_template(component) {
return component.annotations.find((annotation) => annotation instanceof Template);
var TemplateClass = Template.originalClass || Template;
return component.annotations.find((annotation) => annotation instanceof TemplateClass);
}

instantiateOne(module, component, annotation) {
Expand Down
2 changes: 1 addition & 1 deletion src/a1atscript/ng2Directives/TemplateProperties.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Template} from "./Component.js";

export default class TemplateProperties {
constructor(template:Template) {
constructor(template) {
this._template = template;
}

Expand Down
4 changes: 2 additions & 2 deletions test-help/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ require.config({
paths: {
assert: './node_modules/rtts-assert/dist/amd/assert',
angular: './bower_components/angular/angular',
angularMocks: './bower_components/angular-mocks/angular-mocks'
'angular-mocks': './bower_components/angular-mocks/angular-mocks'
},

shim: {
'angular': {'exports': 'angular'},
'angularMocks': {deps: ['angular'], 'exports': 'angular.mock'},
'angular-mocks': {deps: ['angular'], 'exports': 'angular.mock'},
},

// Dynamically load all test files.
Expand Down
6 changes: 4 additions & 2 deletions test/Component_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import angular from 'angular.js';
import mock from 'angularMocks.js';
import angular from 'angular';
import 'angular-mocks';

import {Component, Template} from '../src/a1atscript/ng2Directives/Component'
import "../src/a1atscript/ng2Directives/ComponentInjector";
Expand All @@ -13,6 +13,8 @@ import {
bootstrap
} from '../src/a1atscript/Injector';

var mock = angular.mock;

@Service('ExampleService')
class ExampleService {
constructor() {
Expand Down
Loading

0 comments on commit f9b3d93

Please sign in to comment.