Home

Awesome

readts

build status npm version

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.

Usage

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.

API

Docs generated using docts

<a name="api-ClassSpec"></a>

Class ClassSpec

<em>Class or interface and its members.</em>
Source code: <>

Methods:

new( ) <sup><code>ClassSpec</code></sup> <>
 ▪ spec <sup><code>SymbolSpec</code></sup>

Properties:

.name <sup><code>string</code></sup>
<em>Class name.</em>
.pos <sup><code>SourcePos</code></sup>

.construct <sup><code>FunctionSpec</code></sup>
<em>Constructor function.</em>
.index <sup><code>IndexSpec</code></sup>
<em>Index signature.</em>
.methodList <sup><code>FunctionSpec[]</code></sup>
<em>Public methods.</em>
.propertyList <sup><code>IdentifierSpec[]</code></sup>
<em>Public properties.</em>
.extendList <sup><code>ClassSpec[]</code></sup>
<em>Class extends</em>
.exports <sup><code>ModuleSpec</code></sup>
<em>Class exports</em>
.doc <sup><code>string</code></sup>
<em>JSDoc comment.</em>

<a name="api-EnumSpec"></a>

Class EnumSpec

<em>Enum and its members.</em>
Source code: <>

Methods:

new( ) <sup><code>EnumSpec</code></sup> <>
 ▪ spec <sup><code>SymbolSpec</code></sup>
.addMember( ) <sup><code>void</code></sup> <>
 ▪ spec <sup><code>IdentifierSpec</code></sup>

Properties:

.name <sup><code>string</code></sup>
<em>Class name.</em>
.pos <sup><code>SourcePos</code></sup>

.memberList <sup><code>IdentifierSpec[]</code></sup>
<em>Public properties.</em>
.doc <sup><code>string</code></sup>
<em>JSDoc comment.</em>

<a name="api-FormatHooks"></a>

Interface FormatHooks

<em>Hooks to change how parts of type definitions are converted to strings.</em>
Source code: <>

Properties:

.unknown<sub>?</sub> <sup><code>FormatHook</code></sup>
.ref<sub>?</sub> <sup><code>FormatHook</code></sup>
.array<sub>?</sub> <sup><code>FormatHook</code></sup>
.union<sub>?</sub> <sup><code>FormatHook</code></sup>
.intersection<sub>?</sub> <sup><code>FormatHook</code></sup>
.generic<sub>?</sub> <sup><code>FormatHook</code></sup>

<a name="api-FunctionSpec"></a>

Class FunctionSpec

<em>Function or method with any number of overloaded signatures.</em>
Source code: <>

Methods:

new( ) <sup><code>FunctionSpec</code></sup> <>
 ▪ spec <sup><code>SymbolSpec</code></sup>

Properties:

.name <sup><code>string</code></sup>
<em>Function name.</em>
.signatureList <sup><code>SignatureSpec[]</code></sup>
<em>List of signatures, one for each overload.</em>

<a name="api-IdentifierSpec"></a>

Class IdentifierSpec

<em>Property, function / method parameter or variable.</em>
Source code: <>

Properties:

.name <sup><code>string</code></sup>
<em>Identifier name.</em>
.pos <sup><code>SourcePos</code></sup>
.type <sup><code>TypeSpec</code></sup>
<em>Type definition.</em>
.value<sub>?</sub> <sup><code>any</code></sup>
<em>Literal type value</em>
.optional <sup><code>boolean</code></sup>
<em>Interface members and function / method parameters may be optional.</em>
.doc <sup><code>string</code></sup>
<em>JSDoc comment.</em>

<a name="api-IndexSpec"></a>

Class IndexSpec

<em>Index signature.</em>
Source code: <>

Properties:

.signature <sup><code>TypeSpec</code></sup>
<em>Singature type.</em>
.value <sup><code>TypeSpec</code></sup>
<em>Value type</em>

<a name="api-ModuleSpec"></a>

Class ModuleSpec

<em>Module or source file.</em>
Source code: <>

Methods:

.isEmpty( ) <sup><code>boolean</code></sup> <>
<em>Test if nothing is exported.</em>

Properties:

.enumList <sup><code>EnumSpec[]</code></sup>
<em>Definitions of exported enums.</em>
.classList <sup><code>ClassSpec[]</code></sup>
<em>Definitions of exported classes.</em>
.interfaceList <sup><code>ClassSpec[]</code></sup>
<em>Definitions of exported interfaces.</em>
.functionList <sup><code>FunctionSpec[]</code></sup>
<em>Definitions of exported functions.</em>
.variableList <sup><code>IdentifierSpec[]</code></sup>
<em>Definitions of exported variables.</em>

<a name="api-Parser"></a>

Class Parser

<em>Main parser class with public methods, also holding its internal state.</em>
Source code: <>

Methods:

.parseConfig( ) <sup><code>ParsedCommandLine</code></sup> <>
<em>Parse a tsconfig.json file using TypeScript services API.</em>
 ▪ tsconfigPath <sup><code>string</code></sup>
.parse( ) <sup><code>ModuleSpec[]</code></sup> <>
<em>Parse a TypeScript project using TypeScript services API and configuration.</em>
 ▪ config <sup><code>ParsedCommandLine</code></sup>
 ▫ nameFilter<sub>?</sub> <sup><code>(pathName: string) => boolean</code></sup>
 ▫ extension<sub>?</sub> <sup><code>string</code></sup>

<a name="api-RefSpec"></a>

Interface RefSpec

Source code: <>

Properties:

.name<sub>?</sub> <sup><code>string</code></sup>
.symbol<sub>?</sub> <sup><code>Symbol</code></sup>
.class<sub>?</sub> <sup><code>ClassSpec</code></sup>
.enum<sub>?</sub> <sup><code>EnumSpec</code></sup>

<a name="api-SignatureSpec"></a>

Class SignatureSpec

<em>Function or method signature defining input and output types.</em>
Source code: <>

Methods:

new( ) <sup><code>SignatureSpec</code></sup> <>
 ▪ pos <sup><code>SourcePos</code></sup>
 ▪ returnType <sup><code>TypeSpec</code></sup>
 ▪ doc <sup><code>string</code></sup>

Properties:

.pos <sup><code>SourcePos</code></sup>
.paramList <sup><code>IdentifierSpec[]</code></sup>
<em>List of parameters.</em>
.returnType <sup><code>TypeSpec</code></sup>
<em>Return type definition.</em>
.doc <sup><code>string</code></sup>
<em>JSDoc comment.</em>

<a name="api-SourcePos"></a>

Interface SourcePos

Source code: <>

Properties:

.sourcePath <sup><code>string</code></sup>
.firstLine <sup><code>number</code></sup>
.lastLine <sup><code>number</code></sup>

<a name="api-TypeSpec"></a>

Class TypeSpec

<em>Type definition.</em>
Source code: <>

Methods:

.format( ) <sup><code>string</code></sup> <>
<em>Convert to string, with optional hooks replacing default formatting code.</em>
 ▫ hooks<sub>?</sub> <sup><code>FormatHooks</code></sup>
 ▫ needParens<sub>?</sub> <sup><code>boolean</code></sup>

Properties:

.name <sup><code>string</code></sup>
<em>Name of the type, only present if not composed of other type or class etc.</em>
.value<sub>?</sub> <sup><code>string | number</code></sup>
<em>Value of the type, only present if literal type</em>
.ref <sup><code>RefSpec</code></sup>
<em>Definition of what the type points to, if available.</em>
.unionOf <sup><code>TypeSpec[]</code></sup>
<em>If the type is a union, list of the possible types.</em>
.intersectionOf <sup><code>TypeSpec[]</code></sup>
<em>If the type is an intersection, list of the possible types.</em>
.arrayOf <sup><code>TypeSpec</code></sup>
<em>If the type is an array, its element type.</em>
.argumentList <sup><code>TypeSpec[]</code></sup>
<em>Arguments of a generic type.</em>

License

The MIT License

Copyright (c) 2016 BusFaster Ltd