@alicloud/console-toolkit-plugin-docs
Version:
console toolkit plugin for build docs
106 lines • 5.08 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectInterfaceInfo = void 0;
var typescript_1 = __importDefault(require("typescript"));
var defaultTsConfig = {
moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs,
};
function collectInterfaceInfo(fileName) {
var _a;
// Build a program using the set of root file names in fileNames
var program = typescript_1.default.createProgram([fileName], defaultTsConfig);
// Get the checker, we will use it to find more about classes
var checker = program.getTypeChecker();
var sourceFile = program.getSourceFile(fileName);
// inspired by
// https://github.com/microsoft/rushstack/blob/6ca0cba723ad8428e6e099f12715ce799f29a73f/apps/api-extractor/src/analyzer/ExportAnalyzer.ts#L702
// and https://stackoverflow.com/a/58885450
var fileSymbol = checker.getSymbolAtLocation(sourceFile);
if (!fileSymbol || !fileSymbol.exports) {
throw new Error("unexpected fileSymbol");
}
// 找第一个export
var exportSymbol = fileSymbol.exports.values().next().value;
if (!exportSymbol) {
throw new Error("no export is not found in file");
}
var sourceDeclareSymbol = getAliasedSymbolIfNecessary(exportSymbol);
var sourceDeclare = (_a = sourceDeclareSymbol.declarations) === null || _a === void 0 ? void 0 : _a[0];
if (!sourceDeclare) {
throw new Error("Can't find sourceDeclare");
}
var interfaceInfo = collectInterfaceInfo(sourceDeclare, sourceDeclareSymbol);
return interfaceInfo;
function getAliasedSymbolIfNecessary(symbol) {
if ((symbol.flags & typescript_1.default.SymbolFlags.Alias) !== 0)
return checker.getAliasedSymbol(symbol);
return symbol;
}
function collectInterfaceInfo(node, symbol) {
var _a;
if (!typescript_1.default.isInterfaceDeclaration(node))
throw new Error("target is not an InterfaceDeclaration");
if (!symbol)
throw new Error("can't find symbol");
var name = node.name.getText();
var documentation = typescript_1.default.displayPartsToString(symbol.getDocumentationComment(checker));
var propertiesInfo = [];
(_a = symbol.members) === null || _a === void 0 ? void 0 : _a.forEach(function (symbol) {
var _a, _b;
var name = symbol.name;
var declaration = symbol.valueDeclaration;
if (!(declaration &&
(typescript_1.default.isPropertySignature(declaration) ||
typescript_1.default.isMethodSignature(declaration)))) {
throw new Error("unexpected declaration type in interface. name: ".concat(name, ", kind: ").concat(
// @ts-ignore
typescript_1.default.SyntaxKind[declaration.kind]));
}
var typeText = (_b = (_a = declaration.type) === null || _a === void 0 ? void 0 : _a.getFullText()) !== null && _b !== void 0 ? _b : "";
var documentation = typescript_1.default.displayPartsToString(symbol.getDocumentationComment(checker));
var defaultValue = (function () {
var _a, _b;
var jsDoc = (_a = declaration.jsDoc) === null || _a === void 0 ? void 0 : _a[0];
var defaultValueTag = (_b = jsDoc === null || jsDoc === void 0 ? void 0 : jsDoc.tags) === null || _b === void 0 ? void 0 : _b.find(function (t) {
return (t.tagName.escapedText === "defaultValue" ||
t.tagName.escapedText === "default");
});
return defaultValueTag === null || defaultValueTag === void 0 ? void 0 : defaultValueTag.comment;
})();
propertiesInfo.push({
name: name,
typeText: typeText,
documentation: documentation,
// @ts-ignore
defaultValue: defaultValue,
});
});
var interfaceInfo = {
name: name,
documentation: documentation,
properties: propertiesInfo,
};
return interfaceInfo;
}
}
exports.collectInterfaceInfo = collectInterfaceInfo;
/**
* ref:
*
* https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
*
* https://stackoverflow.com/questions/59838013/how-can-i-use-the-ts-compiler-api-to-find-where-a-variable-was-defined-in-anothe
*
* https://stackoverflow.com/questions/60249275/typescript-compiler-api-generate-the-full-properties-arborescence-of-a-type-ide
*
* https://stackoverflow.com/questions/47429792/is-it-possible-to-get-comments-as-nodes-in-the-ast-using-the-typescript-compiler
*
* Instructions of learning ts compiler:
* https://stackoverflow.com/a/58885450
*
* https://learning-notes.mistermicheels.com/javascript/typescript/compiler-api/
*/
//# sourceMappingURL=collectInterfaceInfo.js.map