forked from openlayers/jsdoc-plugin-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (212 loc) · 10.2 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
const path = require('path');
const fs = require('fs');
const env = require('jsdoc/env');
const addInherited = require('jsdoc/augment').addInherited;
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 \{[^\}]*\} ([^ \r?\n?]*)/;
const noClassdescRegEx = /@(typedef|module|type)/;
const slashRegEx = /\\/g;
const moduleInfos = {};
const fileNodes = {};
function getModuleInfo(moduleId, parser) {
if (!moduleInfos[moduleId]) {
const moduleInfo = moduleInfos[moduleId] = {
namedExports: {}
};
if (!fileNodes[moduleId]) {
const absolutePath = path.join(process.cwd(), moduleRoot, moduleId + '.js');
if (!fs.existsSync(absolutePath)) {
return null;
}
const file = fs.readFileSync(absolutePath, 'UTF-8');
fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath);
}
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.astNodeVisitor = {
visitNode: function(node, e, parser, currentSourceName) {
if (node.type === 'File') {
const relPath = path.relative(process.cwd(), currentSourceName);
const modulePath = path.relative(path.join(process.cwd(), moduleRoot), currentSourceName).replace(/\.js$/, '');
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];
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
node = node.declaration;
}
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 === '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);
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
if (getModuleInfo(moduleId, 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;
while ((importMatch = importRegEx.exec(comment.value))) {
importRegEx.lastIndex = 0;
let replacement;
if (importMatch[1].charAt(0) !== '.') {
// simplified replacement for external packages
replacement = `module:${importMatch[1]}${importMatch[2] === 'default' ? '' : '~' + importMatch[2]}`;
} else {
const rel = path.resolve(path.dirname(currentSourceName), importMatch[1]);
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), rel).replace(/\.js$/, '');
if (getModuleInfo(moduleId, parser)) {
const exportName = importMatch[2] === 'default' ? getDefaultExportName(moduleId, parser) : importMatch[2];
const delimiter = importMatch[2] === 'default' ? '~': getDelimiter(moduleId, exportName, parser);
replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
}
}
if (replacement) {
comment.value = comment.value.replace(importMatch[0], replacement + importMatch[3]);
}
}
// Treat `@typedef`s like named exports
const typedefMatch = comment.value.replace(/\r?\n?\s*\*\s/g, ' ').match(typedefRegEx);
if (typedefMatch) {
identifiers[typedefMatch[1]] = {
value: path.basename(currentSourceName)
};
}
// Replace local types with the full `module:` path
Object.keys(identifiers).forEach(key => {
const regex = new RegExp(`(@event |@fires |[\{<\|,] ?!?)${key}`, 'g');
if (regex.test(comment.value)) {
const identifier = identifiers[key];
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
if (getModuleInfo(moduleId, 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);
}
}
});
});
}
}
}
};
exports.handlers = {
parseComplete: function(e) {
// Build inheritance chain after adding @extends annotations
addInherited(e.doclets, e.doclets.index);
}
}