-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathparse.js
676 lines (633 loc) · 16.4 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import doctrine from 'doctrine-temporary-fork';
import parseMarkdown from './remark-parse.js';
/**
* Flatteners: these methods simplify the structure of JSDoc comments
* into a flat object structure, parsing markdown and extracting
* information where appropriate.
* @private
*/
const flatteners = {
abstract: flattenBoolean,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
access(result, tag) {
// doctrine ensures that tag.access is valid
result.access = tag.access;
},
alias: flattenName,
arg: synonym('param'),
argument: synonym('param'),
async: flattenBoolean,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
augments(result, tag) {
// Google variation of augments/extends tag:
// uses type with brackets instead of name.
// https://github.com/google/closure-library/issues/746
if (!tag.name && tag.type && tag.type.name) {
tag.name = tag.type.name;
}
if (!tag.name) {
console.error('@extends from complex types is not supported yet'); // eslint-disable-line no-console
return;
}
result.augments.push(tag);
},
author: flattenDescription,
borrows: todo,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
callback(result, tag) {
result.kind = 'typedef';
if (tag.description) {
result.name = tag.description;
}
result.type = {
type: 'NameExpression',
name: 'Function'
};
},
class: flattenKindShorthand,
classdesc: flattenMarkdownDescription,
const: synonym('constant'),
constant: flattenKindShorthand,
constructor: synonym('class'),
constructs: todo,
copyright: flattenMarkdownDescription,
default: todo,
defaultvalue: synonym('default'),
deprecated(result, tag) {
const description = tag.description || 'This is deprecated.';
result.deprecated = parseMarkdown(description);
},
flattenMarkdownDescription,
desc: synonym('description'),
description: flattenMarkdownDescription,
emits: synonym('fires'),
enum(result, tag) {
result.kind = 'enum';
result.type = tag.type;
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
event(result, tag) {
result.kind = 'event';
if (tag.description) {
result.name = tag.description;
}
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
example(result, tag) {
if (!tag.description) {
result.errors.push({
message: '@example without code',
commentLineNumber: tag.lineNumber
});
return;
}
const example = {
description: tag.description
};
if (tag.caption) {
example.caption = parseMarkdown(tag.caption);
}
result.examples.push(example);
},
exception: synonym('throws'),
exports: todo,
extends: synonym('augments'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
external(result, tag) {
result.kind = 'external';
if (tag.description) {
result.name = tag.description;
}
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
file(result, tag) {
result.kind = 'file';
if (tag.description) {
result.description = parseMarkdown(tag.description);
}
},
fileoverview: synonym('file'),
fires: todo,
func: synonym('function'),
function: flattenKindShorthand,
generator: flattenBoolean,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
global(result) {
result.scope = 'global';
},
hideconstructor: flattenBoolean,
host: synonym('external'),
ignore: flattenBoolean,
implements(result, tag) {
// Match @extends/@augments above.
if (!tag.name && tag.type && tag.type.name) {
tag.name = tag.type.name;
}
result.implements.push(tag);
},
inheritdoc: todo,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
inner(result) {
result.scope = 'inner';
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
instance(result) {
result.scope = 'instance';
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
interface(result, tag) {
result.kind = 'interface';
if (tag.description) {
result.name = tag.description;
}
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
kind(result, tag) {
// doctrine ensures that tag.kind is valid
result.kind = tag.kind;
},
lends: flattenDescription,
license: flattenDescription,
listens: todo,
member: flattenKindShorthand,
memberof: flattenDescription,
method: synonym('function'),
mixes: todo,
mixin: flattenKindShorthand,
module: flattenKindShorthand,
name: flattenName,
namespace: flattenKindShorthand,
override: flattenBoolean,
overview: synonym('file'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
param(result, tag) {
const param = {
title: 'param',
name: tag.name,
lineNumber: tag.lineNumber // TODO: remove
};
if (tag.description) {
param.description = parseMarkdown(tag.description);
}
if (tag.type) {
param.type = tag.type;
}
if (tag.default) {
param.default = tag.default;
if (param.type && param.type.type === 'OptionalType') {
param.type = param.type.expression;
}
}
result.params.push(param);
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
private(result) {
result.access = 'private';
},
prop: synonym('property'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
property(result, tag) {
const property = {
title: 'property',
name: tag.name,
lineNumber: tag.lineNumber // TODO: remove
};
if (tag.description) {
property.description = parseMarkdown(tag.description);
}
if (tag.type) {
property.type = tag.type;
}
result.properties.push(property);
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
protected(result) {
result.access = 'protected';
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
public(result) {
result.access = 'public';
},
readonly: flattenBoolean,
requires: todo,
return: synonym('returns'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
returns(result, tag) {
const returns = {
description: parseMarkdown(tag.description),
title: 'returns'
};
if (tag.type) {
returns.type = tag.type;
}
result.returns.push(returns);
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
see(result, tag) {
const sees = {
description: parseMarkdown(tag.description),
title: 'sees'
};
if (tag.type) {
sees.type = tag.type;
}
result.sees.push(sees);
},
since: flattenDescription,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @returns {undefined} has side-effects
*/
static(result) {
result.scope = 'static';
},
summary: flattenMarkdownDescription,
this: todo,
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
throws(result, tag) {
const throws = {};
if (tag.description) {
throws.description = parseMarkdown(tag.description);
}
if (tag.type) {
throws.type = tag.type;
}
result.throws.push(throws);
},
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
todo(result, tag) {
result.todos.push(parseMarkdown(tag.description));
},
tutorial: todo,
type(result, tag) {
result.type = tag.type;
},
typedef: flattenKindShorthand,
var: synonym('member'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
variation(result, tag) {
result.variation = tag.variation;
},
version: flattenDescription,
virtual: synonym('abstract'),
yield: synonym('yields'),
/**
* Parse tag
* @private
* @param {Object} result target comment
* @param {Object} tag the tag
* @returns {undefined} has side-effects
*/
yields(result, tag) {
const yields = {
description: parseMarkdown(tag.description),
title: 'yields'
};
if (tag.type) {
yields.type = tag.type;
}
result.yields.push(yields);
}
};
/**
* A no-op function for unsupported tags
* @returns {undefined} does nothing
*/
function todo() {}
/**
* Generate a function that curries a destination key for a flattener
* @private
* @param {string} key the eventual destination key
* @returns {Function} a flattener that remembers that key
*/
function synonym(key) {
return function (result, tag) {
const fun = flatteners[key];
fun.apply(null, [result, tag, key].slice(0, fun.length));
};
}
/**
* Treat the existence of a tag as a sign to mark `key` as true in the result
* @private
* @param {Object} result the documentation object
* @param {Object} tag the tag object, with a name property
* @param {string} key destination on the result
* @returns {undefined} operates with side-effects
*/
function flattenBoolean(result, tag, key) {
result[key] = true;
}
/**
* Flatten a usable-once name tag into a key
* @private
* @param {Object} result the documentation object
* @param {Object} tag the tag object, with a name property
* @param {string} key destination on the result
* @returns {undefined} operates with side-effects
*/
function flattenName(result, tag, key) {
result[key] = tag.name;
}
/**
* Flatten a usable-once description tag into a key
* @private
* @param {Object} result the documentation object
* @param {Object} tag the tag object, with a description property
* @param {string} key destination on the result
* @returns {undefined} operates with side-effects
*/
function flattenDescription(result, tag, key) {
result[key] = tag.description;
}
/**
* Flatten a usable-once description tag into a key and parse it as Markdown
* @private
* @param {Object} result the documentation object
* @param {Object} tag the tag object, with a description property
* @param {string} key destination on the result
* @returns {undefined} operates with side-effects
*/
function flattenMarkdownDescription(result, tag, key) {
result[key] = parseMarkdown(tag.description);
}
/**
* Parse [kind shorthand](http://usejsdoc.org/tags-kind.html) into
* both name and type tags, like `@class [<type> <name>]`
*
* @param {Object} result comment
* @param {Object} tag parsed tag
* @param {string} key tag
* @returns {undefined} operates through side effects
* @private
*/
function flattenKindShorthand(result, tag, key) {
result.kind = key;
if (tag.name) {
result.name = tag.name;
}
if (tag.type) {
result.type = tag.type;
}
}
/**
* Parse a comment with doctrine, decorate the result with file position and code
* context, handle parsing errors, and fix up various infelicities in the structure
* outputted by doctrine.
*
* The following tags are treated as synonyms for a canonical tag:
*
* * `@virtual` ⇢ `@abstract`
* * `@extends` ⇢ `@augments`
* * `@constructor` ⇢ `@class`
* * `@const` ⇢ `@constant`
* * `@defaultvalue` ⇢ `@default`
* * `@desc` ⇢ `@description`
* * `@host` ⇢ `@external`
* * `@fileoverview`, `@overview` ⇢ `@file`
* * `@emits` ⇢ `@fires`
* * `@func`, `@method` ⇢ `@function`
* * `@var` ⇢ `@member`
* * `@arg`, `@argument` ⇢ `@param`
* * `@prop` ⇢ `@property`
* * `@return` ⇢ `@returns`
* * `@exception` ⇢ `@throws`
* * `@linkcode`, `@linkplain` ⇢ `@link`
*
* The following tags are assumed to be singletons, and are flattened
* to a top-level property on the result whose value is extracted from
* the tag:
*
* * `@name`
* * `@memberof`
* * `@classdesc`
* * `@kind`
* * `@class`
* * `@constant`
* * `@event`
* * `@external`
* * `@file`
* * `@function`
* * `@member`
* * `@mixin`
* * `@module`
* * `@namespace`
* * `@typedef`
* * `@access`
* * `@lends`
* * `@description`
* * `@summary`
* * `@copyright`
* * `@deprecated`
*
* The following tags are flattened to a top-level array-valued property:
*
* * `@param` (to `params` property)
* * `@property` (to `properties` property)
* * `@returns` (to `returns` property)
* * `@augments` (to `augments` property)
* * `@example` (to `examples` property)
* * `@throws` (to `throws` property)
* * `@see` (to `sees` property)
* * `@todo` (to `todos` property)
*
* The `@global`, `@static`, `@instance`, and `@inner` tags are flattened
* to a `scope` property whose value is `"global"`, `"static"`, `"instance"`,
* or `"inner"`.
*
* The `@access`, `@public`, `@protected`, and `@private` tags are flattened
* to an `access` property whose value is `"protected"` or `"private"`.
* The assumed default value is `"public"`, so `@access public` or `@public`
* tags result in no `access` property.
*
* @param {string} comment input to be parsed
* @param {Object} loc location of the input
* @param {Object} context code context of the input
* @returns {Comment} an object conforming to the
* [documentation schema](https://github.com/documentationjs/api-json)
*/
export default function parseJSDoc(comment, loc, context) {
const result = doctrine.parse(comment, {
// have doctrine itself remove the comment asterisks from content
unwrap: true,
// enable parsing of optional parameters in brackets, JSDoc3 style
sloppy: true,
// `recoverable: true` is the only way to get error information out
recoverable: true,
// include line numbers
lineNumbers: true
});
result.loc = loc;
result.context = context;
result.augments = [];
result.errors = [];
result.examples = [];
result.implements = [];
result.params = [];
result.properties = [];
result.returns = [];
result.sees = [];
result.throws = [];
result.todos = [];
result.yields = [];
if (result.description) {
result.description = parseMarkdown(result.description);
}
// Reject parameter tags without a parameter name
result.tags.filter(function (tag) {
if (tag.title === 'param' && tag.name === undefined) {
result.errors.push({
message: 'A @param tag without a parameter name was rejected'
});
return false;
}
return true;
});
result.tags.forEach(function (tag) {
if (tag.errors) {
for (let j = 0; j < tag.errors.length; j++) {
result.errors.push({ message: tag.errors[j] });
}
} else if (flatteners[tag.title]) {
flatteners[tag.title](result, tag, tag.title);
} else {
result.errors.push({
message: 'unknown tag @' + tag.title,
commentLineNumber: tag.lineNumber
});
}
});
// Using the @name tag, or any other tag that sets the name of a comment,
// disconnects the comment from its surrounding code.
if (context && result.name) {
delete context.ast;
}
return result;
}