Skip to content

Commit 2e9ed96

Browse files
authored
chore: rename generics (#2046)
1 parent 0740aa0 commit 2e9ed96

File tree

12 files changed

+102
-96
lines changed

12 files changed

+102
-96
lines changed

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ module.exports = defineConfig({
4848
leadingUnderscore: 'forbid',
4949
trailingUnderscore: 'forbid',
5050
},
51+
{
52+
format: ['PascalCase'],
53+
selector: ['typeParameter'],
54+
prefix: ['T'],
55+
leadingUnderscore: 'forbid',
56+
trailingUnderscore: 'forbid',
57+
},
5158
],
5259
'@typescript-eslint/no-inferrable-types': [
5360
'error',

scripts/apidoc/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export function adjustUrls(description: string): string {
4747
return description.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/');
4848
}
4949

50-
export function mapByName<T extends { name: string }, V>(
51-
input: T[],
52-
valueExtractor: (item: T) => V
53-
): Record<string, V> {
50+
export function mapByName<TInput extends { name: string }, TValue>(
51+
input: TInput[],
52+
valueExtractor: (item: TInput) => TValue
53+
): Record<string, TValue> {
5454
return input.reduce(
5555
(acc, item) => ({ ...acc, [item.name]: valueExtractor(item) }),
5656
{}

scripts/generateLocales.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ const pathDocsGuideLocalization = resolve(
4040
);
4141

4242
// Workaround for nameOf<T>
43-
type PascalCase<S extends string> = S extends `${infer P1}_${infer P2}`
44-
? `${Capitalize<P1>}${PascalCase<P2>}`
45-
: Capitalize<S>;
43+
type PascalCase<TName extends string> =
44+
TName extends `${infer Prefix}_${infer Remainder}`
45+
? `${Capitalize<Prefix>}${PascalCase<Remainder>}`
46+
: Capitalize<TName>;
4647

4748
type DefinitionType = {
4849
[key in keyof LocaleDefinition]-?: PascalCase<`${key}Definition`>;

src/definitions/definitions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import type { WordDefinition } from './word';
2222
/**
2323
* Wrapper type for all definition categories that will make all properties optional and allow extra properties.
2424
*/
25-
export type LocaleEntry<T extends Record<string, unknown>> = {
26-
[P in keyof T]?: T[P] | null;
25+
export type LocaleEntry<TCategoryDefinition extends Record<string, unknown>> = {
26+
[P in keyof TCategoryDefinition]?: TCategoryDefinition[P] | null;
2727
} & Record<string, unknown>; // Unsupported & custom entries
2828

2929
/**

src/locale-proxy.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy {
6161
* @param categoryData The module to create the proxy for.
6262
*/
6363
function createCategoryProxy<
64-
CategoryData extends Record<string | symbol, unknown>
64+
TCategoryData extends Record<string | symbol, unknown>
6565
>(
6666
categoryName: string,
67-
categoryData: CategoryData = {} as CategoryData
68-
): Required<CategoryData> {
67+
categoryData: TCategoryData = {} as TCategoryData
68+
): Required<TCategoryData> {
6969
return new Proxy(categoryData, {
70-
has(target: CategoryData, entryName: keyof CategoryData): boolean {
70+
has(target: TCategoryData, entryName: keyof TCategoryData): boolean {
7171
const value = target[entryName];
7272
return value != null;
7373
},
7474

7575
get(
76-
target: CategoryData,
77-
entryName: keyof CategoryData
78-
): CategoryData[keyof CategoryData] {
76+
target: TCategoryData,
77+
entryName: keyof TCategoryData
78+
): TCategoryData[keyof TCategoryData] {
7979
const value = target[entryName];
8080
if (typeof entryName === 'symbol' || entryName === 'nodeType') {
8181
return value;
@@ -97,5 +97,5 @@ function createCategoryProxy<
9797

9898
set: throwReadOnlyError,
9999
deleteProperty: throwReadOnlyError,
100-
}) as Required<CategoryData>;
100+
}) as Required<TCategoryData>;
101101
}

src/modules/helpers/index.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ export class HelpersModule {
732732
/**
733733
* Returns the result of the callback if the probability check was successful, otherwise `undefined`.
734734
*
735-
* @template T The type of result of the given callback.
735+
* @template TResult The type of result of the given callback.
736736
*
737737
* @param callback The callback to that will be invoked if the probability check was successful.
738738
* @param options The options to use. Defaults to `{}`.
@@ -745,8 +745,8 @@ export class HelpersModule {
745745
*
746746
* @since 6.3.0
747747
*/
748-
maybe<T>(
749-
callback: () => T,
748+
maybe<TResult>(
749+
callback: () => TResult,
750750
options: {
751751
/**
752752
* The probability (`[0.00, 1.00]`) of the callback being invoked.
@@ -755,7 +755,7 @@ export class HelpersModule {
755755
*/
756756
probability?: number;
757757
} = {}
758-
): T | undefined {
758+
): TResult | undefined {
759759
if (this.faker.datatype.boolean(options)) {
760760
return callback();
761761
}
@@ -990,7 +990,7 @@ export class HelpersModule {
990990
*
991991
* This does the same as `objectValue` except that it ignores (the values assigned to) the numeric keys added for TypeScript enums.
992992
*
993-
* @template EnumType Type of generic enums, automatically inferred by TypeScript.
993+
* @template T Type of generic enums, automatically inferred by TypeScript.
994994
*
995995
* @param enumObject Enum to pick the value from.
996996
*
@@ -1006,11 +1006,11 @@ export class HelpersModule {
10061006
*
10071007
* @since 8.0.0
10081008
*/
1009-
enumValue<EnumType extends Record<string | number, string | number>>(
1010-
enumObject: EnumType
1011-
): EnumType[keyof EnumType] {
1009+
enumValue<T extends Record<string | number, string | number>>(
1010+
enumObject: T
1011+
): T[keyof T] {
10121012
// ignore numeric keys added by TypeScript
1013-
const keys: Array<keyof EnumType> = Object.keys(enumObject).filter((key) =>
1013+
const keys: Array<keyof T> = Object.keys(enumObject).filter((key) =>
10141014
isNaN(Number(key))
10151015
);
10161016
const randomKey = this.arrayElement(keys);
@@ -1280,7 +1280,7 @@ export class HelpersModule {
12801280
* Generates a unique result using the results of the given method.
12811281
* Used unique entries will be stored internally and filtered from subsequent calls.
12821282
*
1283-
* @template Method The type of the method to execute.
1283+
* @template TMethod The type of the method to execute.
12841284
*
12851285
* @param method The method used to generate the values.
12861286
* @param args The arguments used to call the method.
@@ -1304,14 +1304,14 @@ export class HelpersModule {
13041304
* More info can be found in issue [faker-js/faker #1785](https://github.com/faker-js/faker/issues/1785).
13051305
*/
13061306
unique<
1307-
Method extends (
1307+
TMethod extends (
13081308
// TODO @Shinigami92 2023-02-14: This `any` type can be fixed by anyone if they want to.
13091309
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13101310
...parameters: any[]
13111311
) => RecordKey
13121312
>(
1313-
method: Method,
1314-
args: Parameters<Method> = [] as Parameters<Method>,
1313+
method: TMethod,
1314+
args: Parameters<TMethod> = [] as Parameters<TMethod>,
13151315
options: {
13161316
/**
13171317
* This parameter does nothing.
@@ -1358,7 +1358,7 @@ export class HelpersModule {
13581358
*/
13591359
store?: Record<RecordKey, RecordKey>;
13601360
} = {}
1361-
): ReturnType<Method> {
1361+
): ReturnType<TMethod> {
13621362
deprecated({
13631363
deprecated: 'faker.helpers.unique',
13641364
proposed:
@@ -1387,7 +1387,7 @@ export class HelpersModule {
13871387
/**
13881388
* Generates an array containing values returned by the given method.
13891389
*
1390-
* @template T The type of elements.
1390+
* @template TResult The type of elements.
13911391
*
13921392
* @param method The method used to generate the values.
13931393
* @param options The optional options object.
@@ -1399,8 +1399,8 @@ export class HelpersModule {
13991399
*
14001400
* @since 8.0.0
14011401
*/
1402-
multiple<T>(
1403-
method: () => T,
1402+
multiple<TResult>(
1403+
method: () => TResult,
14041404
options: {
14051405
/**
14061406
* The number or range of elements to generate.
@@ -1420,7 +1420,7 @@ export class HelpersModule {
14201420
max: number;
14211421
};
14221422
} = {}
1423-
): T[] {
1423+
): TResult[] {
14241424
const count = this.rangeToNumber(options.count ?? 3);
14251425
if (count <= 0) {
14261426
return [];

src/modules/helpers/unique.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`
5757
* Generates a unique result using the results of the given method.
5858
* Used unique entries will be stored internally and filtered from subsequent calls.
5959
*
60-
* @template Method The type of the method to execute.
60+
* @template TMethod The type of the method to execute.
6161
*
6262
* @param method The method used to generate the values.
6363
* @param args The arguments used to call the method.
@@ -71,14 +71,14 @@ Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`
7171
* @param options.store The store of unique entries. Defaults to `GLOBAL_UNIQUE_STORE`.
7272
*/
7373
export function exec<
74-
Method extends (
74+
TMethod extends (
7575
// TODO @Shinigami92 2023-02-14: This `any` type can be fixed by anyone if they want to.
7676
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7777
...parameters: any[]
7878
) => RecordKey
7979
>(
80-
method: Method,
81-
args: Parameters<Method>,
80+
method: TMethod,
81+
args: Parameters<TMethod>,
8282
options: {
8383
startTime?: number;
8484
maxTime?: number;
@@ -88,7 +88,7 @@ export function exec<
8888
compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1;
8989
store?: Record<RecordKey, RecordKey>;
9090
} = {}
91-
): ReturnType<Method> {
91+
): ReturnType<TMethod> {
9292
const now = new Date().getTime();
9393

9494
const {
@@ -132,7 +132,7 @@ export function exec<
132132
}
133133

134134
// Execute the provided method to find a potential satisfied value.
135-
const result: ReturnType<Method> = method(...args) as ReturnType<Method>;
135+
const result: ReturnType<TMethod> = method(...args) as ReturnType<TMethod>;
136136

137137
// If the result has not been previously found, add it to the found array and return the value as it's unique.
138138
if (compare(store, result) === -1 && exclude.indexOf(result) === -1) {

src/utils/types.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*
44
* @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
55
*/
6-
export type LiteralUnion<T extends U, U = string> =
7-
| T
8-
| (U & { zz_IGNORE_ME?: never });
6+
export type LiteralUnion<TSuggested extends TBase, TBase = string> =
7+
| TSuggested
8+
| (TBase & { zz_IGNORE_ME?: never });
99

1010
/**
1111
* A function that returns a value.
@@ -22,18 +22,18 @@ export type Callable = (
2222
/**
2323
* Type that represents a single method/function name of the given type.
2424
*/
25-
export type MethodOf<ObjectType, Signature extends Callable = Callable> = {
26-
[Key in keyof ObjectType]: ObjectType[Key] extends Signature
25+
export type MethodOf<TObjectType, TSignature extends Callable = Callable> = {
26+
[Key in keyof TObjectType]: TObjectType[Key] extends TSignature
2727
? Key extends string
2828
? Key
2929
: never
3030
: never;
31-
}[keyof ObjectType];
31+
}[keyof TObjectType];
3232

3333
/**
3434
* Type that represents all method/function names of the given type.
3535
*/
3636
export type MethodsOf<
37-
ObjectType,
38-
Signature extends Callable = Callable
39-
> = ReadonlyArray<MethodOf<ObjectType, Signature>>;
37+
TObjectType,
38+
TSignature extends Callable = Callable
39+
> = ReadonlyArray<MethodOf<TObjectType, TSignature>>;

test/all_functional.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function isMethodOf(mod: string) {
1818
return (meth: string) => typeof fakerEN[mod][meth] === 'function';
1919
}
2020

21-
type SkipConfig<Module> = Partial<
22-
Record<keyof Module, '*' | ReadonlyArray<keyof typeof allLocales>>
21+
type SkipConfig<TModule> = Partial<
22+
Record<keyof TModule, '*' | ReadonlyArray<keyof typeof allLocales>>
2323
>;
2424

2525
const BROKEN_LOCALE_METHODS = {

test/scripts/apidoc/__snapshots__/signature.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports[`signature > analyzeSignature() > complexArrayParameter 1`] = `
4343
"returns": "T",
4444
"seeAlsos": [],
4545
"since": "",
46-
"sourcePath": "test/scripts/apidoc/signature.example.ts#L356",
46+
"sourcePath": "test/scripts/apidoc/signature.example.ts#L357",
4747
"throws": undefined,
4848
}
4949
`;

test/scripts/apidoc/signature.example.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ export class SignatureTest {
349349
* Complex array parameter.
350350
*
351351
* @template T The type of the entries to pick from.
352+
*
352353
* @param array Array to pick the value from.
353354
* @param array[].weight The weight of the value.
354355
* @param array[].value The value to pick.

0 commit comments

Comments
 (0)