From 95b2281110335b3690e8aff0a8751bf0977a9bf5 Mon Sep 17 00:00:00 2001 From: Prashant Tiwari Date: Thu, 29 Dec 2016 13:03:27 +0530 Subject: [PATCH 1/4] Refactor - Fix args for throw, add throws alias - Don't expose internal interfaces - Add linting --- code/code-tests.ts | 4 ++-- code/index.d.ts | 20 ++++++++++++-------- code/tslint.json | 3 +++ 3 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 code/tslint.json diff --git a/code/code-tests.ts b/code/code-tests.ts index ea956c6c2c..f84d2c852d 100644 --- a/code/code-tests.ts +++ b/code/code-tests.ts @@ -31,7 +31,7 @@ expect(err).to.be.an.error(Error); expect(err).to.be.an.error("Oops an error occured."); expect(err).to.be.an.error(Error, /occured/); -expect(function () { }).to.be.a.function(); +expect(function () { return; }).to.be.a.function(); expect(123).to.be.a.number(); @@ -147,7 +147,7 @@ fail("This should not occur"); expect(count()).to.be.a.number(); -expect(incomplete()).to.be.null().and.not.be.an.array(); +expect(incomplete() as null).to.be.null().and.not.be.an.array(); const error = thrownAt(new Error("oops")); expect(error).to.not.be.undefined(); diff --git a/code/index.d.ts b/code/index.d.ts index 174215f5d8..50c58d3a24 100644 --- a/code/index.d.ts +++ b/code/index.d.ts @@ -1,20 +1,22 @@ -// Type definitions for code 4.0.0 +// Type definitions for code 4.0 // Project: https://github.com/hapijs/code // Definitions by: Prashant Tiwari // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** Generates an assertion object. */ -export function expect(value: T | T[], prefix?: string): AssertionChain; +declare function expect(value: T | T[], prefix?: string): AssertionChain; /** Makes the test fail with the given message. */ -export function fail(message: string): void; +declare function fail(message: string): void; /** Returns the total number of assertions created using the expect() method. */ -export function count(): number; +declare function count(): number; /** Returns an array of the locations where incomplete assertions were declared or null if no incomplete assertions found. */ -export function incomplete(): Array | null; +declare function incomplete(): Array | null; /** Returns the filename, line number, and column number of where the error was created. */ -export function thrownAt(error?: Error): CodeError; +declare function thrownAt(error?: Error): CodeError; /** Configure code. */ -export const settings: Settings; +declare const settings: Settings; + +export { expect, fail, count, incomplete, thrownAt, settings }; type AssertionChain = Assertion & Expectation; @@ -165,7 +167,9 @@ interface Values { /** Asserts that the reference value satisfies the provided validator function. */ satisfies(validator: (value: T) => boolean): AssertionChain; /** Asserts that the function reference value throws an exception when called. */ - throw(type: Object, message: string | RegExp): AssertionChain; + throw(type?: Object, message?: string | RegExp): AssertionChain; + /** Asserts that the function reference value throws an exception when called. */ + throws(type?: Object, message?: string | RegExp): AssertionChain; } interface Settings { diff --git a/code/tslint.json b/code/tslint.json new file mode 100644 index 0000000000..192203ab54 --- /dev/null +++ b/code/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../tslint.json" +} \ No newline at end of file From 0b9e00f058f7b29fec659f98eb7c77dbec01b4d9 Mon Sep 17 00:00:00 2001 From: Prashant Tiwari Date: Thu, 29 Dec 2016 13:20:31 +0530 Subject: [PATCH 2/4] Fix linter warnings --- code/code-tests.ts | 2 +- code/index.d.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/code/code-tests.ts b/code/code-tests.ts index f84d2c852d..f90d2c9eb7 100644 --- a/code/code-tests.ts +++ b/code/code-tests.ts @@ -31,7 +31,7 @@ expect(err).to.be.an.error(Error); expect(err).to.be.an.error("Oops an error occured."); expect(err).to.be.an.error(Error, /occured/); -expect(function () { return; }).to.be.a.function(); +expect(func).to.be.a.function(); expect(123).to.be.a.number(); diff --git a/code/index.d.ts b/code/index.d.ts index 50c58d3a24..db59f6bf93 100644 --- a/code/index.d.ts +++ b/code/index.d.ts @@ -10,7 +10,7 @@ declare function fail(message: string): void; /** Returns the total number of assertions created using the expect() method. */ declare function count(): number; /** Returns an array of the locations where incomplete assertions were declared or null if no incomplete assertions found. */ -declare function incomplete(): Array | null; +declare function incomplete(): string[] | null; /** Returns the filename, line number, and column number of where the error was created. */ declare function thrownAt(error?: Error): CodeError; /** Configure code. */ @@ -20,9 +20,9 @@ export { expect, fail, count, incomplete, thrownAt, settings }; type AssertionChain = Assertion & Expectation; -interface Assertion extends Grammar, Flags { } +type Assertion = Grammar & Flags; -interface Expectation extends Types, Values { } +type Expectation = Types & Values; interface Grammar { /** Connecting word. */ @@ -80,7 +80,7 @@ interface Types { /** Asserts that the reference value is a Date. */ date(): AssertionChain; /** Asserts that the reference value is an error. */ - error(type?: Object, message?: string | RegExp): AssertionChain; + error(type?: any, message?: string | RegExp): AssertionChain; /** Asserts that the reference value is a function. */ function(): AssertionChain; /** Asserts that the reference value is a number. */ @@ -155,9 +155,9 @@ interface Values { /** Asserts that the reference value is about the provided value within a delta margin of difference. */ about(value: number, delta: number): AssertionChain; /** Asserts that the reference value has the provided instanceof value. */ - instanceof(type: Object): AssertionChain; + instanceof(type: any): AssertionChain; /** Asserts that the reference value has the provided instanceof value. */ - instanceOf(type: Object): AssertionChain; + instanceOf(type: any): AssertionChain; /** Asserts that the reference value's toString() representation matches the provided regular expression. */ match(regex: RegExp): AssertionChain; /** Asserts that the reference value's toString() representation matches the provided regular expression. */ @@ -167,9 +167,9 @@ interface Values { /** Asserts that the reference value satisfies the provided validator function. */ satisfies(validator: (value: T) => boolean): AssertionChain; /** Asserts that the function reference value throws an exception when called. */ - throw(type?: Object, message?: string | RegExp): AssertionChain; + throw(type?: any, message?: string | RegExp): AssertionChain; /** Asserts that the function reference value throws an exception when called. */ - throws(type?: Object, message?: string | RegExp): AssertionChain; + throws(type?: any, message?: string | RegExp): AssertionChain; } interface Settings { From 2ae5bdd2cfe3a347eff2daf184508e5a7e92e06f Mon Sep 17 00:00:00 2001 From: Prashant Tiwari Date: Fri, 30 Dec 2016 00:18:52 +0530 Subject: [PATCH 3/4] Revert to public interfaces --- code/index.d.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/code/index.d.ts b/code/index.d.ts index db59f6bf93..37cbdb224f 100644 --- a/code/index.d.ts +++ b/code/index.d.ts @@ -4,19 +4,17 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /** Generates an assertion object. */ -declare function expect(value: T | T[], prefix?: string): AssertionChain; +export function expect(value: T | T[], prefix?: string): AssertionChain; /** Makes the test fail with the given message. */ -declare function fail(message: string): void; +export function fail(message: string): void; /** Returns the total number of assertions created using the expect() method. */ -declare function count(): number; -/** Returns an array of the locations where incomplete assertions were declared or null if no incomplete assertions found. */ -declare function incomplete(): string[] | null; +export function count(): number; +/** Returns an array of the locations where incomplete assertions were exportd or null if no incomplete assertions found. */ +export function incomplete(): string[] | null; /** Returns the filename, line number, and column number of where the error was created. */ -declare function thrownAt(error?: Error): CodeError; +export function thrownAt(error?: Error): CodeError; /** Configure code. */ -declare const settings: Settings; - -export { expect, fail, count, incomplete, thrownAt, settings }; +export const settings: Settings; type AssertionChain = Assertion & Expectation; From 712495b041eda7a3e64c4bd2bd8c8ec190edb8e6 Mon Sep 17 00:00:00 2001 From: Prashant Tiwari Date: Fri, 30 Dec 2016 00:29:10 +0530 Subject: [PATCH 4/4] Fix typo --- code/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/index.d.ts b/code/index.d.ts index 37cbdb224f..dacd122915 100644 --- a/code/index.d.ts +++ b/code/index.d.ts @@ -9,7 +9,7 @@ export function expect(value: T | T[], prefix?: string): AssertionChain; export function fail(message: string): void; /** Returns the total number of assertions created using the expect() method. */ export function count(): number; -/** Returns an array of the locations where incomplete assertions were exportd or null if no incomplete assertions found. */ +/** Returns an array of the locations where incomplete assertions were declared or null if no incomplete assertions found. */ export function incomplete(): string[] | null; /** Returns the filename, line number, and column number of where the error was created. */ export function thrownAt(error?: Error): CodeError;