fix(docs): index all deeply nested types (#31690)

This commit is contained in:
Max Schmitt 2024-07-15 16:35:15 +02:00 committed by GitHub
parent 58bcdde956
commit 950875f0db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 9 deletions

View file

@ -92,12 +92,13 @@ class ApiParser {
const match = spec.text.match(/(event|method|property|async method|optional method|optional async method): ([^.]+)\.(.*)/); const match = spec.text.match(/(event|method|property|async method|optional method|optional async method): ([^.]+)\.(.*)/);
if (!match) if (!match)
throw new Error('Invalid member: ' + spec.text); throw new Error('Invalid member: ' + spec.text);
const metainfo = extractMetainfo(spec);
const name = match[3]; const name = match[3];
let returnType = null; let returnType = null;
let optional = false; let optional = false;
for (const item of spec.children || []) { for (const item of spec.children || []) {
if (item.type === 'li' && item.liType === 'default') { if (item.type === 'li' && item.liType === 'default') {
const parsed = this.parseType(item); const parsed = this.parseType(item, metainfo.since ?? 'v1.0');
returnType = parsed.type; returnType = parsed.type;
optional = parsed.optional; optional = parsed.optional;
} }
@ -106,7 +107,6 @@ class ApiParser {
returnType = new docs.Type('void'); returnType = new docs.Type('void');
const comments = extractComments(spec); const comments = extractComments(spec);
const metainfo = extractMetainfo(spec);
let member; let member;
if (match[1] === 'event') if (match[1] === 'event')
member = docs.Member.createEvent(metainfo, name, returnType, comments); member = docs.Member.createEvent(metainfo, name, returnType, comments);
@ -188,7 +188,7 @@ class ApiParser {
let options = method.argsArray.find(o => o.name === 'options'); let options = method.argsArray.find(o => o.name === 'options');
if (!options) { if (!options) {
const type = new docs.Type('Object', []); const type = new docs.Type('Object', []);
options = docs.Member.createProperty({ langs: {}, since: 'v1.0', deprecated: undefined, discouraged: undefined }, 'options', type, undefined, false); options = docs.Member.createProperty({ langs: {}, since: method.since, deprecated: undefined, discouraged: undefined }, 'options', type, undefined, false);
method.argsArray.push(options); method.argsArray.push(options);
} }
p.required = false; p.required = false;
@ -209,25 +209,26 @@ class ApiParser {
typeStart--; typeStart--;
const name = text.substring(0, typeStart).replace(/\`/g, '').trim(); const name = text.substring(0, typeStart).replace(/\`/g, '').trim();
const comments = extractComments(spec); const comments = extractComments(spec);
const { type, optional } = this.parseType(/** @type {MarkdownLiNode} */(param));
const metainfo = extractMetainfo(spec); const metainfo = extractMetainfo(spec);
if (metainfo.hidden) if (metainfo.hidden)
return null; return null;
const { type, optional } = this.parseType(/** @type {MarkdownLiNode} */(param), metainfo.since ?? 'v1.0');
return docs.Member.createProperty(metainfo, name, type, comments, !optional); return docs.Member.createProperty(metainfo, name, type, comments, !optional);
} }
/** /**
* @param {MarkdownLiNode} spec * @param {MarkdownLiNode} spec
* @param {string} since
* @return {{ type: docs.Type, optional: boolean }} * @return {{ type: docs.Type, optional: boolean }}
*/ */
parseType(spec) { parseType(spec, since) {
const arg = parseVariable(spec.text); const arg = parseVariable(spec.text);
const properties = []; const properties = [];
for (const child of /** @type {MarkdownLiNode[]} */ (spec.children) || []) { for (const child of /** @type {MarkdownLiNode[]} */ (spec.children) || []) {
const { name, text } = parseVariable(/** @type {string} */(child.text)); const { name, text } = parseVariable(/** @type {string} */(child.text));
const comments = /** @type {MarkdownNode[]} */ ([{ type: 'text', text }]); const comments = /** @type {MarkdownNode[]} */ ([{ type: 'text', text }]);
const childType = this.parseType(child); const childType = this.parseType(child, since);
properties.push(docs.Member.createProperty({ langs: {}, since: 'v1.0', deprecated: undefined, discouraged: undefined }, name, childType.type, comments, !childType.optional)); properties.push(docs.Member.createProperty({ langs: {}, since, deprecated: undefined, discouraged: undefined }, name, childType.type, comments, !childType.optional));
} }
const type = docs.Type.parse(arg.type, properties); const type = docs.Type.parse(arg.type, properties);
return { type, optional: arg.optional }; return { type, optional: arg.optional };

View file

@ -372,15 +372,20 @@ class Member {
this.args = new Map(); this.args = new Map();
if (this.kind === 'method') if (this.kind === 'method')
this.enclosingMethod = this; this.enclosingMethod = this;
const indexType = type => {
type.deepProperties().forEach(p => {
p.enclosingMethod = this;
indexType(p.type);
});
}
for (const arg of this.argsArray) { for (const arg of this.argsArray) {
this.args.set(arg.name, arg); this.args.set(arg.name, arg);
arg.enclosingMethod = this; arg.enclosingMethod = this;
if (arg.name === 'options') { if (arg.name === 'options') {
// @ts-ignore // @ts-ignore
arg.type.properties.sort((p1, p2) => p1.name.localeCompare(p2.name)); arg.type.properties.sort((p1, p2) => p1.name.localeCompare(p2.name));
// @ts-ignore
arg.type.properties.forEach(p => p.enclosingMethod = this);
} }
indexType(arg.type);
} }
} }