chore: let TypeScript infer type (#12256)

This commit is contained in:
Nikolay Vitkov 2024-04-11 18:55:49 +02:00 committed by GitHub
parent 75f170b26d
commit 958fce0fba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -88,7 +88,7 @@ export interface IMarkdownDocumenterOptions {
export class CustomMarkdownEmitter extends ApiFormatterMarkdownEmitter {
protected override getEscapedText(text: string): string {
const textWithBackslashes: string = text
const textWithBackslashes = text
.replace(/\\/g, '\\\\') // first replace the escape character
.replace(/[*#[\]_|`~]/g, x => {
return '\\' + x;
@ -150,12 +150,12 @@ export class MarkdownDocumenter {
}
private _writeApiItemPage(apiItem: ApiItem): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const output: DocSection = new DocSection({
configuration: this._tsdocConfiguration,
const configuration = this._tsdocConfiguration;
const output = new DocSection({
configuration,
});
const scopedName: string = apiItem.getScopedNameWithinPackage();
const scopedName = apiItem.getScopedNameWithinPackage();
switch (apiItem.kind) {
case ApiItemKind.Class:
@ -253,7 +253,7 @@ export class MarkdownDocumenter {
new DocNoteBox({configuration: this._tsdocConfiguration}, [
new DocParagraph({configuration: this._tsdocConfiguration}, [
new DocPlainText({
configuration: this._tsdocConfiguration,
configuration,
text: 'Warning: This API is now obsolete. ',
}),
]),
@ -363,11 +363,11 @@ export class MarkdownDocumenter {
this._writeRemarksSection(output, apiItem);
}
const filename: string = path.join(
const filename = path.join(
this._outputFolder,
this._getFilenameForApiItem(apiItem)
);
const stringBuilder: StringBuilder = new StringBuilder();
const stringBuilder = new StringBuilder();
this._markdownEmitter.emit(stringBuilder, output, {
contextApiItem: apiItem,
@ -376,7 +376,7 @@ export class MarkdownDocumenter {
},
});
let pageContent: string = stringBuilder.toString();
let pageContent = stringBuilder.toString();
if (this._pluginLoader.markdownDocumenterFeature) {
// Allow the plugin to customize the pageContent
@ -407,18 +407,15 @@ export class MarkdownDocumenter {
output: DocSection,
apiItem: ApiDeclaredItem
): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
if (apiItem instanceof ApiClass) {
if (apiItem.extendsType) {
const extendsParagraph: DocParagraph = new DocParagraph(
{configuration},
[
const extendsParagraph = new DocParagraph({configuration}, [
new DocEmphasisSpan({configuration, bold: true}, [
new DocPlainText({configuration, text: 'Extends: '}),
]),
]
);
]);
this._appendExcerptWithHyperlinks(
extendsParagraph,
apiItem.extendsType.excerpt
@ -426,14 +423,11 @@ export class MarkdownDocumenter {
output.appendNode(extendsParagraph);
}
if (apiItem.implementsTypes.length > 0) {
const extendsParagraph: DocParagraph = new DocParagraph(
{configuration},
[
const extendsParagraph = new DocParagraph({configuration}, [
new DocEmphasisSpan({configuration, bold: true}, [
new DocPlainText({configuration, text: 'Implements: '}),
]),
]
);
]);
let needsComma = false;
for (const implementsType of apiItem.implementsTypes) {
if (needsComma) {
@ -453,14 +447,11 @@ export class MarkdownDocumenter {
if (apiItem instanceof ApiInterface) {
if (apiItem.extendsTypes.length > 0) {
const extendsParagraph: DocParagraph = new DocParagraph(
{configuration},
[
const extendsParagraph = new DocParagraph({configuration}, [
new DocEmphasisSpan({configuration, bold: true}, [
new DocPlainText({configuration, text: 'Extends: '}),
]),
]
);
]);
let needsComma = false;
for (const extendsType of apiItem.extendsTypes) {
if (needsComma) {
@ -490,14 +481,11 @@ export class MarkdownDocumenter {
);
});
if (refs.length > 0) {
const referencesParagraph: DocParagraph = new DocParagraph(
{configuration},
[
const referencesParagraph = new DocParagraph({configuration}, [
new DocEmphasisSpan({configuration, bold: true}, [
new DocPlainText({configuration, text: 'References: '}),
]),
]
);
]);
let needsComma = false;
const visited = new Set<string>();
for (const ref of refs) {
@ -542,6 +530,8 @@ export class MarkdownDocumenter {
}
private _writeRemarksSection(output: DocSection, apiItem: ApiItem): void {
const configuration = this._tsdocConfiguration;
if (apiItem instanceof ApiDocumentedItem) {
const tsdocComment: DocComment | undefined = apiItem.tsdocComment;
@ -550,7 +540,7 @@ export class MarkdownDocumenter {
if (tsdocComment.remarksBlock) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Remarks',
})
);
@ -574,7 +564,7 @@ export class MarkdownDocumenter {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: heading,
})
);
@ -621,15 +611,15 @@ export class MarkdownDocumenter {
* GENERATE PAGE: MODEL
*/
private _writeModelTable(output: DocSection, apiModel: ApiModel): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const packagesTable: DocTable = new DocTable({
const packagesTable = new DocTable({
configuration,
headerTitles: ['Package', 'Description'],
});
for (const apiMember of apiModel.members) {
const row: DocTableRow = new DocTableRow({configuration}, [
const row = new DocTableRow({configuration}, [
this._createTitleCell(apiMember),
this._createDescriptionCell(apiMember),
]);
@ -645,7 +635,7 @@ export class MarkdownDocumenter {
if (packagesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Packages',
})
);
@ -660,39 +650,39 @@ export class MarkdownDocumenter {
output: DocSection,
apiContainer: ApiPackage | ApiNamespace
): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const classesTable: DocTable = new DocTable({
const classesTable = new DocTable({
configuration,
headerTitles: ['Class', 'Description'],
});
const enumerationsTable: DocTable = new DocTable({
const enumerationsTable = new DocTable({
configuration,
headerTitles: ['Enumeration', 'Description'],
});
const functionsTable: DocTable = new DocTable({
const functionsTable = new DocTable({
configuration,
headerTitles: ['Function', 'Description'],
});
const interfacesTable: DocTable = new DocTable({
const interfacesTable = new DocTable({
configuration,
headerTitles: ['Interface', 'Description'],
});
const namespacesTable: DocTable = new DocTable({
const namespacesTable = new DocTable({
configuration,
headerTitles: ['Namespace', 'Description'],
});
const variablesTable: DocTable = new DocTable({
const variablesTable = new DocTable({
configuration,
headerTitles: ['Variable', 'Description'],
});
const typeAliasesTable: DocTable = new DocTable({
const typeAliasesTable = new DocTable({
configuration,
headerTitles: ['Type Alias', 'Description'],
});
@ -703,7 +693,7 @@ export class MarkdownDocumenter {
: (apiContainer as ApiNamespace).members;
for (const apiMember of apiMembers) {
const row: DocTableRow = new DocTableRow({configuration}, [
const row = new DocTableRow({configuration}, [
this._createTitleCell(apiMember),
this._createDescriptionCell(apiMember),
]);
@ -749,7 +739,7 @@ export class MarkdownDocumenter {
if (classesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Classes',
})
);
@ -759,7 +749,7 @@ export class MarkdownDocumenter {
if (enumerationsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Enumerations',
})
);
@ -768,7 +758,7 @@ export class MarkdownDocumenter {
if (functionsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Functions',
})
);
@ -778,7 +768,7 @@ export class MarkdownDocumenter {
if (interfacesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Interfaces',
})
);
@ -788,7 +778,7 @@ export class MarkdownDocumenter {
if (namespacesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Namespaces',
})
);
@ -798,7 +788,7 @@ export class MarkdownDocumenter {
if (variablesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Variables',
})
);
@ -808,7 +798,7 @@ export class MarkdownDocumenter {
if (typeAliasesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Type Aliases',
})
);
@ -820,24 +810,24 @@ export class MarkdownDocumenter {
* GENERATE PAGE: CLASS
*/
private _writeClassTables(output: DocSection, apiClass: ApiClass): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const eventsTable: DocTable = new DocTable({
const eventsTable = new DocTable({
configuration,
headerTitles: ['Property', 'Modifiers', 'Type', 'Description'],
});
const constructorsTable: DocTable = new DocTable({
const constructorsTable = new DocTable({
configuration,
headerTitles: ['Constructor', 'Modifiers', 'Description'],
});
const propertiesTable: DocTable = new DocTable({
const propertiesTable = new DocTable({
configuration,
headerTitles: ['Property', 'Modifiers', 'Type', 'Description'],
});
const methodsTable: DocTable = new DocTable({
const methodsTable = new DocTable({
configuration,
headerTitles: ['Method', 'Modifiers', 'Description'],
});
@ -896,7 +886,7 @@ export class MarkdownDocumenter {
if (eventsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Events',
})
);
@ -906,7 +896,7 @@ export class MarkdownDocumenter {
if (constructorsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Constructors',
})
);
@ -916,7 +906,7 @@ export class MarkdownDocumenter {
if (propertiesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Properties',
})
);
@ -926,7 +916,7 @@ export class MarkdownDocumenter {
if (methodsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Methods',
})
);
@ -938,9 +928,9 @@ export class MarkdownDocumenter {
* GENERATE PAGE: ENUM
*/
private _writeEnumTables(output: DocSection, apiEnum: ApiEnum): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const enumMembersTable: DocTable = new DocTable({
const enumMembersTable = new DocTable({
configuration,
headerTitles: ['Member', 'Value', 'Description'],
});
@ -965,7 +955,7 @@ export class MarkdownDocumenter {
if (enumMembersTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Enumeration Members',
})
);
@ -980,19 +970,19 @@ export class MarkdownDocumenter {
output: DocSection,
apiClass: ApiInterface
): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const eventsTable: DocTable = new DocTable({
const eventsTable = new DocTable({
configuration,
headerTitles: ['Property', 'Modifiers', 'Type', 'Description'],
});
const propertiesTable: DocTable = new DocTable({
const propertiesTable = new DocTable({
configuration,
headerTitles: ['Property', 'Modifiers', 'Type', 'Description', 'Default'],
});
const methodsTable: DocTable = new DocTable({
const methodsTable = new DocTable({
configuration,
headerTitles: ['Method', 'Description'],
});
@ -1040,7 +1030,7 @@ export class MarkdownDocumenter {
if (eventsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Events',
})
);
@ -1050,7 +1040,7 @@ export class MarkdownDocumenter {
if (propertiesTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Properties',
})
);
@ -1060,7 +1050,7 @@ export class MarkdownDocumenter {
if (methodsTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Methods',
})
);
@ -1075,14 +1065,14 @@ export class MarkdownDocumenter {
output: DocSection,
apiParameterListMixin: ApiParameterListMixin
): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const parametersTable: DocTable = new DocTable({
const parametersTable = new DocTable({
configuration,
headerTitles: ['Parameter', 'Type', 'Description'],
});
for (const apiParameter of apiParameterListMixin.parameters) {
const parameterDescription: DocSection = new DocSection({configuration});
const parameterDescription = new DocSection({configuration});
if (apiParameter.isOptional) {
parameterDescription.appendNodesInParagraph([
@ -1120,7 +1110,7 @@ export class MarkdownDocumenter {
if (parametersTable.rows.length > 0) {
output.appendNode(
new DocHeading({
configuration: this._tsdocConfiguration,
configuration,
title: 'Parameters',
})
);
@ -1155,9 +1145,9 @@ export class MarkdownDocumenter {
}
private _createParagraphForTypeExcerpt(excerpt: Excerpt): DocParagraph {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const paragraph: DocParagraph = new DocParagraph({configuration});
const paragraph = new DocParagraph({configuration});
if (!excerpt.text.trim()) {
paragraph.appendNode(
new DocPlainText({configuration, text: '(not declared)'})
@ -1182,13 +1172,13 @@ export class MarkdownDocumenter {
docNodeContainer: DocNodeContainer,
token: ExcerptToken
): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
// Markdown doesn't provide a standardized syntax for hyperlinks inside code
// spans, so we will render the type expression as DocPlainText. Instead of
// creating multiple DocParagraphs, we can simply discard any newlines and
// let the renderer do normal word-wrapping.
const unwrappedTokenText: string = token.text.replace(/[\r\n]+/g, ' ');
const unwrappedTokenText = token.text.replace(/[\r\n]+/g, ' ');
// If it's hyperlinkable, then append a DocLinkTag
if (token.kind === ExcerptTokenKind.Reference && token.canonicalReference) {
@ -1220,9 +1210,9 @@ export class MarkdownDocumenter {
}
private _createTitleCell(apiItem: ApiItem, plain = false): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const text: string = Utilities.getConciseSignature(apiItem);
const text = Utilities.getConciseSignature(apiItem);
return new DocTableCell({configuration}, [
new DocParagraph({configuration}, [
@ -1263,9 +1253,9 @@ export class MarkdownDocumenter {
* cast.
*/
private _createDescriptionCell(apiItem: ApiItem): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const section: DocSection = new DocSection({configuration});
const section = new DocSection({configuration});
if (ApiReleaseTagMixin.isBaseClassOf(apiItem)) {
if (apiItem.releaseTag === ReleaseTag.Beta) {
@ -1305,7 +1295,7 @@ export class MarkdownDocumenter {
}
private _createDefaultCell(apiItem: ApiItem): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
if (apiItem instanceof ApiDocumentedItem) {
const block = apiItem.tsdocComment?.customBlocks.find(block => {
@ -1323,9 +1313,9 @@ export class MarkdownDocumenter {
}
private _createModifiersCell(apiItem: ApiItem): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const section: DocSection = new DocSection({configuration});
const section = new DocSection({configuration});
const codes = [];
@ -1370,9 +1360,9 @@ export class MarkdownDocumenter {
}
private _createPropertyTypeCell(apiItem: ApiItem): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const section: DocSection = new DocSection({configuration});
const section = new DocSection({configuration});
if (apiItem instanceof ApiPropertyItem) {
section.appendNode(
@ -1384,9 +1374,9 @@ export class MarkdownDocumenter {
}
private _createInitializerCell(apiItem: ApiItem): DocTableCell {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const configuration = this._tsdocConfiguration;
const section: DocSection = new DocSection({configuration});
const section = new DocSection({configuration});
if (ApiInitializerMixin.isBaseClassOf(apiItem)) {
if (apiItem.initializerExcerpt) {
@ -1403,8 +1393,8 @@ export class MarkdownDocumenter {
}
private _writeBetaWarning(output: DocSection): void {
const configuration: TSDocConfiguration = this._tsdocConfiguration;
const betaWarning: string =
const configuration = this._tsdocConfiguration;
const betaWarning =
'This API is provided as a preview for developers and may change' +
' based on feedback that we receive. Do not use this API in a production environment.';
output.appendNode(
@ -1449,7 +1439,7 @@ export class MarkdownDocumenter {
let baseName = '';
for (const hierarchyItem of apiItem.getHierarchy()) {
// For overloaded methods, add a suffix such as "MyClass.myMethod_2".
let qualifiedName: string = hierarchyItem.displayName;
let qualifiedName = hierarchyItem.displayName;
if (ApiParameterListMixin.isBaseClassOf(hierarchyItem)) {
if (hierarchyItem.overloadIndex > 1) {
// Subtract one for compatibility with earlier releases of API Documenter.
@ -1479,7 +1469,7 @@ export class MarkdownDocumenter {
let suffix = '';
for (const hierarchyItem of apiItem.getHierarchy()) {
// For overloaded methods, add a suffix such as "MyClass.myMethod_2".
let qualifiedName: string = Utilities.getSafeFilenameForName(
let qualifiedName = Utilities.getSafeFilenameForName(
hierarchyItem.displayName
);
if (ApiParameterListMixin.isBaseClassOf(hierarchyItem)) {