mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
feat(website): Show constructor information (#8540)
This commit is contained in:
381
packages/website/src/DocModel/ApiNodeJSONEncoder.ts
Normal file
381
packages/website/src/DocModel/ApiNodeJSONEncoder.ts
Normal file
@@ -0,0 +1,381 @@
|
||||
import {
|
||||
ApiModel,
|
||||
ApiDeclaredItem,
|
||||
ApiPropertyItem,
|
||||
ApiMethod,
|
||||
ApiParameterListMixin,
|
||||
ApiTypeParameterListMixin,
|
||||
ApiClass,
|
||||
ApiFunction,
|
||||
ApiItemKind,
|
||||
ApiTypeAlias,
|
||||
ApiEnum,
|
||||
ApiInterface,
|
||||
ApiMethodSignature,
|
||||
ApiPropertySignature,
|
||||
ApiVariable,
|
||||
ApiItem,
|
||||
ApiConstructor,
|
||||
ApiItemContainerMixin,
|
||||
} from '@microsoft/api-extractor-model';
|
||||
import { generateTypeParamData } from './TypeParameterMixin';
|
||||
import { Visibility } from './Visibility';
|
||||
import { createCommentNode } from './comment';
|
||||
import type { DocBlockJSON } from './comment/CommentBlock';
|
||||
import type { AnyDocNodeJSON } from './comment/CommentNode';
|
||||
import { DocNodeContainerJSON, nodeContainer } from './comment/CommentNodeContainer';
|
||||
import {
|
||||
generatePath,
|
||||
genParameter,
|
||||
genReference,
|
||||
genToken,
|
||||
resolveName,
|
||||
TokenDocumentation,
|
||||
} from '~/util/parse.server';
|
||||
|
||||
export interface ReferenceData {
|
||||
name: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export interface InheritanceData {
|
||||
parentName: string;
|
||||
path: string;
|
||||
parentKey: string;
|
||||
}
|
||||
|
||||
export interface ApiInheritableJSON {
|
||||
inheritanceData: InheritanceData | null;
|
||||
}
|
||||
|
||||
export interface ApiItemJSON {
|
||||
kind: string;
|
||||
name: string;
|
||||
referenceData: ReferenceData;
|
||||
excerpt: string;
|
||||
excerptTokens: TokenDocumentation[];
|
||||
remarks: DocNodeContainerJSON | null;
|
||||
summary: DocNodeContainerJSON | null;
|
||||
deprecated: DocNodeContainerJSON | null;
|
||||
comment: AnyDocNodeJSON | null;
|
||||
containerKey: string;
|
||||
path: string[];
|
||||
}
|
||||
|
||||
export interface ApiPropertyItemJSON extends ApiItemJSON, ApiInheritableJSON {
|
||||
propertyTypeTokens: TokenDocumentation[];
|
||||
readonly: boolean;
|
||||
optional: boolean;
|
||||
}
|
||||
|
||||
export interface ApiTypeParameterListJSON {
|
||||
typeParameters: ApiTypeParameterJSON[];
|
||||
}
|
||||
|
||||
export interface ApiTypeParameterJSON {
|
||||
name: string;
|
||||
constraintTokens: TokenDocumentation[];
|
||||
defaultTokens: TokenDocumentation[];
|
||||
optional: boolean;
|
||||
commentBlock: DocBlockJSON | null;
|
||||
}
|
||||
|
||||
export interface ApiParameterListJSON {
|
||||
parameters: ApiParameterJSON[];
|
||||
}
|
||||
|
||||
export interface ApiMethodSignatureJSON
|
||||
extends ApiItemJSON,
|
||||
ApiTypeParameterListJSON,
|
||||
ApiParameterListJSON,
|
||||
ApiInheritableJSON {
|
||||
returnTypeTokens: TokenDocumentation[];
|
||||
optional: boolean;
|
||||
overloadIndex: number;
|
||||
}
|
||||
|
||||
export interface ApiMethodJSON extends ApiMethodSignatureJSON {
|
||||
static: boolean;
|
||||
visibility: Visibility;
|
||||
}
|
||||
|
||||
export interface ApiParameterJSON {
|
||||
name: string;
|
||||
isOptional: boolean;
|
||||
tokens: TokenDocumentation[];
|
||||
paramCommentBlock: DocBlockJSON | null;
|
||||
}
|
||||
|
||||
export interface ApiClassJSON extends ApiItemJSON, ApiTypeParameterListJSON {
|
||||
constructor: ApiConstructorJSON | null;
|
||||
properties: ApiPropertyItemJSON[];
|
||||
methods: ApiMethodJSON[];
|
||||
extendsTokens: TokenDocumentation[];
|
||||
implementsTokens: TokenDocumentation[][];
|
||||
}
|
||||
|
||||
export interface ApiTypeAliasJSON extends ApiItemJSON, ApiTypeParameterListJSON {
|
||||
typeTokens: TokenDocumentation[];
|
||||
}
|
||||
|
||||
export interface EnumMemberData {
|
||||
name: string;
|
||||
initializerTokens: TokenDocumentation[];
|
||||
summary: DocNodeContainerJSON | null;
|
||||
}
|
||||
|
||||
export interface ApiEnumJSON extends ApiItemJSON {
|
||||
members: EnumMemberData[];
|
||||
}
|
||||
|
||||
export interface ApiInterfaceJSON extends ApiItemJSON, ApiTypeParameterListJSON {
|
||||
properties: ApiPropertyItemJSON[];
|
||||
methods: ApiMethodSignatureJSON[];
|
||||
extendsTokens: TokenDocumentation[][] | null;
|
||||
}
|
||||
|
||||
export interface ApiVariableJSON extends ApiItemJSON {
|
||||
typeTokens: TokenDocumentation[];
|
||||
readonly: boolean;
|
||||
}
|
||||
|
||||
export interface ApiFunctionJSON extends ApiItemJSON, ApiTypeParameterListJSON, ApiParameterListJSON {
|
||||
returnTypeTokens: TokenDocumentation[];
|
||||
overloadIndex: number;
|
||||
}
|
||||
|
||||
export interface ApiConstructorJSON extends ApiItemJSON, ApiParameterListJSON {
|
||||
protected: boolean;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
||||
export class ApiNodeJSONEncoder {
|
||||
public static encode(model: ApiModel, node: ApiItem) {
|
||||
if (!(node instanceof ApiDeclaredItem)) {
|
||||
throw new Error(`Cannot serialize node of type ${node.kind}`);
|
||||
}
|
||||
|
||||
switch (node.kind) {
|
||||
case ApiItemKind.Class:
|
||||
return this.encodeClass(model, node as ApiClass);
|
||||
case ApiItemKind.Function:
|
||||
return this.encodeFunction(model, node as ApiFunction);
|
||||
case ApiItemKind.Interface:
|
||||
return this.encodeInterface(model, node as ApiInterface);
|
||||
case ApiItemKind.TypeAlias:
|
||||
return this.encodeTypeAlias(model, node as ApiTypeAlias);
|
||||
case ApiItemKind.Enum:
|
||||
return this.encodeEnum(model, node as ApiEnum);
|
||||
case ApiItemKind.Variable:
|
||||
return this.encodeVariable(model, node as ApiVariable);
|
||||
default:
|
||||
throw new Error(`Unknown API item kind: ${node.kind}`);
|
||||
}
|
||||
}
|
||||
|
||||
public static encodeItem(model: ApiModel, item: ApiDeclaredItem): ApiItemJSON {
|
||||
const path = [];
|
||||
for (const _item of item.getHierarchy()) {
|
||||
switch (_item.kind) {
|
||||
case 'None':
|
||||
case 'EntryPoint':
|
||||
case 'Model':
|
||||
break;
|
||||
default:
|
||||
path.push(resolveName(_item));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
kind: item.kind,
|
||||
name: resolveName(item),
|
||||
referenceData: genReference(item),
|
||||
excerpt: item.excerpt.text,
|
||||
excerptTokens: item.excerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
remarks: item.tsdocComment?.remarksBlock
|
||||
? (createCommentNode(item.tsdocComment.remarksBlock, model, item.parent) as DocNodeContainerJSON)
|
||||
: null,
|
||||
summary: item.tsdocComment?.summarySection
|
||||
? (createCommentNode(item.tsdocComment.summarySection, model, item.parent) as DocNodeContainerJSON)
|
||||
: null,
|
||||
deprecated: item.tsdocComment?.deprecatedBlock
|
||||
? (createCommentNode(item.tsdocComment.deprecatedBlock, model, item.parent) as DocNodeContainerJSON)
|
||||
: null,
|
||||
path,
|
||||
containerKey: item.containerKey,
|
||||
comment: item.tsdocComment ? createCommentNode(item.tsdocComment, model, item.parent) : null,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeParameterList(
|
||||
model: ApiModel,
|
||||
item: ApiParameterListMixin & ApiDeclaredItem,
|
||||
): { parameters: ApiParameterJSON[] } {
|
||||
return {
|
||||
parameters: item.parameters.map((param) => genParameter(model, param)),
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeTypeParameterList(model: ApiModel, item: ApiTypeParameterListMixin & ApiDeclaredItem) {
|
||||
return {
|
||||
typeParameters: item.typeParameters.map((param) => generateTypeParamData(model, param, item.parent)),
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeProperty(
|
||||
model: ApiModel,
|
||||
item: ApiPropertyItem,
|
||||
parent: ApiItemContainerMixin,
|
||||
): ApiPropertyItemJSON {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeInheritanceData(item, parent),
|
||||
propertyTypeTokens: item.propertyTypeExcerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
readonly: item.isReadonly,
|
||||
optional: item.isOptional,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeInheritanceData(item: ApiDeclaredItem, parent: ApiItemContainerMixin): ApiInheritableJSON {
|
||||
return {
|
||||
inheritanceData:
|
||||
item.parent && item.parent.containerKey !== parent.containerKey
|
||||
? {
|
||||
parentKey: item.parent.containerKey,
|
||||
parentName: item.parent.displayName,
|
||||
path: generatePath(item.parent.getHierarchy()),
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeFunction(model: ApiModel, item: ApiFunction) {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeParameterList(model, item),
|
||||
...this.encodeTypeParameterList(model, item),
|
||||
returnTypeTokens: item.returnTypeExcerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
overloadIndex: item.overloadIndex,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeMethodSignature(
|
||||
model: ApiModel,
|
||||
item: ApiMethodSignature,
|
||||
parent: ApiItemContainerMixin,
|
||||
): ApiMethodSignatureJSON {
|
||||
return {
|
||||
...this.encodeFunction(model, item),
|
||||
...this.encodeInheritanceData(item, parent),
|
||||
optional: item.isOptional,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeMethod(model: ApiModel, item: ApiMethod, parent: ApiItemContainerMixin): ApiMethodJSON {
|
||||
return {
|
||||
...this.encodeMethodSignature(model, item, parent),
|
||||
static: item.isStatic,
|
||||
visibility: item.isProtected ? Visibility.Protected : Visibility.Public,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeClass(model: ApiModel, item: ApiClass): ApiClassJSON {
|
||||
const extendsExcerpt = item.extendsType?.excerpt;
|
||||
|
||||
const methods: ApiMethodJSON[] = [];
|
||||
const properties: ApiPropertyItemJSON[] = [];
|
||||
|
||||
let constructor: ApiConstructor | undefined;
|
||||
|
||||
for (const member of item.findMembersWithInheritance().items) {
|
||||
switch (member.kind) {
|
||||
case ApiItemKind.Method:
|
||||
methods.push(this.encodeMethod(model, member as ApiMethod, item));
|
||||
break;
|
||||
case ApiItemKind.Property:
|
||||
properties.push(this.encodeProperty(model, member as ApiPropertyItem, item));
|
||||
break;
|
||||
case ApiItemKind.Constructor:
|
||||
constructor = member as ApiConstructor;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeTypeParameterList(model, item),
|
||||
constructor: constructor ? this.encodeConstructor(model, constructor) : null,
|
||||
extendsTokens: extendsExcerpt ? extendsExcerpt.spannedTokens.map((token) => genToken(model, token)) : [],
|
||||
implementsTokens: item.implementsTypes.map((excerpt) =>
|
||||
excerpt.excerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
),
|
||||
methods,
|
||||
properties,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeTypeAlias(model: ApiModel, item: ApiTypeAlias): ApiTypeAliasJSON {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeTypeParameterList(model, item),
|
||||
typeTokens: item.typeExcerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeEnum(model: ApiModel, item: ApiEnum): ApiEnumJSON {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
members: item.members.map((member) => ({
|
||||
name: member.name,
|
||||
initializerTokens: member.initializerExcerpt?.spannedTokens.map((token) => genToken(model, token)) ?? [],
|
||||
summary: member.tsdocComment ? nodeContainer(member.tsdocComment.summarySection, model, member) : null,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeInterface(model: ApiModel, item: ApiInterface): ApiInterfaceJSON {
|
||||
const methods: ApiMethodSignatureJSON[] = [];
|
||||
const properties: ApiPropertyItemJSON[] = [];
|
||||
|
||||
for (const member of item.findMembersWithInheritance().items) {
|
||||
switch (member.kind) {
|
||||
case ApiItemKind.MethodSignature:
|
||||
methods.push(this.encodeMethodSignature(model, member as ApiMethodSignature, item));
|
||||
break;
|
||||
case ApiItemKind.PropertySignature:
|
||||
properties.push(this.encodeProperty(model, member as ApiPropertySignature, item));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeTypeParameterList(model, item),
|
||||
extendsTokens: item.extendsTypes.map((excerpt) =>
|
||||
excerpt.excerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
),
|
||||
methods,
|
||||
properties,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeVariable(model: ApiModel, item: ApiVariable): ApiVariableJSON {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
typeTokens: item.variableTypeExcerpt.spannedTokens.map((token) => genToken(model, token)),
|
||||
readonly: item.isReadonly,
|
||||
};
|
||||
}
|
||||
|
||||
public static encodeConstructor(model: ApiModel, item: ApiConstructor): ApiConstructorJSON {
|
||||
return {
|
||||
...this.encodeItem(model, item),
|
||||
...this.encodeParameterList(model, item),
|
||||
protected: item.isProtected,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import {
|
||||
type ApiClass,
|
||||
type ApiModel,
|
||||
ApiItemKind,
|
||||
type ApiMethod,
|
||||
type ApiPropertyItem,
|
||||
} from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import { DocMethod } from './DocMethod';
|
||||
import { DocProperty } from './DocProperty';
|
||||
import { TypeParameterMixin } from './TypeParameterMixin';
|
||||
import { type TokenDocumentation, genToken } from '~/util/parse.server';
|
||||
|
||||
export class DocClass extends TypeParameterMixin(DocItem<ApiClass>) {
|
||||
public readonly extendsTokens: TokenDocumentation[] | null;
|
||||
public readonly implementsTokens: TokenDocumentation[][];
|
||||
public readonly methods: DocMethod[] = [];
|
||||
public readonly properties: DocProperty[] = [];
|
||||
|
||||
public constructor(model: ApiModel, item: ApiClass) {
|
||||
super(model, item);
|
||||
const extendsExcerpt = item.extendsType?.excerpt;
|
||||
|
||||
this.extendsTokens = extendsExcerpt
|
||||
? extendsExcerpt.spannedTokens.map((token) => genToken(this.model, token))
|
||||
: null;
|
||||
|
||||
this.implementsTokens = item.implementsTypes.map((excerpt) =>
|
||||
excerpt.excerpt.spannedTokens.map((token) => genToken(this.model, token)),
|
||||
);
|
||||
|
||||
for (const member of item.findMembersWithInheritance().items) {
|
||||
switch (member.kind) {
|
||||
case ApiItemKind.Method: {
|
||||
const method = member as ApiMethod;
|
||||
|
||||
if (method.parent?.containerKey !== this.containerKey) {
|
||||
this.methods.push(new DocMethod(this.model, method, true));
|
||||
break;
|
||||
}
|
||||
this.methods.push(new DocMethod(this.model, method));
|
||||
break;
|
||||
}
|
||||
case ApiItemKind.Property: {
|
||||
const property = member as ApiPropertyItem;
|
||||
|
||||
if (property.parent?.containerKey !== this.containerKey) {
|
||||
this.properties.push(new DocProperty(this.model, property, true));
|
||||
break;
|
||||
}
|
||||
|
||||
this.properties.push(new DocProperty(this.model, property));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
extendsTokens: this.extendsTokens,
|
||||
implementsTokens: this.implementsTokens,
|
||||
methods: this.methods.map((method) => method.toJSON()),
|
||||
properties: this.properties.map((prop) => prop.toJSON()),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import type { ApiEnum, ApiModel } from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import { nodeContainer } from './comment/CommentNodeContainer';
|
||||
import { genToken, TokenDocumentation } from '~/util/parse.server';
|
||||
|
||||
export interface EnumMemberData {
|
||||
name: string;
|
||||
initializerTokens: TokenDocumentation[];
|
||||
summary: ReturnType<DocItem['toJSON']>['summary'];
|
||||
}
|
||||
|
||||
export class DocEnum extends DocItem<ApiEnum> {
|
||||
public readonly members: EnumMemberData[] = [];
|
||||
|
||||
public constructor(model: ApiModel, item: ApiEnum) {
|
||||
super(model, item);
|
||||
|
||||
this.members = item.members.map((member) => ({
|
||||
name: member.name,
|
||||
initializerTokens: member.initializerExcerpt?.spannedTokens.map((token) => genToken(this.model, token)) ?? [],
|
||||
summary: member.tsdocComment ? nodeContainer(member.tsdocComment.summarySection, model, member) : null,
|
||||
}));
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
members: this.members,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import type { ApiFunction, ApiModel, ApiParameterListMixin } from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import { TypeParameterMixin } from './TypeParameterMixin';
|
||||
import { type TokenDocumentation, genToken, genParameter, ParameterDocumentation } from '~/util/parse.server';
|
||||
|
||||
export class DocFunction extends TypeParameterMixin(DocItem<ApiFunction>) {
|
||||
public readonly returnTypeTokens: TokenDocumentation[];
|
||||
public readonly overloadIndex: number;
|
||||
public readonly parameters: ParameterDocumentation[];
|
||||
|
||||
public constructor(model: ApiModel, item: ApiFunction) {
|
||||
super(model, item);
|
||||
this.returnTypeTokens = item.returnTypeExcerpt.spannedTokens.map((token) => genToken(this.model, token));
|
||||
this.overloadIndex = item.overloadIndex;
|
||||
this.parameters = (item as ApiParameterListMixin).parameters.map((param) => genParameter(this.model, param));
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
parameters: this.parameters,
|
||||
returnTypeTokens: this.returnTypeTokens,
|
||||
overloadIndex: this.overloadIndex,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
import { DocItem } from './DocItem';
|
||||
import { DocMethodSignature } from './DocMethodSignature';
|
||||
import { DocProperty } from './DocProperty';
|
||||
import { TypeParameterMixin } from './TypeParameterMixin';
|
||||
import {
|
||||
ApiInterface,
|
||||
ApiItemKind,
|
||||
ApiMethodSignature,
|
||||
ApiModel,
|
||||
ApiPropertySignature,
|
||||
} from '~/util/api-extractor.server';
|
||||
import { type TokenDocumentation, genToken } from '~/util/parse.server';
|
||||
|
||||
export class DocInterface extends TypeParameterMixin(DocItem<ApiInterface>) {
|
||||
public readonly extendsTokens: TokenDocumentation[][] | null;
|
||||
public readonly methods: DocMethodSignature[] = [];
|
||||
public readonly properties: DocProperty[] = [];
|
||||
|
||||
public constructor(model: ApiModel, item: ApiInterface) {
|
||||
super(model, item);
|
||||
|
||||
this.extendsTokens = item.extendsTypes.map((excerpt) =>
|
||||
excerpt.excerpt.spannedTokens.map((token) => genToken(this.model, token)),
|
||||
);
|
||||
|
||||
for (const member of item.findMembersWithInheritance().items) {
|
||||
switch (member.kind) {
|
||||
case ApiItemKind.MethodSignature: {
|
||||
const method = member as ApiMethodSignature;
|
||||
|
||||
if (method.parent?.containerKey !== this.containerKey) {
|
||||
this.methods.push(new DocMethodSignature(this.model, method, true));
|
||||
break;
|
||||
}
|
||||
this.methods.push(new DocMethodSignature(this.model, method));
|
||||
break;
|
||||
}
|
||||
case ApiItemKind.PropertySignature: {
|
||||
const property = member as ApiPropertySignature;
|
||||
|
||||
if (property.parent?.containerKey !== this.containerKey) {
|
||||
this.properties.push(new DocProperty(this.model, property, true));
|
||||
break;
|
||||
}
|
||||
|
||||
this.properties.push(new DocProperty(this.model, property));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
extendsTokens: this.extendsTokens,
|
||||
methods: this.methods.map((method) => method.toJSON()),
|
||||
properties: this.properties.map((prop) => prop.toJSON()),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
import type { ApiModel, ApiDeclaredItem } from '@microsoft/api-extractor-model';
|
||||
import { createCommentNode } from './comment';
|
||||
import type { AnyDocNodeJSON } from './comment/CommentNode';
|
||||
import type { DocNodeContainerJSON } from './comment/CommentNodeContainer';
|
||||
import type { ReferenceData } from '~/util/model.server';
|
||||
import { resolveName, genReference, TokenDocumentation, genToken } from '~/util/parse.server';
|
||||
|
||||
export type DocItemConstructor<T = DocItem> = new (...args: any[]) => T;
|
||||
|
||||
export class DocItem<T extends ApiDeclaredItem = ApiDeclaredItem> {
|
||||
public readonly item: T;
|
||||
public readonly name: string;
|
||||
public readonly referenceData: ReferenceData;
|
||||
public readonly model: ApiModel;
|
||||
public readonly excerpt: string;
|
||||
public readonly excerptTokens: TokenDocumentation[] = [];
|
||||
public readonly kind: string;
|
||||
public readonly remarks: DocNodeContainerJSON | null;
|
||||
public readonly summary: DocNodeContainerJSON | null;
|
||||
public readonly deprecated: DocNodeContainerJSON | null;
|
||||
public readonly containerKey: string;
|
||||
public readonly comment: AnyDocNodeJSON | null;
|
||||
|
||||
public constructor(model: ApiModel, item: T) {
|
||||
this.item = item;
|
||||
this.kind = item.kind;
|
||||
this.model = model;
|
||||
this.name = resolveName(item);
|
||||
this.referenceData = genReference(item);
|
||||
this.excerpt = item.excerpt.text;
|
||||
this.excerptTokens = item.excerpt.spannedTokens.map((token) => genToken(model, token));
|
||||
this.remarks = item.tsdocComment?.remarksBlock
|
||||
? (createCommentNode(item.tsdocComment.remarksBlock, model, item.parent) as DocNodeContainerJSON)
|
||||
: null;
|
||||
this.summary = item.tsdocComment?.summarySection
|
||||
? (createCommentNode(item.tsdocComment.summarySection, model, item.parent) as DocNodeContainerJSON)
|
||||
: null;
|
||||
this.deprecated = item.tsdocComment?.deprecatedBlock
|
||||
? (createCommentNode(item.tsdocComment.deprecatedBlock, model, item.parent) as DocNodeContainerJSON)
|
||||
: null;
|
||||
this.containerKey = item.containerKey;
|
||||
this.comment = item.tsdocComment ? createCommentNode(item.tsdocComment, model, item.parent) : null;
|
||||
}
|
||||
|
||||
public get path() {
|
||||
const path = [];
|
||||
for (const item of this.item.getHierarchy()) {
|
||||
switch (item.kind) {
|
||||
case 'None':
|
||||
case 'EntryPoint':
|
||||
case 'Model':
|
||||
break;
|
||||
default:
|
||||
path.push(resolveName(item));
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
referenceData: this.referenceData,
|
||||
summary: this.summary,
|
||||
excerpt: this.excerpt,
|
||||
excerptTokens: this.excerptTokens,
|
||||
kind: this.kind,
|
||||
remarks: this.remarks,
|
||||
deprecated: this.deprecated,
|
||||
path: this.path,
|
||||
containerKey: this.containerKey,
|
||||
comment: this.comment,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import type { ApiMethod, ApiModel } from '@microsoft/api-extractor-model';
|
||||
import { DocFunction } from './DocFunction';
|
||||
import { Visibility } from './Visibility';
|
||||
import { generatePath } from '~/util/parse.server';
|
||||
|
||||
export interface InheritanceData {
|
||||
parentName: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export class DocMethod extends DocFunction {
|
||||
public readonly static: boolean;
|
||||
public readonly optional: boolean;
|
||||
public readonly visibility: Visibility;
|
||||
public readonly inheritanceData: InheritanceData | null;
|
||||
|
||||
public constructor(model: ApiModel, item: ApiMethod, inherited = false) {
|
||||
super(model, item);
|
||||
this.static = item.isStatic;
|
||||
this.optional = item.isOptional;
|
||||
this.visibility = item.isProtected ? Visibility.Protected : Visibility.Public;
|
||||
this.inheritanceData =
|
||||
inherited && item.parent
|
||||
? {
|
||||
parentName: item.parent.displayName,
|
||||
path: generatePath(item.parent.getHierarchy()),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
static: this.static,
|
||||
optional: this.optional,
|
||||
visibility: this.visibility,
|
||||
inheritanceData: this.inheritanceData,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import type { ApiMethodSignature, ApiModel } from '@microsoft/api-extractor-model';
|
||||
import { DocFunction } from './DocFunction';
|
||||
import type { InheritanceData } from './DocMethod';
|
||||
import { generatePath } from '~/util/parse.server';
|
||||
|
||||
export class DocMethodSignature extends DocFunction {
|
||||
public readonly optional: boolean;
|
||||
public readonly inheritanceData: InheritanceData | null;
|
||||
|
||||
public constructor(model: ApiModel, item: ApiMethodSignature, inherited = false) {
|
||||
super(model, item);
|
||||
this.optional = item.isOptional;
|
||||
this.inheritanceData =
|
||||
inherited && item.parent
|
||||
? {
|
||||
parentName: item.parent.displayName,
|
||||
path: generatePath(item.parent.getHierarchy()),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
optional: this.optional,
|
||||
inheritanceData: this.inheritanceData,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import type { ApiPropertyItem, ApiModel, ApiPropertySignature } from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import type { InheritanceData } from './DocMethod';
|
||||
import { type TokenDocumentation, genToken, generatePath } from '~/util/parse.server';
|
||||
|
||||
export class DocProperty extends DocItem<ApiPropertyItem> {
|
||||
public readonly propertyTypeTokens: TokenDocumentation[];
|
||||
public readonly readonly: boolean;
|
||||
public readonly optional: boolean;
|
||||
public readonly inheritanceData: InheritanceData | null;
|
||||
|
||||
public constructor(model: ApiModel, item: ApiPropertyItem | ApiPropertySignature, inherited = false) {
|
||||
super(model, item);
|
||||
this.propertyTypeTokens = item.propertyTypeExcerpt.spannedTokens.map((token) => genToken(this.model, token));
|
||||
this.readonly = item.isReadonly;
|
||||
this.optional = item.isOptional;
|
||||
this.inheritanceData =
|
||||
inherited && item.parent
|
||||
? {
|
||||
parentName: item.parent.displayName,
|
||||
path: generatePath(item.parent.getHierarchy()),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
propertyTypeTokens: this.propertyTypeTokens,
|
||||
readonly: this.readonly,
|
||||
optional: this.optional,
|
||||
inheritanceData: this.inheritanceData,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import type { ApiModel, ApiTypeAlias } from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import { TypeParameterMixin } from './TypeParameterMixin';
|
||||
import { type TokenDocumentation, genToken } from '~/util/parse.server';
|
||||
|
||||
export class DocTypeAlias extends TypeParameterMixin(DocItem<ApiTypeAlias>) {
|
||||
public readonly typeTokens: TokenDocumentation[];
|
||||
|
||||
public constructor(model: ApiModel, item: ApiTypeAlias) {
|
||||
super(model, item);
|
||||
this.typeTokens = item.typeExcerpt.spannedTokens.map((token) => genToken(model, token));
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
typeTokens: this.typeTokens,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { ApiModel, ApiVariable } from '@microsoft/api-extractor-model';
|
||||
import { DocItem } from './DocItem';
|
||||
import { genToken, TokenDocumentation } from '~/util/parse.server';
|
||||
|
||||
export class DocVariable extends DocItem<ApiVariable> {
|
||||
public readonly typeTokens: TokenDocumentation[] = [];
|
||||
public readonly readonly: boolean;
|
||||
|
||||
public constructor(model: ApiModel, item: ApiVariable) {
|
||||
super(model, item);
|
||||
this.typeTokens = item.variableTypeExcerpt.spannedTokens.map((token) => genToken(model, token));
|
||||
this.readonly = item.isReadonly;
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
typeTokens: this.typeTokens,
|
||||
readonly: this.readonly,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { ApiItem, ApiModel, ApiTypeParameterListMixin, TypeParameter } from '@microsoft/api-extractor-model';
|
||||
import type { DocItemConstructor } from './DocItem';
|
||||
import type { ApiItem, ApiModel, TypeParameter } from '@microsoft/api-extractor-model';
|
||||
import { block, DocBlockJSON } from './comment/CommentBlock';
|
||||
import { genToken, TokenDocumentation } from '~/util/parse.server';
|
||||
|
||||
@@ -27,24 +26,3 @@ export function generateTypeParamData(
|
||||
commentBlock: typeParam.tsdocTypeParamBlock ? block(typeParam.tsdocTypeParamBlock, model, parentItem) : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function TypeParameterMixin<TBase extends DocItemConstructor>(Base: TBase) {
|
||||
return class Mixed extends Base {
|
||||
public readonly typeParameters: TypeParameterData[] = [];
|
||||
|
||||
public constructor(...args: any[]);
|
||||
public constructor(model: ApiModel, item: ApiItem) {
|
||||
super(model, item);
|
||||
this.typeParameters = (item as ApiTypeParameterListMixin).typeParameters.map((typeParam) =>
|
||||
generateTypeParamData(this.model, typeParam, item.parent),
|
||||
);
|
||||
}
|
||||
|
||||
public override toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
typeParameterData: this.typeParameters,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user