UNPKG

5.74 kBTypeScriptView Raw
1// Type definitions for doctrine the JSDoc parser
2// Project: https://github.com/eslint/doctrine
3// Definitions by: rictic <https://github.com/rictic>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/**
7 * Doctrine is a JSDoc parser that parses documentation comments from JavaScript
8 * (you need to pass in the comment, not a whole JavaScript file).
9 */
10
11/**
12 * Parse the given content as a jsdoc comment.
13 */
14export function parse(content: string, options?: Options): Annotation;
15/**
16 * Remove /*, *, and * / from jsdoc.
17 */
18export function unwrapComment(doc: string): string;
19
20interface Options {
21 /**
22 * Set to `true` to delete the leading `/**`, any `*` that begins a line,
23 * and the trailing `* /` from the source text. Default: `false`.
24 */
25 unwrap?: boolean;
26 /**
27 * An array of tags to return. When specified, Doctrine returns
28 * only tags in this array. For example, if `tags` is `["param"]`, then only
29 * `@param` tags will be returned. Default: `null`.
30 */
31 tags?: string[];
32 /**
33 * set to `true` to keep parsing even when syntax errors occur. Default:
34 * `false`.
35 */
36 recoverable?: boolean;
37 /**
38 * Set to `true` to allow optional parameters to be specified in brackets
39 * (`@param {string} [foo]`). Default: `false`.
40 */
41 sloppy?: boolean;
42 /**
43 * Set to `true` to throw an error when syntax errors occur. If false then
44 * errors will be added to `tag.errors` instead.
45 */
46 strict?: boolean;
47 /**
48 * Set to `true` to preserve leading and trailing whitespace when extracting
49 * comment text.
50 */
51 preserveWhitespace?: boolean;
52 /**
53 * Set to `true` to add `lineNumber` to each node, specifying the line on
54 * which the node is found in the source. Default: `false`.
55 */
56 lineNumbers?: boolean;
57}
58
59/**
60 * Represents a parsed jsdoc comment.
61 */
62interface Annotation {
63 /** The overall description of the thing being documented. */
64 description: string;
65 tags: Tag[];
66}
67
68/**
69 * Represents a single jsdoc tag.
70 *
71 * So for example:
72 * `@ param {{ok:String}} userName`
73 * (ignore the space after the @)
74 *
75 * Would be represented as:
76 *
77 * {title: 'param', name: 'userName',
78 * type: {type: 'RecordType", fields: [
79 * {type: 'FieldType',
80 * key: 'ok',
81 * value: {type: 'NameExpression', name: 'String'}}]}}
82 *
83 */
84export interface Tag {
85 /** The title of the jsdoc tag. e.g. `@foo` will have a title of 'foo'. */
86 title: string;
87 /** The name of the thing this tag is documenting, if any. */
88 name?: string;
89 /** The description of the thing this tag is documenting. */
90 description: string|null;
91 /** The type of the thing this tag is documenting. */
92 type?: Type|null;
93 kind?: string;
94 /** Any errors that were encountered in parsing the tag. */
95 errors?: string[];
96}
97
98export type Type =
99 (type.AllLiteral | type.ArrayType | type.FieldType | type.FunctionType |
100 type.NameExpression | type.NonNullableType | type.NullableLiteral |
101 type.NullableType | type.NullLiteral | type.OptionalType |
102 type.ParameterType | type.RecordType | type.RestType |
103 type.TypeApplication | type.UndefinedLiteral | type.UnionType |
104 type.VoidLiteral);
105
106export module type {
107 export interface AllLiteral { type: 'AllLiteral' }
108 export interface ArrayType { type: 'ArrayType', elements: Type[] }
109 export interface FieldType { type: 'FieldType', key: string, value?: Type }
110 export interface FunctionType {
111 type: 'FunctionType';
112 'this': Type;
113 'new': Type, params: Type[];
114 result: Type[]
115 }
116 export interface NameExpression { type: 'NameExpression', name: string }
117 export interface NonNullableType {
118 type: 'NonNullableType', prefix: boolean, expression: Type
119 }
120 export interface NullableLiteral { type: 'NullableLiteral' }
121 export interface NullableType {
122 type: 'NullableType', prefix: boolean, expression: Type
123 }
124 export interface NullLiteral { type: 'NullLiteral' }
125 export interface OptionalType { type: 'OptionalType', expression: Type }
126 export interface ParameterType {
127 type: 'ParameterType', name: string, expression: Type
128 }
129 export interface RecordType { type: 'RecordType', fields: Type[] }
130 export interface RestType {
131 type: 'RestType';
132 expression?: Type;
133 }
134 export interface TypeApplication {
135 type: 'TypeApplication', expression: Type, applications: Type[]
136 }
137 export interface UndefinedLiteral { type: 'UndefinedLiteral' }
138 export interface UnionType { type: 'UnionType', elements: Type[] }
139 export interface VoidLiteral { type: 'VoidLiteral' }
140
141 export function stringify(type: Type): string;
142 export function parseType(src: string, options?: {midstream: boolean}): Type;
143 export function parseParamType(
144 src: string, options?: {midstream: boolean}): Type;
145
146 export const Syntax: {
147 NullableLiteral: 'NullableLiteral',
148 AllLiteral: 'AllLiteral',
149 NullLiteral: 'NullLiteral',
150 UndefinedLiteral: 'UndefinedLiteral',
151 VoidLiteral: 'VoidLiteral',
152 UnionType: 'UnionType',
153 ArrayType: 'ArrayType',
154 RecordType: 'RecordType',
155 FieldType: 'FieldType',
156 FunctionType: 'FunctionType',
157 ParameterType: 'ParameterType',
158 RestType: 'RestType',
159 NonNullableType: 'NonNullableType',
160 OptionalType: 'OptionalType',
161 NullableType: 'NullableType',
162 NameExpression: 'NameExpression',
163 TypeApplication: 'TypeApplication'
164 }
165}
166
167export const version: string;
168export const parseType: typeof type.parseType;
169export const parseParamType: typeof type.parseParamType;
170export const Syntax: typeof type.Syntax;