playwright/utils/doclint/generateApiJson.js
2021-01-03 08:47:29 -08:00

125 lines
3.4 KiB
JavaScript

/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check
const path = require('path');
const Documentation = require('./Documentation');
const { MDOutline } = require('./MDBuilder');
const PROJECT_DIR = path.join(__dirname, '..', '..');
{
const outline = new MDOutline(path.join(PROJECT_DIR, 'docs', 'src', 'api-body.md'), path.join(PROJECT_DIR, 'docs', 'src', 'api-params.md'));
outline.setLinkRenderer(item => {
const { clazz, member, param, option } = item;
if (param)
return `\`${param}\``;
if (option)
return `\`${option}\``;
if (clazz)
return `\`${clazz.name}\``;
});
outline.generateSourceCodeComments();
const result = serialize(outline);
console.log(JSON.stringify(result));
}
/**
* @param {Documentation} documentation
*/
function serialize(documentation) {
const result = {};
for (const clazz of documentation.classesArray)
result[clazz.name] = serializeClass(clazz);
return result;
}
/**
* @param {Documentation.Class} clazz
*/
function serializeClass(clazz) {
const result = { name: clazz.name };
if (clazz.extends)
result.extends = clazz.extends;
if (clazz.comment)
result.comment = clazz.comment;
result.methods = {};
result.events = {};
result.properties = {};
for (const member of clazz.membersArray) {
let map;
if (member.kind === 'event') {
map = result.events;
} else if (member.kind === 'method') {
map = result.methods;
} else if (member.kind === 'property') {
map = result.properties;
} else {
throw new Error('Unexpected member kind: ' + member.kind + ' ' + member.name + ' ' + member.type);
}
map[member.name] = serializeMember(member);
}
return result;
}
/**
* @param {Documentation.Member} member
*/
function serializeMember(member) {
const result = /** @type {any} */ ({ ...member });
sanitize(result);
result.args = {};
for (const arg of member.argsArray)
result.args[arg.name] = serializeProperty(arg);
if (member.type)
result.type = serializeType(member.type)
return result;
}
function serializeProperty(arg) {
const result = { ...arg };
sanitize(result);
if (arg.type)
result.type = serializeType(arg.type)
return result;
}
function sanitize(result) {
delete result.kind;
delete result.args;
delete result.argsArray;
delete result.templates;
delete result.clazz;
if (result.properties && !Object.keys(result.properties).length)
delete result.properties;
if (result.comment === '')
delete result.comment;
if (result.returnComment === '')
delete result.returnComment;
}
function serializeType(type) {
const result = { ...type };
if (type.properties && type.properties.length) {
result.properties = {};
for (const prop of type.properties)
result.properties[prop.name] = serializeProperty(prop);
} else {
delete result.properties;
}
return result;
}