UNPKG

@microsoft/api-extractor

Version:

Validate, document, and review the exported API for a TypeScript library

85 lines (83 loc) 3.45 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. Object.defineProperty(exports, "__esModule", { value: true }); /* tslint:disable:no-bitwise */ const ts = require("typescript"); const AstItem_1 = require("./AstItem"); const AstModule_1 = require("./AstModule"); const TypeScriptHelpers_1 = require("../TypeScriptHelpers"); /** * This class is part of the AstItem abstract syntax tree. It represents the top-level * exports for an Rush package. This object acts as the root of the Extractor's tree. */ class AstPackage extends AstModule_1.default { constructor(context, rootFile) { super(AstPackage._getOptions(context, rootFile)); this._exportedNormalizedSymbols = []; this.kind = AstItem_1.AstItemKind.Package; // The scoped package name. (E.g. "@microsoft/api-extractor") this.name = context.packageName; const exportSymbols = this.typeChecker.getExportsOfModule(this.declarationSymbol); if (exportSymbols) { for (const exportSymbol of exportSymbols) { this.processModuleExport(exportSymbol); const followedSymbol = this.followAliases(exportSymbol); this._exportedNormalizedSymbols.push({ exportedName: exportSymbol.name, followedSymbol: followedSymbol }); } } } static _getOptions(context, rootFile) { const rootFileSymbol = TypeScriptHelpers_1.default.getSymbolForDeclaration(rootFile); let statement; let foundDescription = undefined; for (const statementNode of rootFile.statements) { if (statementNode.kind === ts.SyntaxKind.VariableStatement) { statement = statementNode; for (const statementDeclaration of statement.declarationList.declarations) { if (statementDeclaration.name.getText() === 'packageDescription') { foundDescription = statement; } } } } if (!rootFileSymbol.declarations) { throw new Error('Unable to find a root declaration for this package'); } return { context, declaration: rootFileSymbol.declarations[0], declarationSymbol: rootFileSymbol, jsdocNode: foundDescription }; } /** * Finds and returns the original symbol name. * * For example, suppose a class is defined as "export default class MyClass { }" * but exported from the package's index.ts like this: * * export { default as _MyClass } from './MyClass'; * * In this example, given the symbol for _MyClass, getExportedSymbolName() will return * the string "MyClass". */ tryGetExportedSymbolName(symbol) { const followedSymbol = this.followAliases(symbol); for (const exportedSymbol of this._exportedNormalizedSymbols) { if (exportedSymbol.followedSymbol === followedSymbol) { return exportedSymbol.exportedName; } } return undefined; } shouldHaveDocumentation() { // We don't write JSDoc for the AstPackage object return false; } } exports.default = AstPackage; //# sourceMappingURL=AstPackage.js.map