Skip to content

Commit

Permalink
chore: enable strictBindCallApply (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Jul 18, 2023
1 parent a3a1480 commit 5f947cb
Show file tree
Hide file tree
Showing 29 changed files with 109 additions and 265 deletions.
24 changes: 24 additions & 0 deletions src/internal/bind-this-to-member-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Bind all functions of the given instance to itself so you can use them independently.
*
* @internal
*
* @param instance The class instance of which the methods are to be bound to itself.
*
* @example
* const someModule = new SomeModule(faker);
* bindThisToMemberFunctions(someModule); // Usually called inside the constructor passing `this`
* const someMethod = someModule.someMethod;
* someMethod(); // Works
*/
export function bindThisToMemberFunctions<TClass extends { new (): any }>(
instance: InstanceType<TClass>
): void {
for (const name of Object.getOwnPropertyNames(
Object.getPrototypeOf(instance)
)) {
if (typeof instance[name] === 'function' && name !== 'constructor') {
instance[name] = instance[name].bind(instance);
}
}
}
12 changes: 2 additions & 10 deletions src/modules/airline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* operations.
*/
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';

export enum Aircraft {
Narrowbody = 'narrowbody',
Expand Down Expand Up @@ -79,16 +80,7 @@ const aircraftTypeSeats: Record<AircraftType, string[]> = {
*/
export class AirlineModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
AirlineModule.prototype
) as Array<keyof AirlineModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/animal/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';

/**
* Module to generate animal related entries.
Expand All @@ -13,16 +14,7 @@ import type { Faker } from '../..';
*/
export class AnimalModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
AnimalModule.prototype
) as Array<keyof AnimalModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../../faker';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';

/**
* Color space names supported by CSS.
Expand Down Expand Up @@ -172,16 +173,7 @@ function toColorFormat(
*/
export class ColorModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
ColorModule.prototype
) as Array<keyof ColorModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../../faker';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -14,16 +15,7 @@ import { deprecated } from '../../internal/deprecated';
*/
export class CommerceModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
CommerceModule.prototype
) as Array<keyof CommerceModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/company/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -17,16 +18,7 @@ import { deprecated } from '../../internal/deprecated';
*/
export class CompanyModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
CompanyModule.prototype
) as Array<keyof CompanyModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';

/**
* Module to generate database related entries.
Expand All @@ -11,16 +12,7 @@ import type { Faker } from '../..';
*/
export class DatabaseModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
DatabaseModule.prototype
) as Array<keyof DatabaseModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -12,16 +13,7 @@ import { deprecated } from '../../internal/deprecated';
*/
export class DatatypeModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
DatatypeModule.prototype
) as Array<keyof DatatypeModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Faker } from '../..';
import type { DateEntryDefinition } from '../../definitions';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand Down Expand Up @@ -39,16 +40,7 @@ function toDate(
*/
export class DateModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
DateModule.prototype
) as Array<keyof DateModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import iban from './iban';

Expand Down Expand Up @@ -38,16 +39,7 @@ export interface Currency {
*/
export class FinanceModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
FinanceModule.prototype
) as Array<keyof FinanceModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -10,16 +11,7 @@ import { deprecated } from '../../internal/deprecated';
*/
export class GitModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(GitModule.prototype) as Array<
keyof GitModule | 'constructor'
>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/hacker/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';

/**
* Module to generate hacker/IT words and phrases.
Expand All @@ -17,16 +18,7 @@ import type { Faker } from '../..';
*/
export class HackerModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
HackerModule.prototype
) as Array<keyof HackerModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { luhnCheckValue } from './luhn-check';
import type { RecordKey } from './unique';
Expand Down Expand Up @@ -92,16 +93,7 @@ export class HelpersModule {
private readonly uniqueStore: Record<RecordKey, RecordKey> = {};

constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
HelpersModule.prototype
) as Array<keyof HelpersModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
14 changes: 2 additions & 12 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import type { MethodsOf } from '../../utils/types';
import { LoremPicsum } from './providers/lorempicsum';
Expand Down Expand Up @@ -38,18 +39,7 @@ export class ImageModule {
readonly placeholder: Placeholder;

constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
ImageModule.prototype
) as Array<keyof ImageModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] =
// @ts-expect-error: remove this expect-error when we remove the deprecated sub-modules
this[name].bind(this);
}
bindThisToMemberFunctions(this);

// eslint-disable-next-line deprecation/deprecation
this.unsplash = new Unsplash(this.faker);
Expand Down
12 changes: 2 additions & 10 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { charMapping } from './char-mappings';
import * as random_ua from './user-agent';
Expand Down Expand Up @@ -39,16 +40,7 @@ export type HTTPProtocolType = 'http' | 'https';
*/
export class InternetModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
InternetModule.prototype
) as Array<keyof InternetModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -15,16 +16,7 @@ import { deprecated } from '../../internal/deprecated';
*/
export class LocationModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
LocationModule.prototype
) as Array<keyof LocationModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}

this[name] = this[name].bind(this);
}
bindThisToMemberFunctions(this);
}

/**
Expand Down
Loading

0 comments on commit 5f947cb

Please sign in to comment.