Skip to content

Commit

Permalink
Fix error when imported file is not found
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
nbilyk committed Feb 11, 2021
1 parent c7cbe80 commit e5fcd89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function getFileInfo(filename, source = null) {
moduleId: null, typedefs: [], filename: filenameNor,
});

const s = source || (fs.readFileSync(filenameNor).toString());
const s = source || ((fs.existsSync(filenameNor)) ?
fs.readFileSync(filenameNor).toString() : '');
s.replace(docCommentsRegex, (comment) => {
if (!fileInfo.moduleId) {
// Searches for @module doc comment
Expand Down Expand Up @@ -172,24 +173,27 @@ function relPath(root, relative) {
}

/**
* Given a filename, if there is no extension, scan the files for the
* Given a filename, if there is no extension, scan the files for the
* most likely match.
*
* @param {string} filename
* @returns {string}
* @param {string} filename The filename with or without an
* extension to resolve.
* @returns {string} The path to the resolved file.
*/
function inferExtension(filename) {
const filenameNor = path.normalize(filename);
const ext = path.extname(filenameNor);
if (ext) return ext;
if (ext && fs.existsSync(filename)) return ext;
const files = fs.readdirSync(path.dirname(filenameNor));

const name = path.basename(filenameNor);
return path.join(path.dirname(filenameNor), files.find((iFile) => {
const foundFile = files.find((iFile) => {
if (noExtension(iFile) == name) {
return true;
}
}));
});
if (foundFile === undefined) return filename;
return path.join(path.dirname(filenameNor), foundFile);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdoc-tsimport-plugin",
"version": "1.0.3",
"version": "1.0.4",
"description": "A JSDoc plugin to support the typescript module import syntax.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e5fcd89

Please sign in to comment.