Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ src/**/*.js*
*.d.ts
dist
usage.js
CURRENT-CHANGELOG.txt
CURRENT-CHANGELOG.txt
yarn.lock
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"gulp-tslint": "8.1.2",
"gulp-typescript": "3.2.2",
"mocha": "3.5.3",
"reflect-metadata": "^0.1.12",
"tslint": "5.7.0",
"tslint-microsoft-contrib": "5.0.1",
"typescript": "2.5.3"
Expand Down
12 changes: 12 additions & 0 deletions src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IInjectionMd, ProviderToken } from './interfaces';
import { INJECTABLE_MD_KEY, INJECTIONS_MD_KEY } from './metadata/keys';
import { IMetadataAnnotator } from './metadata/metadata-annotator.interface';
import { AnnotatorProvider } from './metadata/index';
import 'reflect-metadata';

const MetadataAnnotator: IMetadataAnnotator = AnnotatorProvider.get();

Expand All @@ -14,6 +15,17 @@ export function Injectable(injections?: ProviderToken[]) {
injections.forEach(token => injectionsMd.push(token));
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionsMd, target);
}

const parameters = Reflect.getMetadata('design:paramtypes', target);
if (parameters && Array.isArray(parameters)) {
const injectionsMd: IInjectionMd[] = MetadataAnnotator.getMetadata(INJECTIONS_MD_KEY, target) || [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Thank you for your pull request, I really appreciate your input. Do some changes so I can merge it, just simplify this expression so it can be read and understood easily by others. Getting rid of logical "OR" and putting things in self-describing variables might suffice.

parameters.forEach((param, index) => {
if (param['DI:injectable'] && !injectionsMd[index]) {
injectionsMd[index] = param;
}
});
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionsMd, target);
}
};
}

Expand Down
18 changes: 18 additions & 0 deletions src/tests/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Container } from '../lib/index';
import 'mocha';
import { expect } from 'chai';
import { Inject, Injectable } from '../lib/decorators';
import { RegistrationProvider } from '../lib/interfaces';

/* tslint:disable: no-unused-expression max-classes-per-file*/

Expand Down Expand Up @@ -34,6 +35,23 @@ describe('Decorators', () => {
expect(instanceB.a).to.be.instanceOf(A);
});

it('should inject dependency in to the constructor of a resolved class wihout @Inject keyword', () => {

@Injectable()
class A { }

@Injectable()
class B {
constructor(private a: A) { }
}

container.register([A, B]);

const instanceB = container.resolve(B);

expect(instanceB.a).to.be.instanceOf(A);
});

it('should throw an error when wrong token was provided not registered token', () => {
@Injectable()
class A {}
Expand Down