@@ -17,7 +17,7 @@ if (!fs.existsSync(moduleRootAbsolute)) {
17
17
}
18
18
19
19
const importRegEx = / i m p o r t \( [ " ' ] ( [ ^ " ' ] * ) [ " ' ] \) \. ( [ ^ \. \| \} > < , \) = # \n ] * ) ( [ \. \| \} > < , \) = # \n ] ) / g;
20
- const typedefRegEx = / @ t y p e d e f \{ [ ^ \} ] * \} ( \S + ) / ;
20
+ const typedefRegEx = / @ t y p e d e f \{ [ ^ \} ] * \} ( \S + ) / g ;
21
21
const noClassdescRegEx = / @ ( t y p e d e f | m o d u l e | t y p e ) / ;
22
22
const slashRegEx = / \\ / g;
23
23
@@ -26,9 +26,6 @@ const fileNodes = {};
26
26
27
27
function getModuleInfo ( moduleId , parser ) {
28
28
if ( ! moduleInfos [ moduleId ] ) {
29
- const moduleInfo = moduleInfos [ moduleId ] = {
30
- namedExports : { }
31
- } ;
32
29
if ( ! fileNodes [ moduleId ] ) {
33
30
const absolutePath = path . join ( process . cwd ( ) , moduleRoot , moduleId + '.js' ) ;
34
31
if ( ! fs . existsSync ( absolutePath ) ) {
@@ -37,6 +34,9 @@ function getModuleInfo(moduleId, parser) {
37
34
const file = fs . readFileSync ( absolutePath , 'UTF-8' ) ;
38
35
fileNodes [ moduleId ] = parser . astBuilder . build ( file , absolutePath ) ;
39
36
}
37
+ const moduleInfo = moduleInfos [ moduleId ] = {
38
+ namedExports : { }
39
+ } ;
40
40
const node = fileNodes [ moduleId ] ;
41
41
if ( node . program && node . program . body ) {
42
42
const classDeclarations = { } ;
@@ -79,8 +79,12 @@ exports.astNodeVisitor = {
79
79
const nodes = node . program . body ;
80
80
for ( let i = 0 , ii = nodes . length ; i < ii ; ++ i ) {
81
81
let node = nodes [ i ] ;
82
+ let leadingComments = node . leadingComments ;
82
83
if ( node . type === 'ExportNamedDeclaration' && node . declaration ) {
83
84
node = node . declaration ;
85
+ if ( node . leadingComments ) {
86
+ leadingComments = node . leadingComments ;
87
+ }
84
88
}
85
89
if ( node . type === 'ImportDeclaration' ) {
86
90
node . specifiers . forEach ( specifier => {
@@ -98,6 +102,21 @@ exports.astNodeVisitor = {
98
102
default :
99
103
}
100
104
} ) ;
105
+ } else if ( node . type === 'VariableDeclaration' ) {
106
+ for ( const declaration of node . declarations ) {
107
+ let declarationComments = leadingComments ;
108
+ if ( declaration . leadingComments ) {
109
+ declarationComments = declaration . leadingComments ;
110
+ }
111
+ if ( declarationComments && declarationComments . length > 0 ) {
112
+ const comment = declarationComments [ declarationComments . length - 1 ] . value ;
113
+ if ( / @ e n u m / . test ( comment ) ) {
114
+ identifiers [ declaration . id . name ] = {
115
+ value : path . basename ( currentSourceName )
116
+ } ;
117
+ }
118
+ }
119
+ }
101
120
} else if ( node . type === 'ClassDeclaration' ) {
102
121
if ( node . id && node . id . name ) {
103
122
identifiers [ node . id . name ] = {
@@ -190,9 +209,9 @@ exports.astNodeVisitor = {
190
209
}
191
210
192
211
// Treat `@typedef`s like named exports
193
- const typedefMatch = comment . value . replace ( / \s * \* \s * / g, ' ' ) . match ( typedefRegEx ) ;
194
- if ( typedefMatch ) {
195
- identifiers [ typedefMatch [ 1 ] ] = {
212
+ const typedefMatches = comment . value . replace ( / \s * \* \s * / g, ' ' ) . matchAll ( typedefRegEx ) ;
213
+ for ( const match of typedefMatches ) {
214
+ identifiers [ match [ 1 ] ] = {
196
215
value : path . basename ( currentSourceName )
197
216
} ;
198
217
}
@@ -201,10 +220,10 @@ exports.astNodeVisitor = {
201
220
node . comments . forEach ( comment => {
202
221
// Replace local types with the full `module:` path
203
222
Object . keys ( identifiers ) . forEach ( key => {
204
- const eventRegex = new RegExp ( `@(event |fires )${ key } (\\s* )` , 'g' ) ;
223
+ const eventRegex = new RegExp ( `@(event |fires )${ key } ([^A-Za-z] )` , 'g' ) ;
205
224
replace ( eventRegex ) ;
206
225
207
- const typeRegex = new RegExp ( `@(.*[{<|,]\\s*[!?]? )${ key } (=?\\s*[}>|,] )` , 'g' ) ;
226
+ const typeRegex = new RegExp ( `@(.*[{<|,(!?: ]\\s*)${ key } ([^A-Za-z].*?\}|\} )` , 'g' ) ;
208
227
replace ( typeRegex ) ;
209
228
210
229
function replace ( regex ) {
0 commit comments