@workday/canvas-kit-docs
Version:
Documentation components of Canvas Kit components
117 lines • 5.62 kB
TypeScript
import ts from 'typescript';
import { JSDoc, ExportedSymbol, Value, TypeParameter, UnknownValue, ObjectProperty, FunctionParameter } from './docTypes';
export declare class DocParser<T extends {
kind: string;
} = any> {
program: ts.Program;
plugins: ParserPlugin<T>[];
/** A shared reference to the Typescript type checker */
checker: ts.TypeChecker;
visitedTypeScriptSymbols: Record<string, ts.SyntaxKind>;
/**
* This is the shared mutable instance of all exported symbols already processed. You can push new
* symbols or search for existing symbols. If your plugin doesn't need to access existing symbols,
* you can ignore this parameter.
*/
symbols: ExportedSymbol<Value | T>[];
constructor(program: ts.Program, plugins?: ParserPlugin<T>[]);
/**
* Get all {@link ExportedSymbol}s from a file.
*/
getExportedSymbols(fileName: string): ExportedSymbol<T | Value>[];
getValueFromNode(node: ts.Node): Value | T;
getValueFromType(type: ts.Type, node?: ts.Node): Value | T | undefined;
}
export declare const defaultJSDoc: JSDoc;
export declare function getFullJsDocComment(checker: ts.TypeChecker, symbol: ts.Symbol): JSDoc;
export declare function findDocComment(checker: ts.TypeChecker, symbol?: ts.Symbol): JSDoc;
/**
* Attempt to get the name of a declaration or expression
*/
export declare function getNodeName(node: ts.Expression | ts.Declaration): string | undefined;
export declare function filterObjectProperties(value: any): value is ObjectProperty;
export declare function filterObjectTypeParameters(value: any): value is TypeParameter;
export declare function filterFunctionParameters(value: any): value is FunctionParameter;
export declare function getSymbolFromNode(parser: DocParser, node: ts.Node): ts.Symbol | undefined;
export declare function isObject(type: ts.Type): type is ts.ObjectType;
/**
* Given a node, extract a default value and only return a `Value` if we consider it to be
* a valid default. For example, a literal is valid.
*
* Valid:
* - `'medium'`
* - `true`
* - 10
*
* Invalid:
* - `string`
* - `{foo: string}`
*/
export declare function getValidDefaultFromNode(parser: DocParser, node: ts.Node): Value | undefined;
/**
* A parameter might represent a `ObjectBindingPattern` which can be used to set defaults. This will
* return all defaults found within the `ObjectBindingPattern` and return them as a map of the
* property name to the `Value`. These defaults can be used to piece together a default. Also
* `getDefaultFromTags` can be used to get defaults from JSDoc tags.
*/
export declare function getDefaultsFromObjectBindingParameter(parser: DocParser, node: ts.ParameterDeclaration): Record<string, Value>;
/**
*
* @param checker The shared Typescript checker
* @param type The type we're trying to find a Value for
* @param node An optional node that was used to generate the Type. It should be used for all type
* nodes (AST nodes that are types and not JS values). This extra information can prevent errors and
* infinite loops.
*/
export declare function getValueFromType(parser: DocParser, type: ts.Type, node?: ts.Node): Value | undefined;
export declare function unknownValue(nodeText: string): UnknownValue;
export declare function isExportedSymbol(parser: DocParser, node: ts.Node): boolean;
export declare function getValueDeclaration(symbol?: ts.Symbol): ts.Declaration | undefined;
export declare function getValueFromSignatureNode(parser: DocParser, declaration: ts.SignatureDeclaration): Value;
/**
* Get defaults from JSDoc tags if available and do some simple processing to extract useful type
* information. JSDoc tags are not type checked, so our processing is limited.
*/
export declare function getDefaultFromTags(tags: ts.JSDocTagInfo[]): Value | undefined;
/**
* A parser plugin will first receive a node. If the plugin knows how to handle the node, it should
* return an array of exported symbols to add to docs. If it does not know how to handle the node,
* it should return `undefined` to allow other plugins (or the general parser) to process the node.
*/
export type ParserPlugin<TAdditionalValues extends {
kind: string;
} = Value> = (
/** The node currently being processed */
node: ts.Node,
/**
* The parser instance. The parser gives access to symbols, the checker, etc. For example, the
* shared `symbols` property of the parser is the collection of symbols already processed. You can
* push new symbols or search for existing symbols. If your plugin doesn't need to access existing
* symbols, you can ignore this parameter.
*/
parser: DocParser<TAdditionalValues>) => Value | TAdditionalValues | undefined | null;
/**
* This factory function makes it easy to create plugins by providing Typescript types
*
* @example
* ```ts
* import ts from 'typescript'
*
* const myPlugin = createParserPlugin((node, parser) => {
* // run tests on a node
* if (ts.isVariableDeclaration(node))
* }
* ```
*/
export declare function createParserPlugin<TAdditionalValues extends {
kind: string;
} = Value>(fn: ParserPlugin<TAdditionalValues>): ParserPlugin<TAdditionalValues>;
/**
* This small function makes it easier to write tests, but generally shouldn't be used. It creates a
* new parser per file. Instead use the parser directly and call `parser.getExportedSymbols` for
* each file to share memory.
*/
export declare function parse<T extends {
kind: string;
} = Value>(program: ts.Program, fileName: string, plugins?: ParserPlugin<T>[]): ExportedSymbol<Value | T>[];
//# sourceMappingURL=docParser.d.ts.map