Skip to content

Commit d9623d9

Browse files
Use process.cwd as root dir if all source files are within it
1 parent 2033987 commit d9623d9

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ In the absence of `typescript.moduleRoot`, the plugin will mirror the method JSD
2727

2828
1. Parse the referenced module for an `@module` tag.
2929
2. If a tag is found and it has an explicit id, use that.
30-
3. If a tag is found, but it doesn't have an explicit id, use the file path relative to the **nearest shared parent directory**, and remove the file extension.
30+
3. If a tag is found, but it doesn't have an explicit id, use the module's file path relative to the root directory, and remove the file extension.
31+
32+
**NOTE:** The root directory will be `process.cwd()` unless any source files are outside it, in which case, the root folder is considered the nearest shared parent directory of all source files.
3133

3234
## What this plugin does
3335

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,25 @@ const fileNodes = {};
3535
let implicitModuleRoot;
3636

3737
/**
38-
* @return {string} The nearest shared parent directory of all source files.
38+
* Without explicit module ids, JSDoc will use `process.cwd()` if all source files are within cwd.
39+
* If any source files are outside cwd, JSDoc will use the nearest shared parent directory.
40+
* @return {string} The implicit root path with which to resolve all module ids against.
3941
*/
4042
function getImplicitModuleRoot() {
4143
if (implicitModuleRoot) {
4244
return implicitModuleRoot;
4345
}
4446

45-
if (!env.sourceFiles || env.sourceFiles.length === 0) {
47+
if (
48+
!env.sourceFiles ||
49+
env.sourceFiles.length === 0 ||
50+
// If all files are in cwd
51+
env.sourceFiles.every((f) => f.startsWith(process.cwd()))
52+
) {
4653
return process.cwd();
4754
}
4855

56+
// Find the nearest shared parent directory
4957
implicitModuleRoot = path.dirname(env.sourceFiles[0]);
5058

5159
env.sourceFiles.slice(1).forEach((filePath) => {

0 commit comments

Comments
 (0)