Skip to content

Commit

Permalink
chore: use descriptive type parameter names (#9937)
Browse files Browse the repository at this point in the history
* chore: use descriptive type parameter names

* refactor: requested changes

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
almeidx and kodiakhq[bot] authored Nov 12, 2023
1 parent 4ff3ea4 commit 975d5f1
Show file tree
Hide file tree
Showing 34 changed files with 1,097 additions and 888 deletions.
4 changes: 2 additions & 2 deletions apps/website/src/components/ItemLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { usePathname } from 'next/navigation';
import type { PropsWithChildren } from 'react';
import { useCurrentPathMeta } from '~/hooks/useCurrentPathMeta';

export interface ItemLinkProps<T extends string> extends Omit<LinkProps<T>, 'href'> {
export interface ItemLinkProps<Route extends string> extends Omit<LinkProps<Route>, 'href'> {
readonly className?: string;
/**
* The URI of the api item to link to. (e.g. `/RestManager`)
Expand All @@ -29,7 +29,7 @@ export interface ItemLinkProps<T extends string> extends Omit<LinkProps<T>, 'hre
* This component only needs the relative path to the item, and will automatically
* generate the full path to the item client-side.
*/
export function ItemLink<T extends string>(props: PropsWithChildren<ItemLinkProps<T>>) {
export function ItemLink<Route extends string>(props: PropsWithChildren<ItemLinkProps<Route>>) {
const pathname = usePathname();
const { packageName, version } = useCurrentPathMeta();

Expand Down
11 changes: 7 additions & 4 deletions apps/website/src/util/members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import type { ApiItem, ApiItemContainerMixin } from '@discordjs/api-extractor-mo
* @param parent - The parent to resolve the inherited members of.
* @param predicate - A predicate to filter the members by.
*/
export function resolveMembers<T extends ApiItem>(
export function resolveMembers<WantedItem extends ApiItem>(
parent: ApiItemContainerMixin,
predicate: (item: ApiItem) => item is T,
predicate: (item: ApiItem) => item is WantedItem,
) {
const seenItems = new Set<string>();
const inheritedMembers = parent.findMembersWithInheritance().items.reduce((acc, item) => {
Expand All @@ -25,14 +25,17 @@ export function resolveMembers<T extends ApiItem>(
}

return acc;
}, new Array<{ inherited?: ApiItemContainerMixin | undefined; item: T }>());
}, new Array<{ inherited?: ApiItemContainerMixin | undefined; item: WantedItem }>());

const mergedMembers = parent
.getMergedSiblings()
.filter((sibling) => sibling.containerKey !== parent.containerKey)
.flatMap((sibling) => (sibling as ApiItemContainerMixin).findMembersWithInheritance().items)
.filter((item) => predicate(item) && !seenItems.has(item.containerKey))
.map((item) => ({ item: item as T, inherited: item.parent ? (item.parent as ApiItemContainerMixin) : undefined }));
.map((item) => ({
item: item as WantedItem,
inherited: item.parent ? (item.parent as ApiItemContainerMixin) : undefined,
}));

return [...inheritedMembers, ...mergedMembers];
}
15 changes: 15 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ const typeScriptRuleset = merge(...typescript, {
},
rules: {
'@typescript-eslint/consistent-type-definitions': [2, 'interface'],
'@typescript-eslint/naming-convention': [
2,
{
selector: 'typeParameter',
format: ['PascalCase'],
custom: {
regex: '^\\w{3,}',
match: true,
},
},
],
},
settings: {
'import/resolver': {
Expand Down Expand Up @@ -110,6 +121,10 @@ export default [
'@typescript-eslint/no-this-alias': 0,
},
},
{
files: [`packages/{api-extractor,api-extractor-model,api-extractor-utils}/**/*${commonFiles}`],
rules: { '@typescript-eslint/naming-convention': 0 },
},
reactRuleset,
nextRuleset,
edgeRuleset,
Expand Down
8 changes: 6 additions & 2 deletions packages/brokers/src/brokers/Broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface IPubSubBroker<TEvents extends Record<string, any>>
/**
* Publishes an event
*/
publish<T extends keyof TEvents>(event: T, data: TEvents[T]): Promise<void>;
publish<Event extends keyof TEvents>(event: Event, data: TEvents[Event]): Promise<void>;
}

export interface IRPCBroker<TEvents extends Record<string, any>, TResponses extends Record<keyof TEvents, any>>
Expand All @@ -84,5 +84,9 @@ export interface IRPCBroker<TEvents extends Record<string, any>, TResponses exte
/**
* Makes an RPC call
*/
call<T extends keyof TEvents>(event: T, data: TEvents[T], timeoutDuration?: number): Promise<TResponses[T]>;
call<Event extends keyof TEvents>(
event: Event,
data: TEvents[Event],
timeoutDuration?: number,
): Promise<TResponses[Event]>;
}
2 changes: 1 addition & 1 deletion packages/brokers/src/brokers/redis/PubSubRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PubSubRedisBroker<TEvents extends Record<string, any>>
/**
* {@inheritDoc IPubSubBroker.publish}
*/
public async publish<T extends keyof TEvents>(event: T, data: TEvents[T]): Promise<void> {
public async publish<Event extends keyof TEvents>(event: Event, data: TEvents[Event]): Promise<void> {
await this.options.redisClient.xadd(
event as string,
'*',
Expand Down
10 changes: 5 additions & 5 deletions packages/brokers/src/brokers/redis/RPCRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export class RPCRedisBroker<TEvents extends Record<string, any>, TResponses exte
/**
* {@inheritDoc IRPCBroker.call}
*/
public async call<T extends keyof TEvents>(
event: T,
data: TEvents[T],
public async call<Event extends keyof TEvents>(
event: Event,
data: TEvents[Event],
timeoutDuration: number = this.options.timeout,
): Promise<TResponses[T]> {
): Promise<TResponses[Event]> {
const id = await this.options.redisClient.xadd(
event as string,
'*',
Expand All @@ -103,7 +103,7 @@ export class RPCRedisBroker<TEvents extends Record<string, any>, TResponses exte
const timedOut = new Error(`timed out after ${timeoutDuration}ms`);

await this.streamReadClient.subscribe(rpcChannel);
return new Promise<TResponses[T]>((resolve, reject) => {
return new Promise<TResponses[Event]>((resolve, reject) => {
const timeout = setTimeout(() => reject(timedOut), timeoutDuration).unref();

this.promises.set(id!, { resolve, reject, timeout });
Expand Down
16 changes: 8 additions & 8 deletions packages/builders/src/components/ActionRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export type AnyComponentBuilder = MessageActionRowComponentBuilder | ModalAction
/**
* A builder that creates API-compatible JSON data for action rows.
*
* @typeParam T - The types of components this action row holds
* @typeParam ComponentType - The types of components this action row holds
*/
export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBuilder<
export class ActionRowBuilder<ComponentType extends AnyComponentBuilder> extends ComponentBuilder<
APIActionRowComponent<APIMessageActionRowComponent | APIModalActionRowComponent>
> {
/**
* The components within this action row.
*/
public readonly components: T[];
public readonly components: ComponentType[];

/**
* Creates a new action row from API data.
Expand Down Expand Up @@ -100,15 +100,15 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
*/
public constructor({ components, ...data }: Partial<APIActionRowComponent<APIActionRowComponentTypes>> = {}) {
super({ type: ComponentType.ActionRow, ...data });
this.components = (components?.map((component) => createComponentBuilder(component)) ?? []) as T[];
this.components = (components?.map((component) => createComponentBuilder(component)) ?? []) as ComponentType[];
}

/**
* Adds components to this action row.
*
* @param components - The components to add
*/
public addComponents(...components: RestOrArray<T>) {
public addComponents(...components: RestOrArray<ComponentType>) {
this.components.push(...normalizeArray(components));
return this;
}
Expand All @@ -118,18 +118,18 @@ export class ActionRowBuilder<T extends AnyComponentBuilder> extends ComponentBu
*
* @param components - The components to set
*/
public setComponents(...components: RestOrArray<T>) {
public setComponents(...components: RestOrArray<ComponentType>) {
this.components.splice(0, this.components.length, ...normalizeArray(components));
return this;
}

/**
* {@inheritDoc ComponentBuilder.toJSON}
*/
public toJSON(): APIActionRowComponent<ReturnType<T['toJSON']>> {
public toJSON(): APIActionRowComponent<ReturnType<ComponentType['toJSON']>> {
return {
...this.data,
components: this.components.map((component) => component.toJSON()),
} as APIActionRowComponent<ReturnType<T['toJSON']>>;
} as APIActionRowComponent<ReturnType<ComponentType['toJSON']>>;
}
}
14 changes: 8 additions & 6 deletions packages/builders/src/components/Components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ export interface MappedComponentTypes {
/**
* Factory for creating components from API data.
*
* @typeParam T - The type of component to use
* @typeParam ComponentType - The type of component to use
* @param data - The API data to transform to a component class
*/
export function createComponentBuilder<T extends keyof MappedComponentTypes>(
export function createComponentBuilder<ComponentType extends keyof MappedComponentTypes>(
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
data: (APIModalComponent | APIMessageComponent) & { type: T },
): MappedComponentTypes[T];
data: (APIModalComponent | APIMessageComponent) & { type: ComponentType },
): MappedComponentTypes[ComponentType];

/**
* Factory for creating components from API data.
*
* @typeParam C - The type of component to use
* @typeParam ComponentBuilder - The type of component to use
* @param data - The API data to transform to a component class
*/
export function createComponentBuilder<C extends MessageComponentBuilder | ModalComponentBuilder>(data: C): C;
export function createComponentBuilder<ComponentBuilder extends MessageComponentBuilder | ModalComponentBuilder>(
data: ComponentBuilder,
): ComponentBuilder;

export function createComponentBuilder(
data: APIMessageComponent | APIModalComponent | MessageComponentBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export function validateChoicesLength(amountAdding: number, choices?: APIApplica
}

export function assertReturnOfBuilder<
T extends ApplicationCommandOptionBase | SlashCommandSubcommandBuilder | SlashCommandSubcommandGroupBuilder,
>(input: unknown, ExpectedInstanceOf: new () => T): asserts input is T {
ReturnType extends ApplicationCommandOptionBase | SlashCommandSubcommandBuilder | SlashCommandSubcommandGroupBuilder,
>(input: unknown, ExpectedInstanceOf: new () => ReturnType): asserts input is ReturnType {
s.instance(ExpectedInstanceOf).parse(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const booleanPredicate = s.boolean;
/**
* This mixin holds choices and autocomplete symbols used for options.
*/
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends number | string> {
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<ChoiceType extends number | string> {
/**
* The choices of this option.
*/
public readonly choices?: APIApplicationCommandOptionChoice<T>[];
public readonly choices?: APIApplicationCommandOptionChoice<ChoiceType>[];

/**
* Whether this option utilizes autocomplete.
Expand All @@ -37,7 +37,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends n
*
* @param choices - The choices to add
*/
public addChoices(...choices: APIApplicationCommandOptionChoice<T>[]): this {
public addChoices(...choices: APIApplicationCommandOptionChoice<ChoiceType>[]): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends n
*
* @param choices - The choices to set
*/
public setChoices<Input extends APIApplicationCommandOptionChoice<T>[]>(...choices: Input): this {
public setChoices<Input extends APIApplicationCommandOptionChoice<ChoiceType>[]>(...choices: Input): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ export class SharedSlashCommandOptions<ShouldOmitSubcommandFunctions = true> {
* @param Instance - The instance of whatever is being added
* @internal
*/
private _sharedAddOptionMethod<T extends ApplicationCommandOptionBase>(
private _sharedAddOptionMethod<OptionBuilder extends ApplicationCommandOptionBase>(
input:
| Omit<T, 'addChoices'>
| Omit<T, 'setAutocomplete'>
| T
| ((builder: T) => Omit<T, 'addChoices'> | Omit<T, 'setAutocomplete'> | T),
Instance: new () => T,
| Omit<OptionBuilder, 'addChoices'>
| Omit<OptionBuilder, 'setAutocomplete'>
| OptionBuilder
| ((
builder: OptionBuilder,
) => Omit<OptionBuilder, 'addChoices'> | Omit<OptionBuilder, 'setAutocomplete'> | OptionBuilder),
Instance: new () => OptionBuilder,
): ShouldOmitSubcommandFunctions extends true ? Omit<this, 'addSubcommand' | 'addSubcommandGroup'> : this {
const { options } = this;

Expand Down
8 changes: 4 additions & 4 deletions packages/builders/src/util/normalizeArray.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Normalizes data that is a rest parameter or an array into an array with a depth of 1.
*
* @typeParam T - The data that must satisfy {@link RestOrArray}.
* @typeParam ItemType - The data that must satisfy {@link RestOrArray}.
* @param arr - The (possibly variadic) data to normalize
*/
export function normalizeArray<T>(arr: RestOrArray<T>): T[] {
export function normalizeArray<ItemType>(arr: RestOrArray<ItemType>): ItemType[] {
if (Array.isArray(arr[0])) return arr[0];
return arr as T[];
return arr as ItemType[];
}

/**
Expand All @@ -16,4 +16,4 @@ export function normalizeArray<T>(arr: RestOrArray<T>): T[] {
* This type is used throughout builders to ensure both an array and variadic arguments
* may be used. It is normalized with {@link normalizeArray}.
*/
export type RestOrArray<T> = T[] | [T[]];
export type RestOrArray<Type> = Type[] | [Type[]];
6 changes: 3 additions & 3 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import { describe, test, expect } from 'vitest';
import { Collection } from '../src/index.js';

type TestCollection<V> = Collection<string, V>;
type TestCollection<Value> = Collection<string, Value>;

function createCollection<V = number>(): TestCollection<V> {
function createCollection<Value = number>(): TestCollection<Value> {
return new Collection();
}

function createCollectionFrom<V = number>(...entries: [key: string, value: V][]): TestCollection<V> {
function createCollectionFrom<Value = number>(...entries: [key: string, value: Value][]): TestCollection<Value> {
return new Collection(entries);
}

Expand Down
Loading

0 comments on commit 975d5f1

Please sign in to comment.