Skip to content

Commit

Permalink
Enable ESLint explicit-function-return-type rule (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
TagawaHirotaka authored Feb 13, 2021
1 parent 4dcb6dd commit f26115d
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 182 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"rules": {
"no-useless-return": "off",
"ava/no-ignored-test-files": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off",
Expand Down
2 changes: 1 addition & 1 deletion source/argument-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {generateStackTrace} from './utils/generate-stack';

const wrapStackTrace = (error: ArgumentError, stack: string) => `${error.name}: ${error.message}\n${stack}`;
const wrapStackTrace = (error: ArgumentError, stack: string): string => `${error.name}: ${error.message}\n${stack}`;

/**
@hidden
Expand Down
8 changes: 4 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface ReusableValidator<T> {
(value: T, label?: string): void;
}

const ow = <T>(value: T, labelOrPredicate: unknown, predicate?: BasePredicate<T>) => {
const ow = <T>(value: T, labelOrPredicate: unknown, predicate?: BasePredicate<T>): void => {
if (!isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') {
throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``);
}
Expand All @@ -79,7 +79,7 @@ const ow = <T>(value: T, labelOrPredicate: unknown, predicate?: BasePredicate<T>

Object.defineProperties(ow, {
isValid: {
value: <T>(value: T, predicate: BasePredicate<T>) => {
value: <T>(value: T, predicate: BasePredicate<T>): boolean => {
try {
ow(value, predicate);
return true;
Expand All @@ -89,11 +89,11 @@ Object.defineProperties(ow, {
}
},
create: {
value: <T>(labelOrPredicate: BasePredicate<T> | string | undefined, predicate?: BasePredicate<T>) => (value: T, label?: string) => {
value: <T>(labelOrPredicate: BasePredicate<T> | string | undefined, predicate?: BasePredicate<T>) => (value: T, label?: string): void => {
if (isPredicate(labelOrPredicate)) {
const stackFrames = callsites();

test(value, label ?? (() => inferLabel(stackFrames)), labelOrPredicate);
test(value, label ?? ((): string | void => inferLabel(stackFrames)), labelOrPredicate);

return;
}
Expand Down
2 changes: 1 addition & 1 deletion source/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Modifiers {
export default <T>(object: T): T & Modifiers => {
Object.defineProperties(object, {
optional: {
get: () => predicates({}, {optional: true})
get: (): Predicates => predicates({}, {optional: true})
}
});

Expand Down
70 changes: 35 additions & 35 deletions source/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,109 +207,109 @@ export interface Predicates {
export default <T>(object: T, options?: PredicateOptions): T & Predicates => {
Object.defineProperties(object, {
string: {
get: () => new StringPredicate(options)
get: (): StringPredicate => new StringPredicate(options)
},
number: {
get: () => new NumberPredicate(options)
get: (): NumberPredicate => new NumberPredicate(options)
},
boolean: {
get: () => new BooleanPredicate(options)
get: (): BooleanPredicate => new BooleanPredicate(options)
},
undefined: {
get: () => new Predicate('undefined', options)
get: (): Predicate => new Predicate('undefined', options)
},
null: {
get: () => new Predicate('null', options)
get: (): Predicate => new Predicate('null', options)
},
nullOrUndefined: {
get: () => new Predicate('nullOrUndefined', options)
get: (): Predicate => new Predicate('nullOrUndefined', options)
},
nan: {
get: () => new Predicate('nan', options)
get: (): Predicate => new Predicate('nan', options)
},
symbol: {
get: () => new Predicate('symbol', options)
get: (): Predicate => new Predicate('symbol', options)
},
array: {
get: () => new ArrayPredicate(options)
get: (): ArrayPredicate => new ArrayPredicate(options)
},
object: {
get: () => new ObjectPredicate(options)
get: (): ObjectPredicate => new ObjectPredicate(options)
},
date: {
get: () => new DatePredicate(options)
get: (): DatePredicate => new DatePredicate(options)
},
error: {
get: () => new ErrorPredicate(options)
get: (): ErrorPredicate => new ErrorPredicate(options)
},
map: {
get: () => new MapPredicate(options)
get: (): MapPredicate => new MapPredicate(options)
},
weakMap: {
get: () => new WeakMapPredicate(options)
get: (): WeakMapPredicate => new WeakMapPredicate(options)
},
set: {
get: () => new SetPredicate(options)
get: (): SetPredicate => new SetPredicate(options)
},
weakSet: {
get: () => new WeakSetPredicate(options)
get: (): WeakSetPredicate => new WeakSetPredicate(options)
},
function: {
get: () => new Predicate('Function', options)
get: (): Predicate => new Predicate('Function', options)
},
buffer: {
get: () => new Predicate('Buffer', options)
get: (): Predicate => new Predicate('Buffer', options)
},
regExp: {
get: () => new Predicate('RegExp', options)
get: (): Predicate<RegExp> => new Predicate('RegExp', options)
},
promise: {
get: () => new Predicate('Promise', options)
get: (): Predicate => new Predicate('Promise', options)
},
typedArray: {
get: () => new TypedArrayPredicate('TypedArray', options)
get: (): TypedArrayPredicate<TypedArray> => new TypedArrayPredicate('TypedArray', options)
},
int8Array: {
get: () => new TypedArrayPredicate('Int8Array', options)
get: (): TypedArrayPredicate<Int8Array> => new TypedArrayPredicate('Int8Array', options)
},
uint8Array: {
get: () => new TypedArrayPredicate('Uint8Array', options)
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8Array', options)
},
uint8ClampedArray: {
get: () => new TypedArrayPredicate('Uint8ClampedArray', options)
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8ClampedArray', options)
},
int16Array: {
get: () => new TypedArrayPredicate('Int16Array', options)
get: (): TypedArrayPredicate<Int16Array> => new TypedArrayPredicate('Int16Array', options)
},
uint16Array: {
get: () => new TypedArrayPredicate('Uint16Array', options)
get: (): TypedArrayPredicate<Uint16Array> => new TypedArrayPredicate('Uint16Array', options)
},
int32Array: {
get: () => new TypedArrayPredicate('Int32Array', options)
get: (): TypedArrayPredicate<Int32Array> => new TypedArrayPredicate('Int32Array', options)
},
uint32Array: {
get: () => new TypedArrayPredicate('Uint32Array', options)
get: (): TypedArrayPredicate<Uint32Array> => new TypedArrayPredicate('Uint32Array', options)
},
float32Array: {
get: () => new TypedArrayPredicate('Float32Array', options)
get: (): TypedArrayPredicate<Float32Array> => new TypedArrayPredicate('Float32Array', options)
},
float64Array: {
get: () => new TypedArrayPredicate('Float64Array', options)
get: (): TypedArrayPredicate<Float64Array> => new TypedArrayPredicate('Float64Array', options)
},
arrayBuffer: {
get: () => new ArrayBufferPredicate('ArrayBuffer', options)
get: (): ArrayBufferPredicate<ArrayBuffer> => new ArrayBufferPredicate('ArrayBuffer', options)
},
sharedArrayBuffer: {
get: () => new ArrayBufferPredicate('SharedArrayBuffer', options)
get: (): ArrayBufferPredicate<SharedArrayBuffer> => new ArrayBufferPredicate('SharedArrayBuffer', options)
},
dataView: {
get: () => new DataViewPredicate(options)
get: (): DataViewPredicate => new DataViewPredicate(options)
},
iterable: {
get: () => new Predicate('Iterable', options)
get: (): Predicate => new Predicate('Iterable', options)
},
any: {
value: (...predicates: BasePredicate[]) => new AnyPredicate(predicates, options)
value: (...predicates: BasePredicate[]): AnyPredicate => new AnyPredicate(predicates, options)
}
});

Expand Down
6 changes: 3 additions & 3 deletions source/predicates/array-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class ArrayBufferPredicate<T extends ArrayBufferLike> extends Predicate<T
@param byteLength - The byte length of the array buffer.
*/
byteLength(byteLength: number) {
byteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength === byteLength
Expand All @@ -18,7 +18,7 @@ export class ArrayBufferPredicate<T extends ArrayBufferLike> extends Predicate<T
@param byteLength - The minimum byte length of the array buffer.
*/
minByteLength(byteLength: number) {
minByteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength >= byteLength,
Expand All @@ -31,7 +31,7 @@ export class ArrayBufferPredicate<T extends ArrayBufferLike> extends Predicate<T
@param length - The minimum byte length of the array buffer.
*/
maxByteLength(byteLength: number) {
maxByteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength <= byteLength,
Expand Down
22 changes: 11 additions & 11 deletions source/predicates/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param length - The length of the array.
*/
length(length: number) {
length(length: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have length \`${length}\`, got \`${value.length}\``,
validator: value => value.length === length
Expand All @@ -30,7 +30,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param length - The minimum length of the array.
*/
minLength(length: number) {
minLength(length: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a minimum length of \`${length}\`, got \`${value.length}\``,
validator: value => value.length >= length,
Expand All @@ -43,7 +43,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param length - The maximum length of the array.
*/
maxLength(length: number) {
maxLength(length: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a maximum length of \`${length}\`, got \`${value.length}\``,
validator: value => value.length <= length,
Expand All @@ -56,7 +56,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param searchElement - The value that should be the start of the array.
*/
startsWith(searchElement: T) {
startsWith(searchElement: T): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to start with \`${searchElement}\`, got \`${value[0]}\``,
validator: value => value[0] === searchElement
Expand All @@ -68,7 +68,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param searchElement - The value that should be the end of the array.
*/
endsWith(searchElement: T) {
endsWith(searchElement: T): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to end with \`${searchElement}\`, got \`${value[value.length - 1]}\``,
validator: value => value[value.length - 1] === searchElement
Expand All @@ -80,7 +80,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param searchElements - The values that should be included in the array.
*/
includes(...searchElements: readonly T[]) {
includes(...searchElements: readonly T[]): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to include all elements of \`${JSON.stringify(searchElements)}\`, got \`${JSON.stringify(value)}\``,
validator: value => searchElements.every(element => value.includes(element))
Expand All @@ -92,7 +92,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param searchElements - The values that should be included in the array.
*/
includesAny(...searchElements: readonly T[]) {
includesAny(...searchElements: readonly T[]): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to include any element of \`${JSON.stringify(searchElements)}\`, got \`${JSON.stringify(value)}\``,
validator: value => searchElements.some(element => value.includes(element))
Expand All @@ -102,7 +102,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
/**
Test an array to be empty.
*/
get empty() {
get empty(): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to be empty, got \`${JSON.stringify(value)}\``,
validator: value => value.length === 0
Expand All @@ -112,7 +112,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
/**
Test an array to be not empty.
*/
get nonEmpty() {
get nonEmpty(): this {
return this.addValidator({
message: (_, label) => `Expected ${label} to not be empty`,
validator: value => value.length > 0
Expand All @@ -124,7 +124,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
@param expected - Expected value to match.
*/
deepEqual(expected: readonly T[]) {
deepEqual(expected: readonly T[]): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify(expected)}\`, got \`${JSON.stringify(value)}\``,
validator: value => isEqual(value, expected)
Expand Down Expand Up @@ -172,7 +172,7 @@ export class ArrayPredicate<T = unknown> extends Predicate<T[]> {
ow(['1', 2], ow.array.exactShape([ow.string, ow.number]));
```
*/
exactShape(predicates: Predicate[]) {
exactShape(predicates: Predicate[]): this {
const shape = predicates as unknown as Shape;

return this.addValidator({
Expand Down
4 changes: 2 additions & 2 deletions source/predicates/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class BooleanPredicate extends Predicate<boolean> {
/**
Test a boolean to be true.
*/
get true() {
get true(): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to be true, got ${value}`,
validator: value => value
Expand All @@ -21,7 +21,7 @@ export class BooleanPredicate extends Predicate<boolean> {
/**
Test a boolean to be false.
*/
get false() {
get false(): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to be false, got ${value}`,
validator: value => !value
Expand Down
6 changes: 3 additions & 3 deletions source/predicates/data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DataViewPredicate extends Predicate<DataView> {
@param byteLength - The byte length of the DataView.
*/
byteLength(byteLength: number) {
byteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength === byteLength
Expand All @@ -25,7 +25,7 @@ export class DataViewPredicate extends Predicate<DataView> {
@param byteLength - The minimum byte length of the DataView.
*/
minByteLength(byteLength: number) {
minByteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength >= byteLength,
Expand All @@ -38,7 +38,7 @@ export class DataViewPredicate extends Predicate<DataView> {
@param length - The minimum byte length of the DataView.
*/
maxByteLength(byteLength: number) {
maxByteLength(byteLength: number): this {
return this.addValidator({
message: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength}\`, got \`${value.byteLength}\``,
validator: value => value.byteLength <= byteLength,
Expand Down
4 changes: 2 additions & 2 deletions source/predicates/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DatePredicate extends Predicate<Date> {
@param date - Maximum value.
*/
before(date: Date) {
before(date: Date): this {
return this.addValidator({
message: (value, label) => `Expected ${label} ${value.toISOString()} to be before ${date.toISOString()}`,
validator: value => value.getTime() < date.getTime()
Expand All @@ -25,7 +25,7 @@ export class DatePredicate extends Predicate<Date> {
@param date - Minimum value.
*/
after(date: Date) {
after(date: Date): this {
return this.addValidator({
message: (value, label) => `Expected ${label} ${value.toISOString()} to be after ${date.toISOString()}`,
validator: value => value.getTime() > date.getTime()
Expand Down
Loading

0 comments on commit f26115d

Please sign in to comment.