-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjsdocCommentToMember.mjs
611 lines (540 loc) · 18.8 KB
/
jsdocCommentToMember.mjs
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
import { parse } from "comment-parser";
import trimNewlines from "trim-newlines";
import CodeLocation from "./CodeLocation.mjs";
import CodePosition from "./CodePosition.mjs";
import COMMENT_PARSER_OPTIONS from "./COMMENT_PARSER_OPTIONS.mjs";
import deconstructJsdocNamepath from "./deconstructJsdocNamepath.mjs";
import getJsdocBlockDescriptionSource from "./getJsdocBlockDescriptionSource.mjs";
import getJsdocSourceTokenCodeLocation from "./getJsdocSourceTokenCodeLocation.mjs";
import InvalidJsdocError from "./InvalidJsdocError.mjs";
/**
* Analyzes a JSDoc comment to produce JSDoc member details.
* @kind function
* @name jsdocCommentToMember
* @param {object} jsdocComment JSDoc comment, from a Babel parse result.
* @param {CodeFilesMap} codeFiles Map of code file paths and their code.
* @param {string} codeFilePath File path for the code containing the JSDoc comment.
* @returns {JsdocMember|void} JSDoc member, if it’s valid and not ignored.
* @ignore
*/
export default function jsdocCommentToMember(
jsdocComment,
codeFiles,
codeFilePath
) {
if (typeof jsdocComment !== "object")
throw new TypeError("Argument 1 `jsdocComment` must be an object.");
if (!(codeFiles instanceof Map))
throw new TypeError("Argument 2 `codeFiles` must be a `Map` instance.");
if (typeof codeFilePath !== "string")
throw new TypeError("Argument 3 `codeFilePath` must be a string.");
if (codeFilePath === "")
throw new TypeError(
"Argument 3 `codeFilePath` must be a populated string."
);
const jsdocBlockStartCodePosition = new CodePosition(
jsdocComment.loc.start.line,
// Babel starts columns at 0, not 1.
jsdocComment.loc.start.column + 1
);
const [jsdocBlock] = parse(
// Restore the start `/*` and end `*/` that the Babel parse result excludes,
// so that the JSDoc comment parser can accept it.
`/*${jsdocComment.value}*/`,
{
...COMMENT_PARSER_OPTIONS,
startLine: jsdocBlockStartCodePosition.line,
}
);
// Ignore JSDoc without tags.
if (jsdocBlock && jsdocBlock.tags && jsdocBlock.tags.length) {
let kind;
let namepath;
let memberof;
let type;
let description;
const parameters = [];
const properties = [];
const returns = {};
const fires = [];
const see = [];
const examples = [];
// Scan tags for membership data, looping tags backwards as later tags
// override earlier ones.
for (let index = jsdocBlock.tags.length - 1; index >= 0; index--) {
const tag = jsdocBlock.tags[index];
switch (tag.tag) {
case "kind":
if (
!kind &&
// Ignore an invalid tag missing a name.
tag.name
)
kind = tag.name;
break;
case "name":
if (
!namepath &&
// Ignore an invalid tag missing a name.
tag.name
)
namepath = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.name,
};
break;
case "typedef":
// Ignore an invalid tag missing a name.
if (tag.name) {
if (!kind) kind = "typedef";
if (!namepath)
namepath = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.name,
};
if (!type && tag.type)
type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"type",
jsdocBlockStartCodePosition
),
},
data: tag.type,
};
}
break;
case "callback":
// A callback tag is a typedef with an implied function type.
// Ignore an invalid tag missing a name.
if (tag.name) {
if (!kind) kind = "typedef";
if (!namepath)
namepath = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.name,
};
if (!type)
type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"tag",
jsdocBlockStartCodePosition
),
},
// A special case; the tag implies this type so this data is not
// what is actually at the associated code file location.
data: "Function",
};
}
break;
case "type":
if (
!type &&
// Ignore an invalid tag missing a type.
tag.type
)
type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"type",
jsdocBlockStartCodePosition
),
},
data: tag.type,
};
break;
case "desc":
case "description": {
if (!description) {
const tagDescriptionTrimmed = trimNewlines(tag.description);
// Ignore an invalid tag missing a description.
if (tagDescriptionTrimmed)
description = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
),
},
data: tagDescriptionTrimmed,
};
}
break;
}
case "arg":
case "argument":
case "param": {
// Ignore an invalid tag missing a name.
if (tag.name) {
// Define the JSDoc parameter with nicely ordered properties.
const parameter = {
name: tag.name,
};
if (tag.type)
parameter.type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"type",
jsdocBlockStartCodePosition
),
},
data: tag.type,
};
parameter.optional = tag.optional;
if (tag.default)
parameter.default = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.default,
};
const tagDescriptionTrimmed = trimNewlines(tag.description);
if (tagDescriptionTrimmed)
parameter.description = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
),
},
data: tagDescriptionTrimmed,
};
parameters.unshift(parameter);
}
break;
}
case "prop":
case "property": {
// Ignore an invalid tag missing a name.
if (tag.name) {
// Define the JSDoc property with nicely ordered properties.
const property = {
name: tag.name,
};
if (tag.type)
property.type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"type",
jsdocBlockStartCodePosition
),
},
data: tag.type,
};
property.optional = tag.optional;
if (tag.default)
property.default = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.default,
};
const tagDescriptionTrimmed = trimNewlines(tag.description);
if (tagDescriptionTrimmed)
property.description = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
),
},
data: tagDescriptionTrimmed,
};
properties.unshift(property);
}
break;
}
case "return":
case "returns": {
// Ignore an invalid tag missing both a type and description.
if (!returns.type && tag.type)
returns.type = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"type",
jsdocBlockStartCodePosition
),
},
data: tag.type,
};
if (!returns.description) {
const tagDescriptionTrimmed = trimNewlines(tag.description);
if (tagDescriptionTrimmed)
returns.description = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
),
},
data: tagDescriptionTrimmed,
};
}
break;
}
case "emits":
case "fires": {
if (
// Ignore an invalid tag missing a name.
tag.name &&
// Ignore a duplicate name.
!fires.some(({ data }) => data === tag.name)
)
fires.unshift({
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"name",
jsdocBlockStartCodePosition
),
},
data: tag.name,
});
break;
}
case "see": {
const tagDescriptionTrimmed = trimNewlines(tag.description);
// Ignore an invalid tag missing a description.
if (tagDescriptionTrimmed)
see.unshift({
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
),
},
data: tagDescriptionTrimmed,
});
break;
}
case "example": {
const tagDescriptionTrimmed = trimNewlines(tag.description);
// Ignore an invalid tag missing a description.
if (tagDescriptionTrimmed) {
const {
groups: {
beforeCaption,
captionData,
beforeContent,
contentData,
},
} = tagDescriptionTrimmed.match(
// Group what comes before the caption and content so their
// lengths can be used to derive offsets and then code locations
// for both.
/^(?<beforeContent>(?:(?<beforeCaption>\s*<caption>)(?<captionData>[^]*?)<\/caption>(?:\n+|[ \t])?)?)(?<contentData>[^]+)?/u
);
if (captionData || contentData) {
let { line, column } = getJsdocSourceTokenCodeLocation(
tag.source,
"description",
jsdocBlockStartCodePosition
).start;
let charIndex = 0;
const nextChar = () => {
if (tagDescriptionTrimmed[charIndex] === "\n") {
line++;
// Because the tag description characters being looped have
// the JSDoc comment fence stripped by `comment-parser`, the
// line column start must account for the length of the
// possible fence (e.g. ` * `).
const { start, delimiter, postDelimiter } =
jsdocBlock.source.find(
({ number }) => number === line
).tokens;
column =
start.length + delimiter.length + postDelimiter.length + 1;
} else column++;
charIndex++;
};
const example = {};
if (captionData) {
const captionOffsetStart = beforeCaption.length;
const captionOffsetEnd =
captionOffsetStart + captionData.length - 1;
while (charIndex < captionOffsetStart) nextChar();
const captionStart = new CodePosition(line, column);
while (charIndex < captionOffsetEnd) nextChar();
example.caption = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: new CodeLocation(
captionStart,
new CodePosition(line, column)
),
},
data: captionData,
};
}
if (contentData) {
const contentOffsetStart = beforeContent.length;
const contentOffsetEnd =
contentOffsetStart + contentData.length - 1;
while (charIndex < contentOffsetStart) nextChar();
const contentStart = new CodePosition(line, column);
while (charIndex < contentOffsetEnd) nextChar();
example.content = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: new CodeLocation(
contentStart,
new CodePosition(line, column)
),
},
data: contentData,
};
}
examples.unshift(example);
}
}
break;
}
case "ignore":
// Ignore JSDoc with an ignore tag. It’s best for this tag to be used
// last in a JSDoc comment, so processing can be bailed earlier.
return;
}
}
// Ignore JSDoc without a kind or namepath.
if (kind && namepath) {
try {
var {
memberof: memberofNamepath,
membership,
name,
} = deconstructJsdocNamepath(namepath.data);
} catch (error) {
throw new InvalidJsdocError(
error.message,
namepath.codeFileLocation,
codeFiles.get(codeFilePath)
);
}
if (memberofNamepath)
memberof = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: new CodeLocation(
namepath.codeFileLocation.codeLocation.start,
new CodePosition(
namepath.codeFileLocation.codeLocation.end.line,
namepath.codeFileLocation.codeLocation.end.column -
membership.length -
name.length
)
),
},
data: memberofNamepath,
};
// Automatically add a missing `event:` prefix to an event name.
if (kind === "event" && !name.startsWith("event:"))
name = `event:${name}`;
// Define the JSDoc member with nicely ordered properties, and only add
// properties that contain details.
const member = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: new CodeLocation(
jsdocBlockStartCodePosition,
new CodePosition(
jsdocComment.loc.end.line,
// Although Babel starts columns at 0, not 1, it considers the end
// character the one after the actual end.
jsdocComment.loc.end.column
)
),
},
};
member.namepath = namepath;
if (memberof) member.memberof = memberof;
if (membership) member.membership = membership;
member.name = name;
member.kind = kind;
if (type) member.type = type;
// The description is special as it can be specified without a tag.
// Description tags override a JSDoc block description as they come later.
if (description) member.description = description;
else {
// Description was not populated from tags, so try to get it from the
// JSDoc block.
const jsdocBlockDescriptionTrimmed = trimNewlines(
jsdocBlock.description
);
if (jsdocBlockDescriptionTrimmed)
member.description = {
codeFileLocation: {
filePath: codeFilePath,
codeLocation: getJsdocSourceTokenCodeLocation(
getJsdocBlockDescriptionSource(jsdocBlock),
"description",
jsdocBlockStartCodePosition
),
},
data: jsdocBlockDescriptionTrimmed,
};
}
if (parameters.length) member.parameters = parameters;
if (properties.length) member.properties = properties;
if (Object.keys(returns).length) member.returns = returns;
if (fires.length) member.fires = fires;
if (see.length) member.see = see;
if (examples.length) member.examples = examples;
return member;
}
}
}