fix: type mapping for docgen methods/props (#9969)

* fix: minify mainlib docs json

* fix: minify them all

* fix: type mapping for docgen methods/props
This commit is contained in:
Qjuh
2023-11-14 21:54:29 +01:00
committed by GitHub
parent 4b88306dcb
commit cab60142ff

View File

@@ -219,12 +219,15 @@ function filePathFromJson(meta: DocgenMetaJson): string {
return `${meta.path.slice('packages/discord.js/'.length)}/${meta.file}`; return `${meta.path.slice('packages/discord.js/'.length)}/${meta.file}`;
} }
function formatVarType(type: DocgenVarTypeJson): string { function fixPrimitiveTypes(type: string) {
return (Array.isArray(type) ? type : type.types ?? []).map((t1) => t1.map((t2) => t2.join('')).join('')).join(' | '); switch (type) {
} case '*':
return 'any';
function getFirstType(type: DocgenVarTypeJson): string { case 'Object':
return (Array.isArray(type) ? type[0]?.[0]?.[0] : type.types?.[0]?.[0]?.[0]) ?? 'unknown'; return 'object';
default:
return type;
}
} }
export class ApiModelGenerator { export class ApiModelGenerator {
@@ -1086,7 +1089,12 @@ export class ApiModelGenerator {
fileColumn: sourceLocation.sourceFileColumn, fileColumn: sourceLocation.sourceFileColumn,
}); });
} else if (jsDoc) { } else if (jsDoc) {
apiMethod = new ApiMethod(this._mapMethod(jsDoc, parentApiItem.getAssociatedPackage()!.name)); const methodOptions = this._mapMethod(jsDoc, parentApiItem.getAssociatedPackage()!.name);
if (methodOptions.releaseTag === ReleaseTag.Internal || methodOptions.releaseTag === ReleaseTag.Alpha) {
return; // trim out items marked as "@internal" or "@alpha"
}
apiMethod = new ApiMethod(methodOptions);
} }
parentApiItem.addMember(apiMethod); parentApiItem.addMember(apiMethod);
@@ -1276,9 +1284,12 @@ export class ApiModelGenerator {
fileColumn: sourceLocation.sourceFileColumn, fileColumn: sourceLocation.sourceFileColumn,
}); });
} else if (parentApiItem.kind === ApiItemKind.Class || parentApiItem.kind === ApiItemKind.Interface) { } else if (parentApiItem.kind === ApiItemKind.Class || parentApiItem.kind === ApiItemKind.Interface) {
apiProperty = new ApiProperty( const propertyOptions = this._mapProp(jsDoc as DocgenPropertyJson, parentApiItem.getAssociatedPackage()!.name);
this._mapProp(jsDoc as DocgenPropertyJson, parentApiItem.getAssociatedPackage()!.name), if (propertyOptions.releaseTag === ReleaseTag.Internal || propertyOptions.releaseTag === ReleaseTag.Alpha) {
); return; // trim out items marked as "@internal" or "@alpha"
}
apiProperty = new ApiProperty(propertyOptions);
} else { } else {
console.log(`We got a property in ApiItem of kind ${ApiItemKind[parentApiItem.kind]}`); console.log(`We got a property in ApiItem of kind ${ApiItemKind[parentApiItem.kind]}`);
} }
@@ -1664,38 +1675,44 @@ export class ApiModelGenerator {
[ts.SyntaxKind.InterfaceDeclaration]: 'interface', [ts.SyntaxKind.InterfaceDeclaration]: 'interface',
[ts.SyntaxKind.TypeAliasDeclaration]: 'type', [ts.SyntaxKind.TypeAliasDeclaration]: 'type',
}; };
return mapper.flatMap((typ) => return mapper
typ.reduce<IExcerptToken[]>( .flatMap((typ, index) => {
(arr, [type, symbol]) => [ const result = typ.reduce<IExcerptToken[]>(
...arr, (arr, [type, symbol]) => [
{ ...arr,
kind: type?.includes("'") ? ExcerptTokenKind.Content : ExcerptTokenKind.Reference, {
text: type ?? 'unknown', kind: type?.includes("'") ? ExcerptTokenKind.Content : ExcerptTokenKind.Reference,
canonicalReference: type?.includes("'") text: fixPrimitiveTypes(type ?? 'unknown'),
? undefined canonicalReference: type?.includes("'")
: DeclarationReference.package(this._apiModel.packages[0]!.name) ? undefined
.addNavigationStep(Navigation.Members as any, DeclarationReference.parseComponent(type ?? 'unknown')) : DeclarationReference.package(this._apiModel.packages[0]!.name)
.withMeaning( .addNavigationStep(
lookup[ Navigation.Members as any,
( DeclarationReference.parseComponent(type ?? 'unknown'),
(this._collector.entities.find( )
(entity) => entity.nameForEmit === type && 'astDeclarations' in entity.astEntity, .withMeaning(
)?.astEntity as AstSymbol | undefined) ?? lookup[
( (
this._collector.entities.find( (this._collector.entities.find(
(entity) => entity.nameForEmit === type && 'astSymbol' in entity.astEntity, (entity) => entity.nameForEmit === type && 'astDeclarations' in entity.astEntity,
)?.astEntity as AstImport | undefined )?.astEntity as AstSymbol | undefined) ??
)?.astSymbol (
)?.astDeclarations[0]?.declaration.kind ?? ts.SyntaxKind.ClassDeclaration this._collector.entities.find(
] ?? ('class' as any), (entity) => entity.nameForEmit === type && 'astSymbol' in entity.astEntity,
) )?.astEntity as AstImport | undefined
.toString(), )?.astSymbol
}, )?.astDeclarations[0]?.declaration.kind ?? ts.SyntaxKind.ClassDeclaration
{ kind: ExcerptTokenKind.Content, text: symbol ?? '' }, ] ?? ('class' as any),
], )
[], .toString(),
), },
); { kind: ExcerptTokenKind.Content, text: symbol ?? '' },
],
[],
);
return index === 0 ? result : [{ kind: ExcerptTokenKind.Content, text: ' | ' }, ...result];
})
.filter((excerpt) => excerpt.text.length);
} }
private _mapProp(prop: DocgenPropertyJson, _package: string): IApiPropertyOptions { private _mapProp(prop: DocgenPropertyJson, _package: string): IApiPropertyOptions {
@@ -1720,15 +1737,10 @@ export class ApiModelGenerator {
} :`, } :`,
}, },
...mappedVarType, ...mappedVarType,
{
kind: ExcerptTokenKind.Reference,
text: formatVarType(prop.type),
canonicalReference: `${_package}!${getFirstType(prop.type)}:class`,
},
{ kind: ExcerptTokenKind.Content, text: ';' }, { kind: ExcerptTokenKind.Content, text: ';' },
], ],
propertyTypeTokenRange: { startIndex: 1, endIndex: 1 + mappedVarType.length }, propertyTypeTokenRange: { startIndex: 1, endIndex: 1 + mappedVarType.length },
releaseTag: prop.access === 'public' ? ReleaseTag.Public : ReleaseTag.Internal, releaseTag: prop.access === 'private' ? ReleaseTag.Internal : ReleaseTag.Public,
fileLine: prop.meta?.line ?? 0, fileLine: prop.meta?.line ?? 0,
fileUrlPath: prop.meta ? `${prop.meta.path.slice(`packages/${_package}/`.length)}/${prop.meta.file}` : '', fileUrlPath: prop.meta ? `${prop.meta.path.slice(`packages/${_package}/`.length)}/${prop.meta.file}` : '',
}; };
@@ -1798,11 +1810,9 @@ export class ApiModelGenerator {
isStatic: method.scope === 'static', isStatic: method.scope === 'static',
overloadIndex: 1, overloadIndex: 1,
parameters: method.params?.map((param, index) => this._mapParam(param, index, _package, paramTokens)) ?? [], parameters: method.params?.map((param, index) => this._mapParam(param, index, _package, paramTokens)) ?? [],
releaseTag: method.access === 'public' ? ReleaseTag.Public : ReleaseTag.Internal, releaseTag: method.access === 'private' ? ReleaseTag.Internal : ReleaseTag.Public,
returnTypeTokenRange: method.returns?.length returnTypeTokenRange: method.returns?.length
? method.params?.length ? { startIndex: excerptTokens.length - 1 - returnTokens.length, endIndex: excerptTokens.length - 1 }
? { startIndex: 2 + 2 * method.params.length, endIndex: 3 + 2 * method.params.length }
: { startIndex: 1, endIndex: 2 }
: { startIndex: 0, endIndex: 0 }, : { startIndex: 0, endIndex: 0 },
typeParameters: [], typeParameters: [],
docComment: this._tsDocParser.parseString( docComment: this._tsDocParser.parseString(