UNPKG

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