Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(website): show union members of type aliases #10001

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/website/src/components/UnionMemberList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Excerpt, type ApiTypeAlias, type ExcerptToken } from '@discordjs/api-extractor-model';
import { useMemo } from 'react';
import { ExcerptText } from './ExcerptText';

export type UnionMember = ExcerptToken[];

export function UnionMemberList({ item, members }: { readonly item: ApiTypeAlias; readonly members: UnionMember[] }) {
return useMemo(
() =>
members.map((member, idx) => (
<div className="flex flex-row place-items-center gap-4" key={`union-${idx}`}>
<span className="break-all font-mono space-y-2">
<ExcerptText
excerpt={new Excerpt(member, { startIndex: 0, endIndex: member.length })}
apiPackage={item.getAssociatedPackage()!}
/>
</span>
</div>
)),
[item, members],
);
}
Qjuh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ApiTypeAlias } from '@discordjs/api-extractor-model';
import { VscSymbolArray } from '@react-icons/all-files/vsc/VscSymbolArray';
import type { UnionMember } from '~/components/UnionMemberList';
import { UnionMemberList } from '~/components/UnionMemberList';
import { DocumentationSection } from './DocumentationSection';

export function UnionMembersSection({
item,
members,
}: {
readonly item: ApiTypeAlias;
readonly members: UnionMember[];
}) {
return (
<DocumentationSection icon={<VscSymbolArray size={20} />} padded title="Union Members">
<div className="flex flex-col gap-4">
<UnionMemberList item={item} members={members} />
</div>
</DocumentationSection>
);
}
53 changes: 52 additions & 1 deletion apps/website/src/components/model/TypeAlias.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
import type { ApiTypeAlias } from '@discordjs/api-extractor-model';
import { ExcerptTokenKind, type ApiTypeAlias, ExcerptToken } from '@discordjs/api-extractor-model';
import { useMemo } from 'react';
import { SyntaxHighlighter } from '../SyntaxHighlighter';
import { Documentation } from '../documentation/Documentation';
import { Header } from '../documentation/Header';
import { SummarySection } from '../documentation/section/SummarySection';
import { UnionMembersSection } from '../documentation/section/UnionMembersSection';

export function TypeAlias({ item }: { readonly item: ApiTypeAlias }) {
const union = useMemo(() => {
const union: ExcerptToken[][] = [];
let currentUnionMember: ExcerptToken[] = [];
let depth = 0;
for (const token of item.typeExcerpt.spannedTokens) {
if (token.text.includes('?')) {
return [];
}

if (token.text.includes('<')) {
depth++;
}

console.log(token.text);
if (token.text.includes('>')) {
depth--;
}

if (token.text.trim() === '|' && depth === 0) {
if (currentUnionMember.length) {
union.push(currentUnionMember);
currentUnionMember = [];
}
} else if (depth === 0 && token.kind === ExcerptTokenKind.Content && token.text.includes('|')) {
for (const [idx, tokenpart] of token.text.split('|').entries()) {
if (currentUnionMember.length && depth === 0 && idx === 0) {
currentUnionMember.push(new ExcerptToken(ExcerptTokenKind.Content, tokenpart));
union.push(currentUnionMember);
currentUnionMember = [];
} else if (currentUnionMember.length && depth === 0) {
union.push(currentUnionMember);
currentUnionMember = [new ExcerptToken(ExcerptTokenKind.Content, tokenpart)];
} else if (tokenpart.length) {
currentUnionMember.push(new ExcerptToken(ExcerptTokenKind.Content, tokenpart));
}
}
} else {
currentUnionMember.push(token);
}
}

if (currentUnionMember.length && union.length) {
union.push(currentUnionMember);
}

return union;
}, [item]);

return (
<Documentation>
<Header
Expand All @@ -16,6 +66,7 @@ export function TypeAlias({ item }: { readonly item: ApiTypeAlias }) {
{/* @ts-expect-error async component */}
<SyntaxHighlighter code={item.excerpt.text} />
<SummarySection item={item} />
{union.length ? <UnionMembersSection item={item} members={union} /> : null}
</Documentation>
);
}
85 changes: 38 additions & 47 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ export interface IconData {
proxyIconURL?: string;
}

export type EmbedAuthorData = Omit<APIEmbedAuthor, 'icon_url' | 'proxy_icon_url'> & IconData;
export interface EmbedAuthorData extends Omit<APIEmbedAuthor, 'icon_url' | 'proxy_icon_url'>, IconData {}

export type EmbedFooterData = Omit<APIEmbedFooter, 'icon_url' | 'proxy_icon_url'> & IconData;
export interface EmbedFooterData extends Omit<APIEmbedFooter, 'icon_url' | 'proxy_icon_url'>, IconData {}

export interface EmbedAssetData extends Omit<APIEmbedImage, 'proxy_url'> {
proxyURL?: string;
Expand Down Expand Up @@ -1988,29 +1988,29 @@ export class LimitedCollection<Key, Value> extends Collection<Key, Value> {

export type MessageComponentType = Exclude<ComponentType, ComponentType.TextInput | ComponentType.ActionRow>;

export type MessageCollectorOptionsParams<
export interface MessageCollectorOptionsParams<
ComponentType extends MessageComponentType,
Cached extends boolean = boolean,
> = {
> extends MessageComponentCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]> {
componentType?: ComponentType;
} & MessageComponentCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]>;
}

export type MessageChannelCollectorOptionsParams<
export interface MessageChannelCollectorOptionsParams<
ComponentType extends MessageComponentType,
Cached extends boolean = boolean,
> = {
> extends MessageChannelComponentCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]> {
componentType?: ComponentType;
} & MessageChannelComponentCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]>;
}

export type AwaitMessageCollectorOptionsParams<
export interface AwaitMessageCollectorOptionsParams<
ComponentType extends MessageComponentType,
Cached extends boolean = boolean,
> = {
> extends Pick<
InteractionCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]>,
keyof AwaitMessageComponentOptions<any>
> {
componentType?: ComponentType;
} & Pick<
InteractionCollectorOptions<MappedInteractionTypes<Cached>[ComponentType]>,
keyof AwaitMessageComponentOptions<any>
>;
}

export interface StringMappedInteractionTypes<Cached extends CacheType = CacheType> {
Button: ButtonInteraction<Cached>;
Expand Down Expand Up @@ -2436,7 +2436,9 @@ export interface GuildForumTag {
emoji: GuildForumTagEmoji | null;
}

export type GuildForumTagData = Partial<GuildForumTag> & { name: string };
export interface GuildForumTagData extends Partial<GuildForumTag> {
name: string;
}

export interface DefaultReactionEmoji {
id: Snowflake | null;
Expand Down Expand Up @@ -3973,7 +3975,7 @@ export class ChannelManager extends CachedManager<Snowflake, Channel, ChannelRes
public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<Channel | null>;
}

export type FetchGuildApplicationCommandFetchOptions = Omit<FetchApplicationCommandOptions, 'guildId'>;
export interface FetchGuildApplicationCommandFetchOptions extends Omit<FetchApplicationCommandOptions, 'guildId'> {}

export class GuildApplicationCommandManager extends ApplicationCommandManager<ApplicationCommand, {}, Guild> {
private constructor(guild: Guild, iterable?: Iterable<RawApplicationCommandData>);
Expand Down Expand Up @@ -4420,7 +4422,7 @@ export interface WebhookFields extends PartialWebhookFields {

//#region Typedefs

export type ActivitiesOptions = Omit<ActivityOptions, 'shardId'>;
export interface ActivitiesOptions extends Omit<ActivityOptions, 'shardId'> {}

export interface ActivityOptions {
name: string;
Expand Down Expand Up @@ -4775,22 +4777,16 @@ export interface AutoModerationTriggerMetadata {
mentionRaidProtectionEnabled: boolean;
}

export type AwaitMessageComponentOptions<Interaction extends CollectedMessageInteraction> = Omit<
MessageComponentCollectorOptions<Interaction>,
'max' | 'maxComponents' | 'maxUsers'
>;
export interface AwaitMessageComponentOptions<Interaction extends CollectedMessageInteraction>
extends Omit<MessageComponentCollectorOptions<Interaction>, 'max' | 'maxComponents' | 'maxUsers'> {}

export type ModalSubmitInteractionCollectorOptions<Interaction extends ModalSubmitInteraction> = Omit<
InteractionCollectorOptions<Interaction>,
'channel' | 'message' | 'guild' | 'interactionType'
>;
export interface ModalSubmitInteractionCollectorOptions<Interaction extends ModalSubmitInteraction>
extends Omit<InteractionCollectorOptions<Interaction>, 'channel' | 'message' | 'guild' | 'interactionType'> {}

export type AwaitModalSubmitOptions<Interaction extends ModalSubmitInteraction> = Omit<
ModalSubmitInteractionCollectorOptions<Interaction>,
'max' | 'maxComponents' | 'maxUsers'
> & {
export interface AwaitModalSubmitOptions<Interaction extends ModalSubmitInteraction>
extends Omit<ModalSubmitInteractionCollectorOptions<Interaction>, 'max' | 'maxComponents' | 'maxUsers'> {
time: number;
};
}

export interface AwaitMessagesOptions extends MessageCollectorOptions {
errors?: string[];
Expand Down Expand Up @@ -5113,14 +5109,14 @@ export interface CommandInteractionResolvedData<Cached extends CacheType = Cache
attachments?: Collection<Snowflake, Attachment>;
}

export type AutocompleteFocusedOption = Pick<CommandInteractionOption, 'name'> & {
export interface AutocompleteFocusedOption extends Pick<CommandInteractionOption, 'name'> {
focused: true;
type:
| ApplicationCommandOptionType.String
| ApplicationCommandOptionType.Integer
| ApplicationCommandOptionType.Number;
value: string;
};
}

export declare const Colors: {
Default: 0x000000;
Expand Down Expand Up @@ -5912,7 +5908,7 @@ export interface InteractionDeferReplyOptions {
fetchReply?: boolean;
}

export type InteractionDeferUpdateOptions = Omit<InteractionDeferReplyOptions, 'ephemeral'>;
export interface InteractionDeferUpdateOptions extends Omit<InteractionDeferReplyOptions, 'ephemeral'> {}

export interface InteractionReplyOptions extends BaseMessageOptions {
tts?: boolean;
Expand Down Expand Up @@ -6024,15 +6020,11 @@ export type CollectedMessageInteraction<Cached extends CacheType = CacheType> =
ModalSubmitInteraction
>;

export type MessageComponentCollectorOptions<Interaction extends CollectedMessageInteraction> = Omit<
InteractionCollectorOptions<Interaction>,
'channel' | 'message' | 'guild' | 'interactionType'
>;
export interface MessageComponentCollectorOptions<Interaction extends CollectedMessageInteraction>
extends Omit<InteractionCollectorOptions<Interaction>, 'channel' | 'message' | 'guild' | 'interactionType'> {}

export type MessageChannelComponentCollectorOptions<Interaction extends CollectedMessageInteraction> = Omit<
InteractionCollectorOptions<Interaction>,
'channel' | 'guild' | 'interactionType'
>;
export interface MessageChannelComponentCollectorOptions<Interaction extends CollectedMessageInteraction>
extends Omit<InteractionCollectorOptions<Interaction>, 'channel' | 'guild' | 'interactionType'> {}

export interface MessageEvent {
data: WebSocketData;
Expand Down Expand Up @@ -6093,8 +6085,9 @@ export interface MessageCreateOptions extends BaseMessageOptions {
>;
}

export type GuildForumThreadMessageCreateOptions = BaseMessageOptions &
Pick<MessageCreateOptions, 'flags' | 'stickers'>;
export interface GuildForumThreadMessageCreateOptions
extends BaseMessageOptions,
Pick<MessageCreateOptions, 'flags' | 'stickers'> {}

export interface MessageEditAttachmentData {
id: Snowflake;
Expand Down Expand Up @@ -6214,9 +6207,7 @@ export type PermissionResolvable = BitFieldResolvable<keyof typeof PermissionFla

export type PermissionOverwriteResolvable = UserResolvable | RoleResolvable | PermissionOverwrites;

export type RecursiveArray<ItemType> = ReadonlyArray<ItemType | RecursiveArray<ItemType>>;

export type RecursiveReadonlyArray<ItemType> = ReadonlyArray<ItemType | RecursiveReadonlyArray<ItemType>>;
export interface RecursiveReadonlyArray<ItemType> extends ReadonlyArray<ItemType | RecursiveReadonlyArray<ItemType>> {}

export interface PartialRecipient {
username: string;
Expand Down Expand Up @@ -6566,7 +6557,7 @@ export interface WebhookClientDataURL {
url: string;
}

export type WebhookClientOptions = Pick<ClientOptions, 'allowedMentions' | 'rest'>;
export interface WebhookClientOptions extends Pick<ClientOptions, 'allowedMentions' | 'rest'> {}

export interface WebhookDeleteOptions {
token?: string;
Expand Down