Skip to content

Commit 91af72d

Browse files
committed
Do not fail on external types
1 parent 1538f6b commit 91af72d

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function getModuleInfo(moduleId, parser) {
3030
};
3131
if (!fileNodes[moduleId]) {
3232
const absolutePath = path.join(process.cwd(), moduleRoot, moduleId + '.js');
33+
if (!fs.existsSync(absolutePath)) {
34+
return null;
35+
}
3336
const file = fs.readFileSync(absolutePath, 'UTF-8');
3437
fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath);
3538
}
@@ -139,9 +142,11 @@ exports.astNodeVisitor = {
139142
if (identifier) {
140143
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
141144
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
142-
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : node.superClass.name;
143-
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
144-
lines[lines.length - 2] = ' * @extends ' + `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
145+
if (getModuleInfo(moduleId, parser)) {
146+
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : node.superClass.name;
147+
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
148+
lines[lines.length - 2] = ' * @extends ' + `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
149+
}
145150
} else {
146151
lines[lines.length - 2] = ' * @extends ' + node.superClass.name;
147152
}
@@ -169,11 +174,15 @@ exports.astNodeVisitor = {
169174
} else {
170175
const rel = path.resolve(path.dirname(currentSourceName), importMatch[1]);
171176
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), rel).replace(/\.js$/, '');
172-
const exportName = importMatch[2] === 'default' ? getDefaultExportName(moduleId, parser) : importMatch[2];
173-
const delimiter = importMatch[2] === 'default' ? '~': getDelimiter(moduleId, exportName, parser);
174-
replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
177+
if (getModuleInfo(moduleId, parser)) {
178+
const exportName = importMatch[2] === 'default' ? getDefaultExportName(moduleId, parser) : importMatch[2];
179+
const delimiter = importMatch[2] === 'default' ? '~': getDelimiter(moduleId, exportName, parser);
180+
replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
181+
}
182+
}
183+
if (replacement) {
184+
comment.value = comment.value.replace(importMatch[0], replacement + importMatch[3]);
175185
}
176-
comment.value = comment.value.replace(importMatch[0], replacement + importMatch[3]);
177186
}
178187

179188
// Treat `@typedef`s like named exports
@@ -191,10 +200,12 @@ exports.astNodeVisitor = {
191200
const identifier = identifiers[key];
192201
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
193202
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
194-
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
195-
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
196-
const replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
197-
comment.value = comment.value.replace(regex, '$1' + replacement);
203+
if (getModuleInfo(moduleId, parser)) {
204+
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
205+
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
206+
const replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
207+
comment.value = comment.value.replace(regex, '$1' + replacement);
208+
}
198209
}
199210
});
200211
});

0 commit comments

Comments
 (0)