/
index.js
429 lines (412 loc) · 15.6 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
require('string.prototype.matchall').shim();
const path = require('path');
const fs = require('fs');
const env = require('jsdoc/env'); // eslint-disable-line import/no-unresolved
const addInherited = require('jsdoc/augment').addInherited; // eslint-disable-line import/no-unresolved
const config = env.conf.typescript;
if (!config) {
throw new Error(
'Configuration "typescript" for jsdoc-plugin-typescript missing.'
);
}
if (!('moduleRoot' in config)) {
throw new Error(
'Configuration "typescript.moduleRoot" for jsdoc-plugin-typescript missing.'
);
}
const moduleRoot = config.moduleRoot;
const moduleRootAbsolute = path.join(process.cwd(), moduleRoot);
if (!fs.existsSync(moduleRootAbsolute)) {
throw new Error(
'Directory "' +
moduleRootAbsolute +
'" does not exist. Check the "typescript.moduleRoot" config option for jsdoc-plugin-typescript'
);
}
const importRegEx =
/import\(["']([^"']*)["']\)(?:\.([^ \.\|\}><,\)=#\n]*))?([ \.\|\}><,\)=#\n])/g;
const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/g;
const noClassdescRegEx = /@(typedef|module|type)/;
const extensionReplaceRegEx = /\.m?js$/;
const slashRegEx = /\\/g;
const moduleInfos = {};
const fileNodes = {};
function getExtension(absolutePath) {
return extensionReplaceRegEx.test(absolutePath)
? extensionReplaceRegEx.exec(absolutePath)[0]
: '.js';
}
function getModuleInfo(moduleId, extension, parser) {
if (!moduleInfos[moduleId]) {
if (!fileNodes[moduleId]) {
const absolutePath = path.join(
process.cwd(),
moduleRoot,
moduleId + extension
);
if (!fs.existsSync(absolutePath)) {
return null;
}
const file = fs.readFileSync(absolutePath, 'UTF-8');
fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath);
}
moduleInfos[moduleId] = {namedExports: {}};
const moduleInfo = moduleInfos[moduleId];
const node = fileNodes[moduleId];
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[moduleId];
}
function getDefaultExportName(moduleId, parser) {
return getModuleInfo(moduleId, parser).defaultExport;
}
function getDelimiter(moduleId, symbol, parser) {
return getModuleInfo(moduleId, parser).namedExports[symbol] ? '.' : '~';
}
exports.defineTags = function (dictionary) {
['type', 'typedef', 'property', 'return', 'param', 'template'].forEach(
function (tagName) {
const tag = dictionary.lookUp(tagName);
const oldOnTagText = tag.onTagText;
tag.onTagText = function (tagText) {
if (oldOnTagText) {
tagText = oldOnTagText.apply(this, arguments);
}
// Replace `templateliteral` with 'templateliteral'
const startIndex = tagText.search('{');
if (startIndex === -1) {
return tagText;
}
const len = tagText.length;
let open = 0;
let i = startIndex;
while (i < len) {
switch (tagText[i]) {
case '\\':
// Skip escaped character
++i;
break;
case '{':
++open;
break;
case '}':
if (!--open) {
return (
tagText.slice(0, startIndex) +
tagText
.slice(startIndex, i + 1)
.replace(/`([^`]*)`/g, "'$1'") +
tagText.slice(i + 1)
);
}
break;
default:
break;
}
++i;
}
throw new Error("Missing closing '}'");
};
}
);
};
exports.astNodeVisitor = {
visitNode: function (node, e, parser, currentSourceName) {
if (node.type === 'File') {
const modulePath = path
.relative(path.join(process.cwd(), moduleRoot), currentSourceName)
.replace(extensionReplaceRegEx, '');
fileNodes[modulePath] = 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.push(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 = path.resolve(
path.dirname(currentSourceName),
identifier.value
);
// default to js extension since .js extention is assumed implicitly
const extension = getExtension(absolutePath);
const moduleId = path
.relative(path.join(process.cwd(), moduleRoot), absolutePath)
.replace(extensionReplaceRegEx, '');
if (getModuleInfo(moduleId, extension, parser)) {
const exportName = identifier.defaultImport
? getDefaultExportName(moduleId, parser)
: node.superClass.name;
const delimiter = identifier.defaultImport
? '~'
: getDelimiter(moduleId, exportName, parser);
lines[lines.length - 2] =
' * @extends ' +
`module:${moduleId.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'
);
// 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 = path.resolve(
path.dirname(currentSourceName),
importSource
);
// default to js extension since .js extention is assumed implicitly
const extension = getExtension(rel);
const moduleId = path
.relative(path.join(process.cwd(), moduleRoot), rel)
.replace(extensionReplaceRegEx, '');
if (getModuleInfo(moduleId, extension, parser)) {
const name =
exportName === 'default'
? getDefaultExportName(moduleId, parser)
: exportName;
const delimiter =
exportName === 'default'
? '~'
: getDelimiter(moduleId, name, parser);
replacement = `module:${moduleId.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*)${key}([^A-Za-z].*?\}|\})`,
'g'
);
replace(typeRegex);
function replace(regex) {
if (regex.test(comment.value)) {
const identifier = identifiers[key];
const absolutePath = path.resolve(
path.dirname(currentSourceName),
identifier.value
);
// default to js extension since .js extention is assumed implicitly
const extension = getExtension(absolutePath);
const moduleId = path
.relative(path.join(process.cwd(), moduleRoot), absolutePath)
.replace(extensionReplaceRegEx, '');
if (getModuleInfo(moduleId, extension, parser)) {
const exportName = identifier.defaultImport
? getDefaultExportName(moduleId, parser)
: key;
const delimiter = identifier.defaultImport
? '~'
: getDelimiter(moduleId, exportName, parser);
const replacement = `module:${moduleId.replace(
slashRegEx,
'/'
)}${exportName ? delimiter + exportName : ''}`;
comment.value = comment.value.replace(
regex,
'@$1' + replacement + '$2'
);
}
}
}
});
});
}
}
},
};
exports.handlers = {
parseComplete: function (e) {
// Build inheritance chain after adding @extends annotations
addInherited(e.doclets, e.doclets.index);
},
};