Skip to content

Commit 18260ff

Browse files
authored
Merge pull request #10 from KlausBenndorf/fix-local-types-conversion
Fix type replacement issues
2 parents 8182dce + ad56372 commit 18260ff

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

index.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (!fs.existsSync(moduleRootAbsolute)) {
1717
}
1818

1919
const importRegEx = /import\(["']([^"']*)["']\)\.([^ \.\|\}><,\)=#\n]*)([ \.\|\}><,\)=#\n])/g;
20-
const typedefRegEx = /@typedef \{[^\}]*\} ([^ \r?\n?]*)/;
20+
const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/;
2121
const noClassdescRegEx = /@(typedef|module|type)/;
2222
const slashRegEx = /\\/g;
2323

@@ -180,7 +180,7 @@ exports.astNodeVisitor = {
180180
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), rel).replace(/\.js$/, '');
181181
if (getModuleInfo(moduleId, parser)) {
182182
const exportName = importMatch[2] === 'default' ? getDefaultExportName(moduleId, parser) : importMatch[2];
183-
const delimiter = importMatch[2] === 'default' ? '~': getDelimiter(moduleId, exportName, parser);
183+
const delimiter = importMatch[2] === 'default' ? '~' : getDelimiter(moduleId, exportName, parser);
184184
replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
185185
}
186186
}
@@ -190,25 +190,34 @@ exports.astNodeVisitor = {
190190
}
191191

192192
// Treat `@typedef`s like named exports
193-
const typedefMatch = comment.value.replace(/\r?\n?\s*\*\s/g, ' ').match(typedefRegEx);
193+
const typedefMatch = comment.value.replace(/\s*\*\s*/g, ' ').match(typedefRegEx);
194194
if (typedefMatch) {
195195
identifiers[typedefMatch[1]] = {
196196
value: path.basename(currentSourceName)
197197
};
198198
}
199+
});
199200

201+
node.comments.forEach(comment => {
200202
// Replace local types with the full `module:` path
201203
Object.keys(identifiers).forEach(key => {
202-
const regex = new RegExp(`@(event |fires |.*[\{<\|,] ?!?)${key}`, 'g');
203-
if (regex.test(comment.value)) {
204-
const identifier = identifiers[key];
205-
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
206-
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
207-
if (getModuleInfo(moduleId, parser)) {
208-
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
209-
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
210-
const replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
211-
comment.value = comment.value.replace(regex, '@$1' + replacement);
204+
const eventRegex = new RegExp(`@(event |fires )${key}(\\s*)`, 'g');
205+
replace(eventRegex);
206+
207+
const typeRegex = new RegExp(`@(.*[{<|,]\\s*[!?]?)${key}(=?\\s*[}>|,])`, 'g');
208+
replace(typeRegex);
209+
210+
function replace(regex) {
211+
if (regex.test(comment.value)) {
212+
const identifier = identifiers[key];
213+
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
214+
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
215+
if (getModuleInfo(moduleId, parser)) {
216+
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
217+
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
218+
let replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
219+
comment.value = comment.value.replace(regex, '@$1' + replacement + '$2');
220+
}
212221
}
213222
}
214223
});

0 commit comments

Comments
 (0)