@@ -31,27 +31,34 @@ const moduleInfos = {};
3131const fileNodes = { } ;
3232
3333// Without explicit module ids, JSDoc will use the nearest shared ancestor directory
34+ /** @type {string } */
3435let implicitModuleRoot ;
3536
3637function getImplicitModuleRoot ( ) {
3738 if ( implicitModuleRoot ) {
3839 return implicitModuleRoot ;
3940 }
4041
41- implicitModuleRoot = env . sourceFiles . reduce ( ( nearestAncestor , curr , i ) => {
42- if ( curr . startsWith ( nearestAncestor ) || i === 0 ) {
43- return nearestAncestor ;
42+ if ( ! env . sourceFiles || env . sourceFiles . length === 0 ) {
43+ return process . cwd ( ) ;
44+ }
45+
46+ implicitModuleRoot = env . sourceFiles [ 0 ] ;
47+
48+ env . sourceFiles . slice ( 1 ) . forEach ( ( filePath ) => {
49+ if ( filePath . startsWith ( implicitModuleRoot ) ) {
50+ return implicitModuleRoot ;
4451 }
4552
46- const currParts = curr . split ( path . sep ) ;
47- const nearestParts = nearestAncestor . split ( path . sep ) ;
53+ const currParts = filePath . split ( path . sep ) ;
54+ const nearestParts = implicitModuleRoot . split ( path . sep ) ;
4855
4956 for ( let i = 0 ; i < currParts . length ; ++ i ) {
5057 if ( currParts [ i ] !== nearestParts [ i ] ) {
5158 return currParts . slice ( 0 , i ) . join ( path . sep ) ;
5259 }
5360 }
54- } , path . dirname ( env . sourceFiles [ 0 ] ) ) ;
61+ } ) ;
5562
5663 return implicitModuleRoot ;
5764}
@@ -135,13 +142,9 @@ function getModuleId(modulePath) {
135142 }
136143 }
137144
138- if ( getImplicitModuleRoot ( ) ) {
139- return path
140- . relative ( implicitModuleRoot , modulePath )
141- . replace ( extensionReplaceRegEx , '' ) ;
142- }
143-
144- throw new Error ( `Unable to resolve module id for file ${ modulePath } .` ) ;
145+ return path
146+ . relative ( getImplicitModuleRoot ( ) , modulePath )
147+ . replace ( extensionReplaceRegEx , '' ) ;
145148}
146149
147150function withJsExt ( filePath ) {
0 commit comments