diff --git a/index.js b/index.js index 5cded48..937ab64 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ if (!fs.existsSync(moduleRootAbsolute)) { } const importRegEx = /import\(["']([^"']*)["']\)\.([^ \.\|\}><,\)=#\n]*)([ \.\|\}><,\)=#\n])/g; -const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/; +const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/g; const noClassdescRegEx = /@(typedef|module|type)/; const slashRegEx = /\\/g; @@ -26,9 +26,6 @@ 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)) { @@ -37,6 +34,9 @@ function getModuleInfo(moduleId, parser) { const file = fs.readFileSync(absolutePath, 'UTF-8'); fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath); } + const moduleInfo = moduleInfos[moduleId] = { + namedExports: {} + }; const node = fileNodes[moduleId]; if (node.program && node.program.body) { const classDeclarations = {}; @@ -79,8 +79,12 @@ exports.astNodeVisitor = { 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 => { @@ -98,6 +102,21 @@ exports.astNodeVisitor = { 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] = { @@ -190,9 +209,9 @@ exports.astNodeVisitor = { } // Treat `@typedef`s like named exports - const typedefMatch = comment.value.replace(/\s*\*\s*/g, ' ').match(typedefRegEx); - if (typedefMatch) { - identifiers[typedefMatch[1]] = { + const typedefMatches = comment.value.replace(/\s*\*\s*/g, ' ').matchAll(typedefRegEx); + for (const match of typedefMatches) { + identifiers[match[1]] = { value: path.basename(currentSourceName) }; } @@ -201,10 +220,10 @@ exports.astNodeVisitor = { node.comments.forEach(comment => { // Replace local types with the full `module:` path Object.keys(identifiers).forEach(key => { - const eventRegex = new RegExp(`@(event |fires )${key}(\\s*)`, 'g'); + const eventRegex = new RegExp(`@(event |fires )${key}([^A-Za-z])`, 'g'); replace(eventRegex); - const typeRegex = new RegExp(`@(.*[{<|,]\\s*[!?]?)${key}(=?\\s*[}>|,])`, 'g'); + const typeRegex = new RegExp(`@(.*[{<|,(!?:]\\s*)${key}([^A-Za-z].*?\}|\})`, 'g'); replace(typeRegex); function replace(regex) {