This is a TypeScript exported class, function, type and documentation parser. It outputs everything needed to automatically generate documentation and better understand a project's public API. Information is extracted using TypeScript's Compiler API.
import * as readts from 'readts';
var parser = new readts.Parser();
// Read configuration used in the project we want to analyze.
var config = parser.parseConfig('tsconfig.json');
// Modify configuration as needed, for example to avoid writing compiler output to disk.
config.options.noEmit = true;
// Parse the project.
var tree = parser.parse(config);
The output is a list of ModuleSpec
objects, each with exported classes and interfaces (ClassSpec
),
functions (FunctionSpec
) and variables (IdentifierSpec
).
Types of variables and function parameters are parsed to TypeSpec
objects,
pointing to correct ClassSpec
objects if applicable.
This allows cross-linking types to their definitions in generated documentation.
See charto/docts for a documentation generator using this parser.
Docs generated using docts
Class
ClassSpec
Class or interface and its members.
Source code:<>
Properties:
.name
string
Class name.
.posSourcePos
.construct
FunctionSpec
Constructor function.
.methodListFunctionSpec[]
Public methods.
.propertyListIdentifierSpec[]
Public properties.
.docstring
JSDoc comment.Interface
FormatHooks
Hooks to change how parts of type definitions are converted to strings.
Source code:<>
Properties:
.unknown?
(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
.ref?(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
.array?(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
.union?(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
.intersection?(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
.generic?(spec: TypeSpec, output?: string, hooks?: FormatHooks) => string
Class
FunctionSpec
Function or method with any number of overloaded signatures.
Source code:<>
Properties:
.name
string
Function name.
.signatureListSignatureSpec[]
List of signatures, one for each overload.Class
IdentifierSpec
Property, function / method parameter or variable.
Source code:<>
Properties:
.name
string
Identifier name.
.posSourcePos
.typeTypeSpec
Type definition.
.optionalboolean
Interface members and function / method parameters may be optional.
.docstring
JSDoc comment.Class
ModuleSpec
Module or source file.
Source code:<>
Methods:
.isEmpty( ) ⇒
boolean
<>
Test if nothing is exported.Properties:
.classList
ClassSpec[]
Definitions of exported classes.
.interfaceListClassSpec[]
Definitions of exported interfaces.
.functionListFunctionSpec[]
Definitions of exported functions.
.variableListIdentifierSpec[]
Definitions of exported variables.Class
Parser
Main parser class with public methods, also holding its internal state.
Source code:<>
Methods:
.parseConfig( ) ⇒
ParsedCommandLine
<>
Parse a tsconfig.json file using TypeScript services API.
▪ tsconfigPathstring
.parse( ) ⇒ModuleSpec[]
<>
Parse a TypeScript project using TypeScript services API and configuration.
▪ configParsedCommandLine
▫ nameFilter?(pathName: string) => boolean
▫ extension?string
Interface
RefSpec
Source code:
<>
Properties:
.name?
string
.symbol?Symbol
.class?ClassSpec
Class
SignatureSpec
Function or method signature defining input and output types.
Source code:<>
Properties:
.pos
SourcePos
.paramListIdentifierSpec[]
List of parameters.
.returnTypeTypeSpec
Return type definition.
.docstring
JSDoc comment.Interface
SourcePos
Source code:
<>
Properties:
.sourcePath
string
.firstLinenumber
.lastLinenumber
Class
TypeSpec
Type definition.
Source code:<>
Methods:
.format( ) ⇒
string
<>
Convert to string, with optional hooks replacing default formatting code.
▫ hooks?FormatHooks
▫ needParens?boolean
Properties:
.name
string
Name of the type, only present if not composed of other type or class etc.
.refRefSpec
Definition of what the type points to, if available.
.unionOfTypeSpec[]
If the type is a union, list of the possible types.
.intersectionOfTypeSpec[]
If the type is an intersection, list of the possible types.
.arrayOfTypeSpec
If the type is an array, its element type.
.argumentListTypeSpec[]
Arguments of a generic type.
Copyright (c) 2016 BusFaster Ltd