diff --git a/package.json b/package.json index 15de70c65..5f4d5ed98 100644 --- a/package.json +++ b/package.json @@ -37,9 +37,9 @@ "devDependencies": { "@types/chai": "^3.4.32", "@types/harmony-proxy": "^1.0.27", - "@types/mocha": "^2.2.31", + "@types/mocha": "^ 2.2.35", "@types/sinon": "^1.16.29", - "bluebird": "^3.4.1", + "bluebird": "^3.4.7", "browserify": "^13.0.0", "chai": "^3.4.1", "es6-symbol": "^3.1.0", @@ -47,7 +47,7 @@ "gulp-istanbul": "^1.0.0", "gulp-mocha": "^3.0.0", "gulp-rename": "^1.2.2", - "gulp-sourcemaps": "^2.1.1", + "gulp-sourcemaps": "^2.2.1", "gulp-tslint": "^7.0.0", "gulp-typescript": "^3.0.0", "gulp-uglify": "^2.0.0", @@ -68,10 +68,10 @@ "mocha": "^3.0.1", "performance-now": "^0.2.0", "publish-please": "^2.1.4", - "reflect-metadata": "^0.1.3", + "reflect-metadata": "^0.1.9", "run-sequence": "^1.2.0", "sinon": "^1.17.3", - "tslint": "^4.0.1", + "tslint": "^4.2.0", "typescript": "^2.1.1", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0" diff --git a/src/annotation/decorator_utils.ts b/src/annotation/decorator_utils.ts index 06ec8f8d0..50e13f829 100644 --- a/src/annotation/decorator_utils.ts +++ b/src/annotation/decorator_utils.ts @@ -9,7 +9,7 @@ function tagParameter( metadata: interfaces.Metadata ) { let metadataKey = METADATA_KEY.TAGGED; - return _tagParameterOrProperty(metadataKey, annotationTarget, propertyName, metadata, parameterIndex); + _tagParameterOrProperty(metadataKey, annotationTarget, propertyName, metadata, parameterIndex); } function tagProperty( @@ -18,7 +18,7 @@ function tagProperty( metadata: interfaces.Metadata ) { let metadataKey = METADATA_KEY.TAGGED_PROP; - return _tagParameterOrProperty(metadataKey, annotationTarget.constructor, propertyName, metadata); + _tagParameterOrProperty(metadataKey, annotationTarget.constructor, propertyName, metadata); } function _tagParameterOrProperty( @@ -60,8 +60,6 @@ function _tagParameterOrProperty( paramOrPropertyMetadata.push(metadata); paramsOrPropertiesMetadata[key] = paramOrPropertyMetadata; Reflect.defineMetadata(metadataKey, paramsOrPropertiesMetadata, annotationTarget); - return annotationTarget; - } function _decorate(decorators: any[], target: any): void { diff --git a/src/annotation/inject.ts b/src/annotation/inject.ts index 3de065e90..75693787f 100644 --- a/src/annotation/inject.ts +++ b/src/annotation/inject.ts @@ -4,14 +4,14 @@ import { tagParameter, tagProperty } from "./decorator_utils"; import * as METADATA_KEY from "../constants/metadata_keys"; function inject(serviceIdentifier: interfaces.ServiceIdentifier) { - return function(target: any, targetKey: string, index?: number) { + return function(target: any, targetKey: string, index?: number): void { let metadata = new Metadata(METADATA_KEY.INJECT_TAG, serviceIdentifier); if (typeof index === "number") { - return tagParameter(target, targetKey, index, metadata); + tagParameter(target, targetKey, index, metadata); } else { - return tagProperty(target, targetKey, metadata); + tagProperty(target, targetKey, metadata); } }; diff --git a/src/constants/error_msgs.ts b/src/constants/error_msgs.ts index e66f26837..1686ee4fa 100644 --- a/src/constants/error_msgs.ts +++ b/src/constants/error_msgs.ts @@ -14,6 +14,9 @@ export const NO_MORE_SNAPSHOTS_AVAILABLE = "No snapshot available to restore."; export const INVALID_MIDDLEWARE_RETURN = "Invalid return type in middleware. Middleware must return!"; export const INVALID_FUNCTION_BINDING = "Value provided to function binding must be a function!"; +export const INVALID_TO_SELF_VALUE = "The toSelf function can only be applied when a constructor is " + + "used as service identifier"; + export const INVALID_DECORATOR_OPERATION = "The @inject @multiInject @tagged and @named decorators " + "must be applied to the parameters of a class constructor or a class property."; diff --git a/src/syntax/binding_to_syntax.ts b/src/syntax/binding_to_syntax.ts index 6c5a91f4c..bc426ad15 100644 --- a/src/syntax/binding_to_syntax.ts +++ b/src/syntax/binding_to_syntax.ts @@ -19,6 +19,9 @@ class BindingToSyntax implements interfaces.BindingToSyntax { } public toSelf(): interfaces.BindingInWhenOnSyntax { + if (typeof this._binding.serviceIdentifier !== "function") { + throw new Error(`${ERROR_MSGS.INVALID_TO_SELF_VALUE}`); + } let self: any = this._binding.serviceIdentifier; return this.to(self); } diff --git a/test/bugs/bugs.test.ts b/test/bugs/bugs.test.ts index a27e91692..d98340372 100644 --- a/test/bugs/bugs.test.ts +++ b/test/bugs/bugs.test.ts @@ -523,4 +523,10 @@ describe("Bugs", () => { }); + it("Should throw a friendly error when binding a non-class using toSelf", () => { + let container = new Container(); + let throws = () => { container.bind("testId").toSelf(); }; + expect(throws).to.throw(ERROR_MSGS.INVALID_TO_SELF_VALUE); + }); + }); diff --git a/test/syntax/binding_to_syntax.test.ts b/test/syntax/binding_to_syntax.test.ts index 044212cc5..5524c53c1 100644 --- a/test/syntax/binding_to_syntax.test.ts +++ b/test/syntax/binding_to_syntax.test.ts @@ -32,6 +32,7 @@ describe("BindingToSyntax", () => { let ninjaIdentifier = "Ninja"; let binding = new Binding(ninjaIdentifier, BindingScopeEnum.Transient); + // let bindingWithClassAsId = new Binding(Ninja, BindingScopeEnum.Transient); let bindingToSyntax = new BindingToSyntax(binding); expect(binding.type).eql(BindingTypeEnum.Invalid); @@ -40,10 +41,12 @@ describe("BindingToSyntax", () => { expect(binding.type).eql(BindingTypeEnum.Instance); expect(binding.implementationType).not.to.eql(null); - bindingToSyntax.toSelf(); - expect(binding.type).eql(BindingTypeEnum.Instance); - expect(binding.implementationType).not.to.eql(null); +// (bindingToSyntax as any)._binding = bindingWithClassAsId; +// bindingToSyntax.toSelf(); +// expect(binding.type).eql(BindingTypeEnum.Instance); +// expect(binding.implementationType).not.to.eql(null); + (bindingToSyntax as any)._binding = binding; bindingToSyntax.toConstantValue(new Ninja()); expect(binding.type).eql(BindingTypeEnum.ConstantValue); expect(binding.cache instanceof Ninja).eql(true);