mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
feat(docgen): proper event parsing for typescript
This commit is contained in:
@@ -36,7 +36,7 @@ export class Documentation {
|
||||
case 'Class': {
|
||||
this.classes.set(item.name, new DocumentedClass(item, config));
|
||||
if (item.children) {
|
||||
this.parse(item.children, item.name);
|
||||
this.parse(item.children, item);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ export class Documentation {
|
||||
case 'Enumeration':
|
||||
this.typedefs.set(item.name, new DocumentedTypeDef(item, config));
|
||||
if (item.children) {
|
||||
this.parse(item.children, item.name);
|
||||
this.parse(item.children, item);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -101,7 +101,7 @@ export class Documentation {
|
||||
}
|
||||
}
|
||||
|
||||
public parse(items: ChildTypes[] | DeclarationReflection[], memberOf = '') {
|
||||
public parse(items: ChildTypes[] | DeclarationReflection[], p?: DeclarationReflection) {
|
||||
if (this.config.typescript) {
|
||||
const it = items as DeclarationReflection[];
|
||||
|
||||
@@ -114,6 +114,12 @@ export class Documentation {
|
||||
break;
|
||||
}
|
||||
case 'Method': {
|
||||
const event = p?.groups?.find((group) => group.title === 'Events');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if ((event?.children as unknown as number[])?.includes(member.id)) {
|
||||
item = new DocumentedEvent(member, this.config);
|
||||
break;
|
||||
}
|
||||
item = new DocumentedMethod(member, this.config);
|
||||
break;
|
||||
}
|
||||
@@ -121,17 +127,13 @@ export class Documentation {
|
||||
item = new DocumentedMember(member, this.config);
|
||||
break;
|
||||
}
|
||||
case 'Event': {
|
||||
item = new DocumentedEvent(member, this.config);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
console.warn(`- Unknown documentation kind "${member.kindString}" - \n${JSON.stringify(member)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
const parent = this.classes.get(memberOf) ?? this.interfaces.get(memberOf);
|
||||
const parent = this.classes.get(p!.name) ?? this.interfaces.get(p!.name);
|
||||
if (parent) {
|
||||
if (item) {
|
||||
parent.add(item);
|
||||
@@ -154,8 +156,8 @@ export class Documentation {
|
||||
path: dirname(member.sources?.[0]?.fileName ?? ''),
|
||||
};
|
||||
|
||||
if (memberOf) {
|
||||
info.push(`member of "${memberOf}"`);
|
||||
if (p!.name) {
|
||||
info.push(`member of "${p!.name}"`);
|
||||
}
|
||||
if (meta) {
|
||||
info.push(
|
||||
|
||||
@@ -2,7 +2,9 @@ import type { DeclarationReflection, SignatureReflection } from 'typedoc';
|
||||
import { DocumentedItemMeta } from './item-meta.js';
|
||||
import { DocumentedItem } from './item.js';
|
||||
import { DocumentedParam } from './param.js';
|
||||
import { DocumentedVarType } from './var-type.js';
|
||||
import type { Event } from '../interfaces/index.js';
|
||||
import { parseType } from '../util/parseType.js';
|
||||
|
||||
export class DocumentedEvent extends DocumentedItem<Event | DeclarationReflection> {
|
||||
public override serializer() {
|
||||
@@ -23,11 +25,27 @@ export class DocumentedEvent extends DocumentedItem<Event | DeclarationReflectio
|
||||
.map((t) => t.content.find((c) => c.kind === 'text')?.text.trim())
|
||||
: undefined;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
const examples = signature.comment?.blockTags?.filter((t) => t.tag === '@example').length
|
||||
? signature.comment.blockTags
|
||||
.filter((t) => t.tag === '@example')
|
||||
.map((t) => t.content.reduce((prev, curr) => (prev += curr.text), '').trim())
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
name: signature.name,
|
||||
// @ts-expect-error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
name: signature.parameters?.[0]?.type?.value,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/prefer-nullish-coalescing
|
||||
description: signature.comment?.summary?.reduce((prev, curr) => (prev += curr.text), '').trim() || undefined,
|
||||
see,
|
||||
access:
|
||||
data.flags.isPrivate ||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
signature.comment?.blockTags?.some((t) => t.tag === '@private' || t.tag === '@internal')
|
||||
? 'private'
|
||||
: undefined,
|
||||
examples,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
deprecated: signature.comment?.blockTags?.some((t) => t.tag === '@deprecated')
|
||||
? signature.comment.blockTags
|
||||
@@ -37,8 +55,34 @@ export class DocumentedEvent extends DocumentedItem<Event | DeclarationReflectio
|
||||
: undefined,
|
||||
// @ts-expect-error
|
||||
params: signature.parameters
|
||||
? (signature as SignatureReflection).parameters?.map((p) => new DocumentedParam(p, this.config).serialize())
|
||||
? (signature as SignatureReflection).parameters
|
||||
?.slice(1)
|
||||
.map((p) => new DocumentedParam(p, this.config).serialize())
|
||||
: undefined,
|
||||
returns: signature.type
|
||||
? [
|
||||
new DocumentedVarType(
|
||||
{
|
||||
names: [parseType(signature.type)],
|
||||
description:
|
||||
signature.comment?.blockTags
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
?.find((t) => t.tag === '@returns')
|
||||
?.content.reduce((prev, curr) => (prev += curr.text), '')
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
.trim() || undefined,
|
||||
},
|
||||
this.config,
|
||||
).serialize(),
|
||||
]
|
||||
: undefined,
|
||||
returnsDescription:
|
||||
signature.comment?.blockTags
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
?.find((t) => t.tag === '@returns')
|
||||
?.content.reduce((prev, curr) => (prev += curr.text), '')
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
.trim() || undefined,
|
||||
meta,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user