Skip to content

Commit 5f947cb

Browse files
authored
chore: enable strictBindCallApply (#2254)
1 parent a3a1480 commit 5f947cb

29 files changed

Lines changed: 109 additions & 265 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Bind all functions of the given instance to itself so you can use them independently.
3+
*
4+
* @internal
5+
*
6+
* @param instance The class instance of which the methods are to be bound to itself.
7+
*
8+
* @example
9+
* const someModule = new SomeModule(faker);
10+
* bindThisToMemberFunctions(someModule); // Usually called inside the constructor passing `this`
11+
* const someMethod = someModule.someMethod;
12+
* someMethod(); // Works
13+
*/
14+
export function bindThisToMemberFunctions<TClass extends { new (): any }>(
15+
instance: InstanceType<TClass>
16+
): void {
17+
for (const name of Object.getOwnPropertyNames(
18+
Object.getPrototypeOf(instance)
19+
)) {
20+
if (typeof instance[name] === 'function' && name !== 'constructor') {
21+
instance[name] = instance[name].bind(instance);
22+
}
23+
}
24+
}

src/modules/airline/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* operations.
66
*/
77
import type { Faker } from '../..';
8+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
89

910
export enum Aircraft {
1011
Narrowbody = 'narrowbody',
@@ -79,16 +80,7 @@ const aircraftTypeSeats: Record<AircraftType, string[]> = {
7980
*/
8081
export class AirlineModule {
8182
constructor(private readonly faker: Faker) {
82-
// Bind `this` so namespaced is working correctly
83-
for (const name of Object.getOwnPropertyNames(
84-
AirlineModule.prototype
85-
) as Array<keyof AirlineModule | 'constructor'>) {
86-
if (name === 'constructor' || typeof this[name] !== 'function') {
87-
continue;
88-
}
89-
90-
this[name] = this[name].bind(this);
91-
}
83+
bindThisToMemberFunctions(this);
9284
}
9385

9486
/**

src/modules/animal/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23

34
/**
45
* Module to generate animal related entries.
@@ -13,16 +14,7 @@ import type { Faker } from '../..';
1314
*/
1415
export class AnimalModule {
1516
constructor(private readonly faker: Faker) {
16-
// Bind `this` so namespaced is working correctly
17-
for (const name of Object.getOwnPropertyNames(
18-
AnimalModule.prototype
19-
) as Array<keyof AnimalModule | 'constructor'>) {
20-
if (name === 'constructor' || typeof this[name] !== 'function') {
21-
continue;
22-
}
23-
24-
this[name] = this[name].bind(this);
25-
}
17+
bindThisToMemberFunctions(this);
2618
}
2719

2820
/**

src/modules/color/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../../faker';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23

34
/**
45
* Color space names supported by CSS.
@@ -172,16 +173,7 @@ function toColorFormat(
172173
*/
173174
export class ColorModule {
174175
constructor(private readonly faker: Faker) {
175-
// Bind `this` so namespaced is working correctly
176-
for (const name of Object.getOwnPropertyNames(
177-
ColorModule.prototype
178-
) as Array<keyof ColorModule | 'constructor'>) {
179-
if (name === 'constructor' || typeof this[name] !== 'function') {
180-
continue;
181-
}
182-
183-
this[name] = this[name].bind(this);
184-
}
176+
bindThisToMemberFunctions(this);
185177
}
186178

187179
/**

src/modules/commerce/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../../faker';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23
import { deprecated } from '../../internal/deprecated';
34

45
/**
@@ -14,16 +15,7 @@ import { deprecated } from '../../internal/deprecated';
1415
*/
1516
export class CommerceModule {
1617
constructor(private readonly faker: Faker) {
17-
// Bind `this` so namespaced is working correctly
18-
for (const name of Object.getOwnPropertyNames(
19-
CommerceModule.prototype
20-
) as Array<keyof CommerceModule | 'constructor'>) {
21-
if (name === 'constructor' || typeof this[name] !== 'function') {
22-
continue;
23-
}
24-
25-
this[name] = this[name].bind(this);
26-
}
18+
bindThisToMemberFunctions(this);
2719
}
2820

2921
/**

src/modules/company/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23
import { deprecated } from '../../internal/deprecated';
34

45
/**
@@ -17,16 +18,7 @@ import { deprecated } from '../../internal/deprecated';
1718
*/
1819
export class CompanyModule {
1920
constructor(private readonly faker: Faker) {
20-
// Bind `this` so namespaced is working correctly
21-
for (const name of Object.getOwnPropertyNames(
22-
CompanyModule.prototype
23-
) as Array<keyof CompanyModule | 'constructor'>) {
24-
if (name === 'constructor' || typeof this[name] !== 'function') {
25-
continue;
26-
}
27-
28-
this[name] = this[name].bind(this);
29-
}
21+
bindThisToMemberFunctions(this);
3022
}
3123

3224
/**

src/modules/database/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23

34
/**
45
* Module to generate database related entries.
@@ -11,16 +12,7 @@ import type { Faker } from '../..';
1112
*/
1213
export class DatabaseModule {
1314
constructor(private readonly faker: Faker) {
14-
// Bind `this` so namespaced is working correctly
15-
for (const name of Object.getOwnPropertyNames(
16-
DatabaseModule.prototype
17-
) as Array<keyof DatabaseModule | 'constructor'>) {
18-
if (name === 'constructor' || typeof this[name] !== 'function') {
19-
continue;
20-
}
21-
22-
this[name] = this[name].bind(this);
23-
}
15+
bindThisToMemberFunctions(this);
2416
}
2517

2618
/**

src/modules/datatype/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
23
import { deprecated } from '../../internal/deprecated';
34

45
/**
@@ -12,16 +13,7 @@ import { deprecated } from '../../internal/deprecated';
1213
*/
1314
export class DatatypeModule {
1415
constructor(private readonly faker: Faker) {
15-
// Bind `this` so namespaced is working correctly
16-
for (const name of Object.getOwnPropertyNames(
17-
DatatypeModule.prototype
18-
) as Array<keyof DatatypeModule | 'constructor'>) {
19-
if (name === 'constructor' || typeof this[name] !== 'function') {
20-
continue;
21-
}
22-
23-
this[name] = this[name].bind(this);
24-
}
16+
bindThisToMemberFunctions(this);
2517
}
2618

2719
/**

src/modules/date/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Faker } from '../..';
22
import type { DateEntryDefinition } from '../../definitions';
33
import { FakerError } from '../../errors/faker-error';
4+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
45
import { deprecated } from '../../internal/deprecated';
56

67
/**
@@ -39,16 +40,7 @@ function toDate(
3940
*/
4041
export class DateModule {
4142
constructor(private readonly faker: Faker) {
42-
// Bind `this` so namespaced is working correctly
43-
for (const name of Object.getOwnPropertyNames(
44-
DateModule.prototype
45-
) as Array<keyof DateModule | 'constructor'>) {
46-
if (name === 'constructor' || typeof this[name] !== 'function') {
47-
continue;
48-
}
49-
50-
this[name] = this[name].bind(this);
51-
}
43+
bindThisToMemberFunctions(this);
5244
}
5345

5446
/**

src/modules/finance/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Faker } from '../..';
22
import { FakerError } from '../../errors/faker-error';
3+
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
34
import { deprecated } from '../../internal/deprecated';
45
import iban from './iban';
56

@@ -38,16 +39,7 @@ export interface Currency {
3839
*/
3940
export class FinanceModule {
4041
constructor(private readonly faker: Faker) {
41-
// Bind `this` so namespaced is working correctly
42-
for (const name of Object.getOwnPropertyNames(
43-
FinanceModule.prototype
44-
) as Array<keyof FinanceModule | 'constructor'>) {
45-
if (name === 'constructor' || typeof this[name] !== 'function') {
46-
continue;
47-
}
48-
49-
this[name] = this[name].bind(this);
50-
}
42+
bindThisToMemberFunctions(this);
5143
}
5244

5345
/**

0 commit comments

Comments
 (0)