|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const env = require('jsdoc/env'); |
| 4 | + |
| 5 | +const config = env.conf.typescript; |
| 6 | +if (!config) { |
| 7 | + throw new Error('Configuration "typescript" for jsdoc-plugin-typescript missing.'); |
| 8 | +} |
| 9 | +if (!'moduleRoot' in config) { |
| 10 | + throw new Error('Configuration "typescript.moduleRoot" for jsdoc-plugin-typescript missing.'); |
| 11 | +} |
| 12 | +const moduleRoot = config.moduleRoot; |
| 13 | +const moduleRootAbsolute = path.join(process.cwd(), moduleRoot); |
| 14 | +if (!fs.existsSync(moduleRootAbsolute)) { |
| 15 | + throw new Error('Directory "' + moduleRootAbsolute + '" does not exist. Check the "typescript.moduleRoot" config option for jsdoc-plugin-typescript'); |
| 16 | +} |
| 17 | + |
| 18 | +const importRegEx = /(typeof )?import\("([^"]*)"\)\.([^ \.\|\}><,\)=\n]*)([ \.\|\}><,\)=\n])/g; |
| 19 | +const typedefRegEx = /@typedef \{[^\}]*\} ([^ \r?\n?]*)/; |
| 20 | + |
| 21 | +const moduleInfos = {}; |
| 22 | +const fileNodes = {}; |
| 23 | + |
| 24 | +function getModuleInfo(moduleId, parser) { |
| 25 | + if (!moduleInfos[moduleId]) { |
| 26 | + const moduleInfo = moduleInfos[moduleId] = { |
| 27 | + namedExports: {} |
| 28 | + }; |
| 29 | + if (!fileNodes[moduleId]) { |
| 30 | + const classDeclarations = {}; |
| 31 | + const absolutePath = path.join(process.cwd(), moduleRoot, moduleId + '.js'); |
| 32 | + const file = fs.readFileSync(absolutePath, 'UTF-8'); |
| 33 | + const node = fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath); |
| 34 | + if (node.program && node.program.body) { |
| 35 | + const nodes = node.program.body; |
| 36 | + for (let i = 0, ii = nodes.length; i < ii; ++i) { |
| 37 | + const node = nodes[i]; |
| 38 | + if (node.type === 'ClassDeclaration') { |
| 39 | + classDeclarations[node.id.name] = node; |
| 40 | + } else if (node.type === 'ExportDefaultDeclaration') { |
| 41 | + const classDeclaration = classDeclarations[node.declaration.name]; |
| 42 | + if (classDeclaration) { |
| 43 | + moduleInfo.defaultExport = classDeclaration.id.name; |
| 44 | + } |
| 45 | + } else if (node.type === 'ExportNamedDeclaration' && node.declaration && node.declaration.type === 'ClassDeclaration') { |
| 46 | + moduleInfo.namedExports[node.declaration.id.name] = true; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return moduleInfos[moduleId]; |
| 53 | +} |
| 54 | + |
| 55 | +function getDefaultExportName(moduleId, parser) { |
| 56 | + return getModuleInfo(moduleId, parser).defaultExport; |
| 57 | +} |
| 58 | + |
| 59 | +function getDelimiter(moduleId, symbol, parser) { |
| 60 | + return getModuleInfo(moduleId, parser).namedExports[symbol] ? '.' : '~' |
| 61 | +} |
| 62 | + |
| 63 | +exports.astNodeVisitor = { |
| 64 | + |
| 65 | + visitNode: function(node, e, parser, currentSourceName) { |
| 66 | + if (node.type === 'File') { |
| 67 | + const relPath = path.relative(process.cwd(), currentSourceName); |
| 68 | + const modulePath = path.relative(path.join(process.cwd(), moduleRoot), currentSourceName).replace(/\.js$/, ''); |
| 69 | + fileNodes[modulePath] = node; |
| 70 | + const identifiers = {}; |
| 71 | + if (node.program && node.program.body) { |
| 72 | + const nodes = node.program.body; |
| 73 | + for (let i = 0, ii = nodes.length; i < ii; ++i) { |
| 74 | + let node = nodes[i]; |
| 75 | + if (node.type === 'ExportNamedDeclaration' && node.declaration) { |
| 76 | + node = node.declaration; |
| 77 | + } |
| 78 | + if (node.type === 'ImportDeclaration') { |
| 79 | + node.specifiers.forEach(specifier => { |
| 80 | + let defaultImport = false; |
| 81 | + switch (specifier.type) { |
| 82 | + case 'ImportDefaultSpecifier': |
| 83 | + defaultImport = true; |
| 84 | + // fallthrough |
| 85 | + case 'ImportSpecifier': |
| 86 | + identifiers[specifier.local.name] = { |
| 87 | + defaultImport, |
| 88 | + value: node.source.value |
| 89 | + }; |
| 90 | + break; |
| 91 | + default: |
| 92 | + } |
| 93 | + }); |
| 94 | + } else if (node.type === 'ClassDeclaration') { |
| 95 | + if (node.id && node.id.name) { |
| 96 | + identifiers[node.id.name] = { |
| 97 | + value: path.basename(currentSourceName) |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + // Add class inheritance information because JSDoc does not honor |
| 102 | + // the ES6 class's `extends` keyword |
| 103 | + if (node.superClass && node.leadingComments) { |
| 104 | + const leadingComment = node.leadingComments[node.leadingComments.length - 1]; |
| 105 | + const lines = leadingComment.value.split(/\r?\n/); |
| 106 | + lines.push(lines[lines.length - 1]); |
| 107 | + const identifier = identifiers[node.superClass.name]; |
| 108 | + if (identifier) { |
| 109 | + const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value); |
| 110 | + const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, ''); |
| 111 | + const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : node.superClass.name; |
| 112 | + const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser); |
| 113 | + lines[lines.length - 2] = ' * @extends ' + `module:${moduleId}${exportName ? delimiter + exportName : ''}`; |
| 114 | + } else { |
| 115 | + lines[lines.length - 2] = ' * @extends ' + node.superClass.name; |
| 116 | + } |
| 117 | + leadingComment.value = lines.join('\n'); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if (node.comments) { |
| 124 | + node.comments.forEach(comment => { |
| 125 | + //TODO Handle typeof, to indicate that a constructor instead of an |
| 126 | + // instance is needed. |
| 127 | + comment.value = comment.value.replace(/typeof /g, ''); |
| 128 | + |
| 129 | + // Convert `import("path/to/module").export` to |
| 130 | + // `module:path/to/module~Name` |
| 131 | + let importMatch; |
| 132 | + while ((importMatch = importRegEx.exec(comment.value))) { |
| 133 | + importRegEx.lastIndex = 0; |
| 134 | + let replacement; |
| 135 | + if (importMatch[2].charAt(0) !== '.') { |
| 136 | + // simplified replacement for external packages |
| 137 | + replacement = `module:${importMatch[2]}${importMatch[3] === 'default' ? '' : '~' + importMatch[3]}`; |
| 138 | + } else { |
| 139 | + const rel = path.resolve(path.dirname(currentSourceName), importMatch[2]); |
| 140 | + const importModule = path.relative(path.join(process.cwd(), moduleRoot), rel).replace(/\.js$/, ''); |
| 141 | + const exportName = importMatch[3] === 'default' ? getDefaultExportName(importModule, parser) : importMatch[3]; |
| 142 | + const delimiter = importMatch[3] === 'default' ? '~': getDelimiter(importModule, exportName, parser); |
| 143 | + replacement = `module:${importModule}${exportName ? delimiter + exportName : ''}`; |
| 144 | + } |
| 145 | + comment.value = comment.value.replace(importMatch[0], replacement + importMatch[4]); |
| 146 | + } |
| 147 | + |
| 148 | + // Treat `@typedef`s like named exports |
| 149 | + const typedefMatch = comment.value.replace(/\r?\n?\s*\*\s/g, ' ').match(typedefRegEx); |
| 150 | + if (typedefMatch) { |
| 151 | + identifiers[typedefMatch[1]] = { |
| 152 | + value: path.basename(currentSourceName) |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + // Replace local types with the full `module:` path |
| 157 | + Object.keys(identifiers).forEach(key => { |
| 158 | + const regex = new RegExp(`(@fires |[\{<\|,] ?)${key}`, 'g'); |
| 159 | + if (regex.test(comment.value)) { |
| 160 | + const identifier = identifiers[key]; |
| 161 | + const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value); |
| 162 | + const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, ''); |
| 163 | + const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key; |
| 164 | + const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser); |
| 165 | + const replacement = `module:${moduleId}${exportName ? delimiter + exportName : ''}`; |
| 166 | + comment.value = comment.value.replace(regex, '$1' + replacement); |
| 167 | + } |
| 168 | + }); |
| 169 | + }); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +}; |
0 commit comments