-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
663 lines (582 loc) · 21.3 KB
/
index.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
require('string.prototype.matchall').shim();
const fs = require('fs');
const path = require('path');
const addInherited = require('jsdoc/augment').addInherited; // eslint-disable-line import/no-unresolved
const env = require('jsdoc/env'); // eslint-disable-line import/no-unresolved
const importRegEx =
/import\(["']([^"']*)["']\)(?:\.([^ \.\|\}><,\)=#\n]*))?([ \.\|\}><,\)=#\n])/g;
const typedefRegEx = /@typedef\s*(?:\{[^\}]*\}\s*)?([^\{\}\s]+)/g;
const noClassdescRegEx = /@(typedef|module|type)/;
const extensionReplaceRegEx = /\.m?js$/;
const extensionEnsureRegEx = /(\.js)?$/;
const slashRegEx = /\\/g;
const variableNameRegEx = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
const moduleInfos = {};
const fileNodes = {};
const resolvedPathCache = new Set();
/** @type {string} */
let implicitModuleRoot;
/**
* Without explicit module ids, JSDoc will use the nearest shared parent directory.
* @return {string} The implicit root path with which to resolve all module ids against.
*/
function getImplicitModuleRoot() {
if (implicitModuleRoot) {
return implicitModuleRoot;
}
if (!env.sourceFiles || env.sourceFiles.length === 0) {
return process.cwd();
}
// Find the nearest shared parent directory
implicitModuleRoot = path.dirname(env.sourceFiles[0]);
env.sourceFiles.slice(1).forEach((filePath) => {
if (filePath.startsWith(implicitModuleRoot)) {
return;
}
const currParts = filePath.split(path.sep);
const nearestParts = implicitModuleRoot.split(path.sep);
for (let i = 0; i < currParts.length; ++i) {
if (currParts[i] !== nearestParts[i]) {
implicitModuleRoot = currParts.slice(0, i).join(path.sep);
return;
}
}
});
return implicitModuleRoot;
}
function getModuleId(modulePath) {
if (moduleInfos[modulePath]) {
return moduleInfos[modulePath].id;
}
// Search for explicit module id
if (fileNodes[modulePath]) {
for (const comment of fileNodes[modulePath].comments) {
if (!/@module(?=\s)/.test(comment.value)) {
continue;
}
const explicitModuleId = comment.value
.split(/@module(?=\s)/)[1]
.split(/\n+\s*\*\s*@\w+/)[0] // Split before the next tag
.replace(/\n+\s*\*|\{[^\}]*\}/g, '') // Remove new lines with asterisks, and type annotations
.trim();
if (explicitModuleId) {
return explicitModuleId;
}
}
}
return path
.relative(getImplicitModuleRoot(), modulePath)
.replace(extensionReplaceRegEx, '');
}
/**
* Checks for the existence of `modulePath`, and if it doesn't exist, checks for `modulePath/index.js`.
* @param {string} from The path to the module.
* @param {string} to The path to the module.
* @return {string | null} The resolved path or null if it can't be resolved.
*/
function getResolvedPath(from, to) {
let resolvedPath = path.resolve(from, to);
if (resolvedPathCache.has(resolvedPath) || fs.existsSync(resolvedPath)) {
resolvedPathCache.add(resolvedPath);
return resolvedPath;
}
resolvedPath = resolvedPath.replace(extensionEnsureRegEx, '/index.js');
if (resolvedPathCache.has(resolvedPath) || fs.existsSync(resolvedPath)) {
resolvedPathCache.add(resolvedPath);
return resolvedPath;
}
return null;
}
function getModuleInfo(modulePath, parser) {
if (!modulePath) {
return null;
}
if (!moduleInfos[modulePath]) {
if (!fileNodes[modulePath]) {
const file = fs.readFileSync(modulePath, {encoding: 'utf8'});
fileNodes[modulePath] = parser.astBuilder.build(file, modulePath);
}
moduleInfos[modulePath] = {
id: getModuleId(modulePath),
namedExports: {},
};
const moduleInfo = moduleInfos[modulePath];
const node = fileNodes[modulePath];
if (node.program && node.program.body) {
const classDeclarations = {};
const nodes = node.program.body;
for (let i = 0, ii = nodes.length; i < ii; ++i) {
const node = nodes[i];
if (node.type === 'ClassDeclaration') {
classDeclarations[node.id.name] = node;
} else if (node.type === 'ExportDefaultDeclaration') {
const classDeclaration = classDeclarations[node.declaration.name];
if (classDeclaration) {
moduleInfo.defaultExport = classDeclaration.id.name;
}
} else if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.type === 'ClassDeclaration'
) {
moduleInfo.namedExports[node.declaration.id.name] = true;
}
}
}
}
return moduleInfos[modulePath];
}
function getDefaultExportName(modulePath) {
return getModuleInfo(modulePath).defaultExport;
}
function getDelimiter(modulePath, symbol) {
return getModuleInfo(modulePath).namedExports[symbol] ? '.' : '~';
}
function ensureJsExt(filePath) {
return filePath.replace(extensionEnsureRegEx, '.js');
}
/**
* Replaces text by indices where each element of `replacements` is `[startIndex, endIndex, replacement]`.
*
* Note: This function does not handle nested replacements.
*
* @param {string} text The text to replace
* @param {Array<[number, number, string]>} replacements The replacements to apply
* @return {string} The text with replacements applied
*/
function replaceByIndices(text, replacements) {
let offset = 0;
let replacedText = text;
replacements.forEach(([startIndex, endIndex, replacement], i) => {
const head = replacedText.slice(0, startIndex + offset);
const tail = replacedText.slice(endIndex + offset);
replacedText = head + replacement + tail;
offset += replacement.length - (endIndex - startIndex);
});
return replacedText;
}
exports.astNodeVisitor = {
visitNode: function (node, e, parser, currentSourceName) {
if (node.type === 'File') {
fileNodes[currentSourceName] = node;
const identifiers = {};
if (node.program && node.program.body) {
const nodes = node.program.body;
for (let i = 0, ii = nodes.length; i < ii; ++i) {
let node = nodes[i];
let leadingComments = node.leadingComments;
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
node = node.declaration;
if (node.leadingComments) {
leadingComments = node.leadingComments;
}
}
if (node.type === 'ImportDeclaration') {
node.specifiers.forEach((specifier) => {
let defaultImport = false;
switch (specifier.type) {
case 'ImportDefaultSpecifier':
defaultImport = true;
// fallthrough
case 'ImportSpecifier':
identifiers[specifier.local.name] = {
defaultImport,
value: node.source.value,
};
break;
default:
}
});
} else if (node.type === 'VariableDeclaration') {
for (const declaration of node.declarations) {
let declarationComments = leadingComments;
if (declaration.leadingComments) {
declarationComments = declaration.leadingComments;
}
if (declarationComments && declarationComments.length > 0) {
const comment =
declarationComments[declarationComments.length - 1].value;
if (/@enum/.test(comment)) {
identifiers[declaration.id.name] = {
value: path.basename(currentSourceName),
};
}
}
}
} else if (node.type === 'ClassDeclaration') {
if (node.id && node.id.name) {
identifiers[node.id.name] = {
value: path.basename(currentSourceName),
};
}
if (!node.leadingComments) {
node.leadingComments = [];
// Restructure named exports of classes so only the class, but not
// the export are documented
if (
node.parent &&
node.parent.type === 'ExportNamedDeclaration' &&
node.parent.leadingComments
) {
for (
let i = node.parent.leadingComments.length - 1;
i >= 0;
--i
) {
const comment = node.parent.leadingComments[i];
if (
comment.value.indexOf('@classdesc') !== -1 ||
!noClassdescRegEx.test(comment.value)
) {
node.leadingComments.unshift(comment);
node.parent.leadingComments.splice(i, 1);
const ignore =
parser.astBuilder.build('/** @ignore */').comments[0];
node.parent.leadingComments.push(ignore);
}
}
}
}
const leadingComments = node.leadingComments;
if (
leadingComments.length === 0 ||
(leadingComments[leadingComments.length - 1].value.indexOf(
'@classdesc',
) === -1 &&
noClassdescRegEx.test(
leadingComments[leadingComments.length - 1].value,
))
) {
// Create a suitable comment node if we don't have one on the class yet
const comment = parser.astBuilder.build('/**\n */', 'helper')
.comments[0];
node.leadingComments.push(comment);
}
const leadingComment =
leadingComments[node.leadingComments.length - 1];
const lines = leadingComment.value.split(/\r?\n/);
// Add @classdesc to make JSDoc show the class description
if (leadingComment.value.indexOf('@classdesc') === -1) {
lines[0] += ' @classdesc';
}
if (node.superClass) {
// Remove the `@extends` tag because JSDoc does not does not handle generic type. (`@extends {Base<Type>}`)
const extendsIndex = lines.findIndex((line) =>
line.includes('@extends'),
);
if (extendsIndex !== -1) {
lines.splice(extendsIndex, 1);
}
// Add class inheritance information because JSDoc does not honor
// the ES6 class's `extends` keyword
lines.push(lines[lines.length - 1]);
const identifier = identifiers[node.superClass.name];
if (identifier) {
const absolutePath = getResolvedPath(
path.dirname(currentSourceName),
ensureJsExt(identifier.value),
);
const moduleInfo = getModuleInfo(absolutePath, parser);
if (moduleInfo) {
const exportName = identifier.defaultImport
? getDefaultExportName(absolutePath)
: node.superClass.name;
const delimiter = identifier.defaultImport
? '~'
: getDelimiter(absolutePath, exportName);
lines[lines.length - 2] =
' * @extends ' +
`module:${moduleInfo.id.replace(slashRegEx, '/')}${
exportName ? delimiter + exportName : ''
}`;
}
} else {
lines[lines.length - 2] = ' * @extends ' + node.superClass.name;
}
leadingComment.value = lines.join('\n');
}
}
}
}
if (node.comments) {
node.comments.forEach((comment) => {
// Replace typeof Foo with Class<Foo>
comment.value = comment.value.replace(
/typeof ([^,\|\}\>]*)([,\|\}\>])/g,
'Class<$1>$2',
);
// Remove `@override` annotations to avoid JSDoc breaking the inheritance chain
comment.value = comment.value.replace(' @override', ' ');
// Convert `import("path/to/module").export` to
// `module:path/to/module~Name`
let importMatch, lastImportPath, replaceAttempt;
while ((importMatch = importRegEx.exec(comment.value))) {
importRegEx.lastIndex = 0;
const importExpression = importMatch[0];
const importSource = importMatch[1];
const exportName = importMatch[2] || 'default';
const remainder = importMatch[3];
let replacement;
if (importSource.charAt(0) !== '.') {
// simplified replacement for external packages
replacement = `module:${importSource}${
exportName === 'default' ? '' : '~' + exportName
}`;
} else {
if (importExpression === lastImportPath) {
++replaceAttempt;
if (replaceAttempt > 100) {
// infinite loop protection
throw new Error(
`Invalid docstring ${comment.value} in ${currentSourceName}.`,
);
}
} else {
replaceAttempt = 0;
}
lastImportPath = importExpression;
const rel = getResolvedPath(
path.dirname(currentSourceName),
ensureJsExt(importSource),
);
const moduleInfo = getModuleInfo(rel, parser);
if (moduleInfo) {
const name =
exportName === 'default'
? getDefaultExportName(rel)
: exportName;
const delimiter =
exportName === 'default' ? '~' : getDelimiter(rel, name);
replacement = `module:${moduleInfo.id.replace(
slashRegEx,
'/',
)}${name ? delimiter + name : ''}`;
}
}
if (replacement) {
comment.value = comment.value.replace(
importExpression,
replacement + remainder,
);
}
}
// Treat `@typedef`s like named exports
const typedefMatches = comment.value
.replace(/\s*\*\s*/g, ' ')
.matchAll(typedefRegEx);
for (const match of typedefMatches) {
identifiers[match[1]] = {
value: path.basename(currentSourceName),
};
}
});
node.comments.forEach((comment) => {
// Replace local types with the full `module:` path
Object.keys(identifiers).forEach((key) => {
const eventRegex = new RegExp(
`@(event |fires )${key}([^A-Za-z])`,
'g',
);
replace(eventRegex);
const typeRegex = new RegExp(
`@(.*[{<|,(!?:]\\s*)(?<!module:|event:|external:)${key}([^A-Za-z].*?\}|\})`,
'g',
);
replace(typeRegex);
const linkWithoutTextRegex = new RegExp(
`@(link (?!https?:))${key}((?:\\.[^\\s}]*)*\\s*\\})`,
'g',
);
const linkMemberAccessorRegex = new RegExp(
`@(link (?!https?:)${key})(?:\\.[^\\s}]*)*([\\s|][^}]*\\})`,
'g',
);
const linkWithTextRegex = new RegExp(
`@(link (?!https?:))${key}([\\s|][^}]*\\})`,
'g',
);
// If link is without text, use key as text
comment.value = comment.value.replace(
linkWithoutTextRegex,
`@$1${key} ${key}$2`,
);
// Remove member accessors from link key
comment.value = comment.value.replace(
linkMemberAccessorRegex,
'@$1$2',
);
replace(linkWithTextRegex);
function replace(regex) {
if (regex.test(comment.value)) {
const identifier = identifiers[key];
const absolutePath = getResolvedPath(
path.dirname(currentSourceName),
ensureJsExt(identifier.value),
);
const moduleInfo = getModuleInfo(absolutePath, parser);
if (moduleInfo) {
const exportName = identifier.defaultImport
? getDefaultExportName(absolutePath)
: key;
const delimiter = identifier.defaultImport
? '~'
: getDelimiter(absolutePath, exportName);
const replacement = `module:${moduleInfo.id.replace(
slashRegEx,
'/',
)}${exportName ? delimiter + exportName : ''}`;
comment.value = comment.value.replace(
regex,
'@$1' + replacement + '$2',
);
}
}
}
});
});
}
}
},
};
exports.defineTags = function (dictionary) {
const tags = [
'type',
'typedef',
'property',
'return',
'param',
'template',
'default',
'member',
];
tags.forEach(function (tagName) {
const tag = dictionary.lookUp(tagName);
const oldOnTagText = tag.onTagText;
/**
* @param {string} tagText The tag text
* @return {string} The modified tag text
*/
tag.onTagText = function (tagText) {
if (oldOnTagText) {
tagText = oldOnTagText.apply(this, arguments);
}
const startIndex = tagText.search('{');
if (startIndex === -1) {
return tagText;
}
const len = tagText.length;
/** @type {Array<[number, number, string]>} */
let replacements = [];
let openCurly = 0;
let openSquare = 0;
let openRound = 0;
let isWithinString = false;
let quoteChar = '';
let i = startIndex;
let functionStartIndex;
while (i < len) {
switch (tagText[i]) {
case '\\':
// Skip escaped character
++i;
break;
case '"':
case "'":
if (isWithinString && quoteChar === tagText[i]) {
isWithinString = false;
quoteChar = '';
} else if (!isWithinString) {
isWithinString = true;
quoteChar = tagText[i];
}
break;
case ';':
// Replace interface-style semi-colon separators with commas
if (!isWithinString && openCurly > 1) {
const isTrailingSemiColon = /^\s*}/.test(tagText.slice(i + 1));
replacements.push([i, i + 1, isTrailingSemiColon ? '' : ',']);
}
break;
case '(':
if (openRound === 0) {
functionStartIndex = i;
}
++openRound;
break;
case ')':
if (!--openRound) {
// If round brackets form a function
const returnMatch = tagText.slice(i + 1).match(/^\s*(:|=>)/);
// Replace TS inline function syntax with JSDoc
if (returnMatch) {
const functionEndIndex = i + returnMatch[0].length + 1;
const hasFunctionKeyword = /\bfunction\s*$/.test(
tagText.slice(0, functionStartIndex),
);
// Filter out any replacements that are within the function
replacements = replacements.filter(([startIndex]) => {
return startIndex < functionStartIndex || startIndex > i;
});
replacements.push([
functionStartIndex,
functionEndIndex,
hasFunctionKeyword ? '():' : 'function():',
]);
}
functionStartIndex = null;
}
break;
case '[':
if (
isWithinString ||
variableNameRegEx.test(tagText.charAt(i - 1))
) {
break;
}
++openSquare;
break;
case ']':
if (isWithinString) {
break;
}
if (!--openSquare) {
// Replace [type1, type2] tuples with Array
replacements.push([startIndex + 1, i + 1, 'Array']);
}
break;
case '{':
++openCurly;
break;
case '}':
if (!--openCurly) {
const head = tagText.slice(0, startIndex);
const tail = tagText.slice(i + 1);
const replaced = replaceByIndices(
tagText.slice(startIndex, i + 1),
replacements,
)
// Replace `templateliteral` with 'templateliteral'
.replace(/`([^`]*)`/g, "'$1'")
// Bracket notation to dot notation
.replace(
/(\w+|>|\)|\])\[(?:'([^']+)'|"([^"]+)")\]/g,
'$1.$2$3',
);
return head + replaced + tail;
}
break;
default:
break;
}
++i;
}
throw new Error("Missing closing '}'");
};
});
};
exports.handlers = {
parseComplete: function (e) {
// Build inheritance chain after adding @extends annotations
addInherited(e.doclets, e.doclets.index);
},
};