diff options
| author | Shinigami <[email protected]> | 2023-07-18 07:23:26 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-18 05:23:26 +0000 |
| commit | 5f947cbd4773f768a90243e54fd707c9769e8530 (patch) | |
| tree | 6fa92071b458fc6a56e7e2f09ed99c07e504ffb8 | |
| parent | a3a1480cb3ad9301b4e5e53ba8a281d1e170bca5 (diff) | |
| download | faker-5f947cbd4773f768a90243e54fd707c9769e8530.tar.xz faker-5f947cbd4773f768a90243e54fd707c9769e8530.zip | |
chore: enable strictBindCallApply (#2254)
29 files changed, 109 insertions, 265 deletions
diff --git a/src/internal/bind-this-to-member-functions.ts b/src/internal/bind-this-to-member-functions.ts new file mode 100644 index 00000000..db0d3885 --- /dev/null +++ b/src/internal/bind-this-to-member-functions.ts @@ -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); + } + } +} diff --git a/src/modules/airline/index.ts b/src/modules/airline/index.ts index e387cecf..f6d2fdc7 100644 --- a/src/modules/airline/index.ts +++ b/src/modules/airline/index.ts @@ -5,6 +5,7 @@ * operations. */ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; export enum Aircraft { Narrowbody = 'narrowbody', @@ -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); } /** diff --git a/src/modules/animal/index.ts b/src/modules/animal/index.ts index f4d69bbd..09d7f9e1 100644 --- a/src/modules/animal/index.ts +++ b/src/modules/animal/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * Module to generate animal related entries. @@ -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); } /** diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 9e148f04..790a6925 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../../faker'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * Color space names supported by CSS. @@ -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); } /** diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index 89f47711..f0068415 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../../faker'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { deprecated } from '../../internal/deprecated'; /** @@ -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); } /** diff --git a/src/modules/company/index.ts b/src/modules/company/index.ts index a199c591..c24ea7f3 100644 --- a/src/modules/company/index.ts +++ b/src/modules/company/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { deprecated } from '../../internal/deprecated'; /** @@ -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); } /** diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index ced5a3ef..6015cfa2 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * Module to generate database related entries. @@ -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); } /** diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 5fee2c0e..ab6998ae 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { deprecated } from '../../internal/deprecated'; /** @@ -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); } /** diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index f6ebd6da..88680ebf 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -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'; /** @@ -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); } /** diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index cbdbad54..43cd16a1 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -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'; @@ -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); } /** diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 26e6e767..f19abeef 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { deprecated } from '../../internal/deprecated'; /** @@ -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); } /** diff --git a/src/modules/hacker/index.ts b/src/modules/hacker/index.ts index 95084832..7513095a 100644 --- a/src/modules/hacker/index.ts +++ b/src/modules/hacker/index.ts @@ -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. @@ -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); } /** diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 9ecd8103..92647942 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -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'; @@ -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); } /** diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index a44c2efb..cfefdb8a 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -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'; @@ -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); diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index a6b54838..842ab153 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -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'; @@ -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); } /** diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index ce556de5..cdfa44d0 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -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'; /** @@ -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); } /** diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index 0f94a187..0f3423bc 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { filterWordListByLength } from '../word/filterWordListByLength'; /** @@ -14,16 +15,7 @@ import { filterWordListByLength } from '../word/filterWordListByLength'; */ export class LoremModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - LoremModule.prototype - ) as Array<keyof LoremModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/music/index.ts b/src/modules/music/index.ts index 6972387e..86e865ae 100644 --- a/src/modules/music/index.ts +++ b/src/modules/music/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * Module to generate music related entries. @@ -9,16 +10,7 @@ import type { Faker } from '../..'; */ export class MusicModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - MusicModule.prototype - ) as Array<keyof MusicModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index fce3829a..9472fb82 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -1,5 +1,6 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import type { Mersenne } from '../../internal/mersenne/mersenne'; /** @@ -18,16 +19,7 @@ import type { Mersenne } from '../../internal/mersenne/mersenne'; */ export class NumberModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - NumberModule.prototype - ) as Array<keyof NumberModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/person/index.ts b/src/modules/person/index.ts index 817f261c..f07727a2 100644 --- a/src/modules/person/index.ts +++ b/src/modules/person/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; export enum Sex { Female = 'female', @@ -75,16 +76,7 @@ function selectDefinition<T>( */ export class PersonModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - PersonModule.prototype - ) as Array<keyof PersonModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/phone/index.ts b/src/modules/phone/index.ts index 27b793db..b677ec4a 100644 --- a/src/modules/phone/index.ts +++ b/src/modules/phone/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { deprecated } from '../../internal/deprecated'; /** @@ -10,16 +11,7 @@ import { deprecated } from '../../internal/deprecated'; */ export class PhoneModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - PhoneModule.prototype - ) as Array<keyof PhoneModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index d0544119..7fa16f40 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -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 type { LiteralUnion } from '../../utils/types'; import type { @@ -16,18 +17,7 @@ import type { */ export class RandomModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - // eslint-disable-next-line deprecation/deprecation - RandomModule.prototype - // eslint-disable-next-line deprecation/deprecation - ) as Array<keyof RandomModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/science/index.ts b/src/modules/science/index.ts index ef85f299..95fc1290 100644 --- a/src/modules/science/index.ts +++ b/src/modules/science/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * The possible definitions related to elements. @@ -38,16 +39,7 @@ export interface Unit { */ export class ScienceModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - ScienceModule.prototype - ) as Array<keyof ScienceModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index aec6b91f..536a1c6a 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -1,5 +1,6 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import type { LiteralUnion } from '../../utils/types'; export type Casing = 'upper' | 'lower' | 'mixed'; @@ -103,16 +104,7 @@ const SAMPLE_MAX_LENGTH = 2 ** 20; */ export class StringModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - StringModule.prototype - ) as Array<keyof StringModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index a4cf6745..4e5b5574 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; const commonFileTypes = ['video', 'audio', 'image', 'text', 'application']; @@ -37,16 +38,7 @@ const CRON_DAY_OF_WEEK = [ */ export class SystemModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - SystemModule.prototype - ) as Array<keyof SystemModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index 836d9290..f5f4f1af 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; /** * Module to generate vehicle related entries. @@ -11,16 +12,7 @@ import type { Faker } from '../..'; */ export class VehicleModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - VehicleModule.prototype - ) as Array<keyof VehicleModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index 614c6a59..7cc4729a 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -1,5 +1,6 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; +import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; import { filterWordListByLength } from './filterWordListByLength'; /** @@ -7,16 +8,7 @@ import { filterWordListByLength } from './filterWordListByLength'; */ export class WordModule { constructor(private readonly faker: Faker) { - // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames( - WordModule.prototype - ) as Array<keyof WordModule | 'constructor'>) { - if (name === 'constructor' || typeof this[name] !== 'function') { - continue; - } - - this[name] = this[name].bind(this); - } + bindThisToMemberFunctions(this); } /** diff --git a/test/internal/bind-this-to-member-functions.spec.ts b/test/internal/bind-this-to-member-functions.spec.ts new file mode 100644 index 00000000..7dedc00e --- /dev/null +++ b/test/internal/bind-this-to-member-functions.spec.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from 'vitest'; +import type { Faker } from '../../src'; +import { faker } from '../../src'; +import { bindThisToMemberFunctions } from '../../src/internal/bind-this-to-member-functions'; + +describe('internal', () => { + describe('bind-this-to-member-functions', () => { + it('should bind this to member functions', () => { + class SomeModule { + constructor(private readonly faker: Faker) {} + + someMethod(): number { + return this.faker.number.int(); + } + } + + const someModule = new SomeModule(faker); + + const someMethodWithoutBind = someModule.someMethod; + + // The second error message is for NodeJS v14 support + expect(() => someMethodWithoutBind()).toThrow( + /^(Cannot read properties of undefined \(reading 'faker'\)|Cannot read property 'faker' of undefined)$/ + ); + + bindThisToMemberFunctions(someModule); + + const someMethod = someModule.someMethod; + + expect(() => someMethod()).not.toThrow(); + }); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 19053aba..3a2b814f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,6 @@ // We need to disable these for now, and need to tackle them in another PR "strictNullChecks": false, - "strictBindCallApply": false, "noImplicitAny": false, // These are configs specifically for !build and have to be reverted in the tsconfig.build.json |
