Permalink
Browse files

fix(dependency-injection): ignore ...rest TypeScript metadata

Fixes #171
  • Loading branch information...
silbinarywolf committed Dec 11, 2018
1 parent 6971546 commit c0937561c4726a1342df29ec9cd277e10a40b756
Showing with 49 additions and 0 deletions.
  1. +8 −0 src/injection.js
  2. +41 −0 test/injection.spec.js
@@ -8,6 +8,14 @@ export function autoinject(potentialTarget?: any): any {
let deco = function(target) {
if (!target.hasOwnProperty('inject')) {
target.inject = (metadata.getOwn(metadata.paramTypes, target) || _emptyParameters).slice();
if (target.inject &&
target.inject.length > 0) {
// TypeScript 3.0 metadata for "...rest" gives type "Object"
// if last parameter is "Object", assume it's a ...rest and remove that metadata.
if (target.inject[target.inject.length - 1] === Object) {
target.inject.splice(-1,1);
}
}
}
};

@@ -276,6 +276,47 @@ describe('injection', () => {
expect(app2.logger).toEqual(jasmine.any(Logger));
expect(app2.service).toEqual(jasmine.any(Service));
});

it('test variadic arguments (TypeScript metadata)', function() {
class VariadicParentApp {
static inject() { return [Logger]; }
constructor(logger) {
this.logger = logger;
}
}
Reflect.metadata(ParentApp, 'design:paramtypes', [Logger]);

// TypeScript 3.0 gives "Object" in metadata for "...rest" arguments.
let VariadicChildApp = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Service, Object])).on(
class VariadicChildApp extends VariadicParentApp {
constructor(service, ...rest) {
super(...rest);
this.service = service;
}
});

let VariadicGrandchildApp = decorators(autoinject(), Reflect.metadata('design:paramtypes', [SubService1, Object])).on(
class VariadicGrandchildApp extends VariadicChildApp {
constructor(subService, ...rest) {
super(...rest);
this.subService = subService;
}
});

let container = new Container();

let app1 = container.get(VariadicParentApp);
expect(app1.logger).toEqual(jasmine.any(Logger));

let app2 = container.get(VariadicChildApp);
expect(app2.logger).toEqual(jasmine.any(Logger));
expect(app2.service).toEqual(jasmine.any(Service));

let app3 = container.get(VariadicGrandchildApp);
expect(app3.logger).toEqual(jasmine.any(Logger));
expect(app3.service).toEqual(jasmine.any(Service));
expect(app3.subService).toEqual(jasmine.any(SubService1));
});
});
});

0 comments on commit c093756

Please sign in to comment.