playwright/utils/doclint/generateApiJson.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-07-31 03:51:41 +03:00
/**
* 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.
*/
2020-12-29 04:38:00 +03:00
// @ts-check
2020-07-31 03:51:41 +03:00
const path = require('path');
2020-12-29 23:12:46 +03:00
const Documentation = require('./Documentation');
2020-12-29 04:38:00 +03:00
const { MDOutline } = require('./MDBuilder');
2020-07-31 03:51:41 +03:00
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 => {
2020-12-29 23:12:46 +03:00
const { clazz, member, param, option } = item;
if (param)
return `\`${param}\``;
if (option)
return `\`${option}\``;
if (clazz)
return `\`${clazz.name}\``;
});
outline.copyDocsFromSuperclasses([]);
2020-12-29 23:12:46 +03:00
outline.generateSourceCodeComments();
const result = serialize(outline);
console.log(JSON.stringify(result));
}
2020-07-31 03:51:41 +03:00
2020-12-29 23:12:46 +03:00
/**
* @param {Documentation} documentation
*/
2020-07-31 03:51:41 +03:00
function serialize(documentation) {
return documentation.classesArray.map(serializeClass);
2020-07-31 03:51:41 +03:00
}
2020-12-29 23:12:46 +03:00
/**
* @param {Documentation.Class} clazz
*/
2020-07-31 03:51:41 +03:00
function serializeClass(clazz) {
const result = { name: clazz.name };
if (clazz.extends)
result.extends = clazz.extends;
if (clazz.comment)
result.comment = clazz.comment;
result.members = clazz.membersArray.map(serializeMember);
2020-07-31 03:51:41 +03:00
return result;
}
2020-12-29 23:12:46 +03:00
/**
* @param {Documentation.Member} member
*/
2020-07-31 03:51:41 +03:00
function serializeMember(member) {
2020-12-29 23:12:46 +03:00
const result = /** @type {any} */ ({ ...member });
sanitize(result);
result.args = member.argsArray.map(serializeProperty);
2020-07-31 03:51:41 +03:00
if (member.type)
result.type = serializeType(member.type)
return result;
}
function serializeProperty(arg) {
const result = { ...arg };
sanitize(result);
2020-07-31 03:51:41 +03:00
if (arg.type)
result.type = serializeType(arg.type)
return result;
}
function sanitize(result) {
delete result.args;
delete result.argsArray;
2020-12-29 23:12:46 +03:00
delete result.clazz;
delete result.spec;
}
/**
* @param {Documentation.Type} type
*/
2020-07-31 03:51:41 +03:00
function serializeType(type) {
/** @type {any} */
2020-07-31 03:51:41 +03:00
const result = { ...type };
if (type.properties)
result.properties = type.properties.map(serializeProperty);
if (type.union)
result.union = type.union.map(serializeType);
if (type.templates)
result.templates = type.templates.map(serializeType);
if (type.args)
result.args = type.args.map(serializeType);
if (type.returnType)
result.returnType = serializeType(type.returnType);
2020-07-31 03:51:41 +03:00
return result;
}