UNPKG

@otris/jsdoc-tsd

Version:

JSDoc Template for generate typescript definition files from JSDoc comments

169 lines (168 loc) 6.16 kB
import * as dom from "dts-dom"; import { Configuration } from "./Configuration"; export interface IParsedJSDocItem { longname: string; memberof?: string; original: TDoclet; parsed: dom.DeclarationBase; } export declare class JSDocTsdParser { private _parsedClassess; /** * Maps the access flags from JSDoc to declaration flags of dts-dom */ private accessFlagMap; /** * Configuration of of this template. */ private config; /** * JSDoc items which were parsed from the passed in taffy db */ private jsdocItems; /** * Tranformed JSDoc items (to declaration bases) which can * be passed in to dts-dom for output the d.ts-file. * * The membership is not resolved. JSDoc allows a lot of bullshit. * E. g. items with same name of different types can be added. This * is not valid in typescript, but we will simply ignore this to enforce * the user of this module to correct their JSDoc comments. For that reason * we store all items with same longname (Pattern: <membership>.<name>) in * an array. Once all items are parsed, we can resolve the memberships * (@see resolveMembership). */ private parsedItems; /** * Already resolved items. Will be set by "resolvedItems" */ private resolvedItems; constructor(config?: Configuration); /** * Creates the type definition file as string * @param targetPath If passed, the output will be written to the passed file path */ generateTypeDefinition(targetPath?: string): string; /** * Returns the parsed Declaration Base of an jsdoc item * @param name The longname of the jsdoc item (name including membership, e.g. "myNamespace.myMember") * @throws {Error} When no item with this name was parsed */ getParsedItem(name: string): dom.DeclarationBase[]; /** * Returns all parsed Declaration Bases */ getParsedItems(): Map<string, dom.DeclarationBase[]>; parse(jsdocItems: TDoclet[]): void; /** * Resolves the membership of all parsed items. For example a namspace member will be * added to the member-property of the parsed namespace, if the namespace was parsed. * Otherwise the member will be added to the top level declaration. * @returns Map with the top level declarations and resolved memberships. The key is the * long name of the item, the value is the @see {dom.TopLevelDeclaration} */ resolveMembershipAndExtends(): Map<string, dom.TopLevelDeclaration>; /** * Creates the comment for the jsdoc item * @param comment The complete comment text of the item * @param addExample Indicates if examples should be omitted or not */ private cleanJSDocComment; /** * Creates parameters for functions, constructors etc. * @todo This function needs to be refactored. * @param params * @param functionName */ private createDomParams; /** * Uses the configured version comparator to check if the passed since tag is in range of the * configured latest since tag. */ private evaluateSinceTag; private findClassParent; /** * Tries to find the parent item of the passed jsdoc item * @param parentItemLongname Long name of the searched item * @param domTopLevelDeclarations Source items to search in */ private findParentItem; private getAllClasses; /** * Determines the return value of a function declaration * @param jsdocItem */ private getFunctionReturnValue; /** * Sets the correct export flags to the declaration base. */ private handleFlags; /** * Handler for template-functions. */ private handleTags; private mapTypesToUnion; private mapVariableType; private mapVariableTypeString; private parseClass; private parseConstant; private parseEnum; private parseFunction; private parseInterface; /** * Converts a JSDoc item to a declaration base which can be printed * with the dts-dom module in a declaration file. * * The resulting item can be passed to dts-dom.emit which will then * output the item in the declaration file. * @param item JSDoc item from the taffy DB */ private parseJSDocItem; private parseMember; private parseModule; private parseNamespace; private parseTypeDefinition; private parseTypeDefinitionAsFunction; private parseTypeDefinitionAsObject; private parseTypeDefinitionAsType; /** * Adds the class item to the class * @param classMember The parsed class member item * @param parsedClass The parsed class item */ private resolveClassMembership; /** * Adds the enum item to the parent enum * @param enumMember The parsed enum member item * @param parsedEnum The parsed enum item */ private resolveEnumMembership; /** * Adds the interface member to it's parent interface * @param interfaceMember The parsed interface member * @param parsedInterface The parsed interface */ private resolveInterfaceMembership; /** * Adds the member item to it's parent module * @param moduleMember The parsed module member item * @param parsedModule The parsed module item */ private resolveModuleMembership; /** * Adds the namespace member item to it's parent namespace * @param namespaceMember The parsed namespace member * @param parsedNamespace The parsed namespace item */ private resolveNamespaceMembership; /** * Transforms a method declaration to a function declaration. * @param functionDeclaration */ private transformFunctionDeclarationToMethod; /** * Transforms a property declaration to a variable declaration. * @param propertyDeclaration */ private transformPropertyDeclarationToVariable; }