@@ -4,8 +4,8 @@ const path = require('path');
4
4
5
5
const jsCommentStartRegex = / ^ [ \s ] * (?: \/ \* + ) ? [ \s ] * ( .* ) / g
6
6
const bsMeaningfulCommentRegex = / ^ [ \s ] * (?: ' | R E M ) [ \s ] * \* * [ \s ] * ( .* ) / g
7
- const paramRegex = / @ p a r a m (?: { ( [ ^ } ] * ) } ) ? ( \w + ) [ \s - \s | \s ] * ( .* ) /
8
- const returnRegex = / @ r e t u r n s ? \s * (?: { (?: [ ^ } ] * ) } ) ? \s * ( .* ) /
7
+ const paramRegex = / @ p a r a m \s + (?: { ( [ ^ } ] * ) } ) ? \s + (?: \[ ( \w + ) . * \] | ( \w + ) ) [ \s - \s | \s ] * ( .* ) /
8
+ const returnRegex = / @ r e t u r n s ? \s * ( { (?: [ ^ } ] * ) } ) ? \s * ( .* ) /
9
9
const extendsRegex = / @ e x t e n d s /
10
10
11
11
/** @type {string[] } */
@@ -69,6 +69,27 @@ function getTypeName(type) {
69
69
return "dynamic"
70
70
}
71
71
72
+
73
+ /**
74
+ * Helper to clean up param or return description strings
75
+ *
76
+ * @param {string } [desc=""]
77
+ * @return {string } cleaned up string
78
+ */
79
+ function paramOrReturnDescriptionHelper ( desc = "" ) {
80
+ desc = ( desc || "" ) . trim ( )
81
+ if ( desc . startsWith ( "-" ) ) {
82
+ return desc ;
83
+ }
84
+ if ( desc . startsWith ( "," ) ) {
85
+ desc = desc . substring ( 1 ) ;
86
+ }
87
+ if ( desc ) {
88
+ return "- " + desc ;
89
+ }
90
+ return ""
91
+ }
92
+
72
93
/**
73
94
* Finds the comment that ends the line above the given statement
74
95
*
@@ -83,11 +104,10 @@ function getCommentForStatement(comments, stmt) {
83
104
}
84
105
85
106
function getMemberOf ( moduleName = "" , namespaceName = "" ) {
86
- if ( namespaceName ) {
87
- return ( " * @memberof module:" + namespaceName )
88
- }
89
- if ( moduleName ) {
90
- return ( ` * @memberof module:${ moduleName } ` ) ;
107
+ const memberOf = namespaceName || moduleName
108
+
109
+ if ( memberOf ) {
110
+ return ( ` * @memberof module:${ memberOf } ` ) ;
91
111
}
92
112
return ""
93
113
}
@@ -167,78 +187,104 @@ function displayStatement(stmt) {
167
187
function processFunction ( comment , func , moduleName = "" , namespaceName = "" ) {
168
188
const output = [ ]
169
189
let commentLines = convertCommentTextToJsDocLines ( comment ) ;
190
+ const paramNameList = [ ]
170
191
171
192
// Find the param line in the comments that match each param
172
193
for ( const param of func . func . parameters ) {
173
194
let paramName = param . name . text ;
195
+ paramNameList . push ( paramName )
174
196
let paramType = getTypeName ( param . type . kind ) ;
175
- let paramDescription ;
176
- for ( var i = 0 ; i < commentLines . length ; i ++ ) {
177
- let commentMatch = commentLines [ i ] . match ( paramRegex ) ;
178
- if ( commentMatch && paramName === commentMatch [ 2 ] ) {
179
- if ( commentMatch [ 1 ] ) paramType = commentMatch [ 1 ] ;
180
- paramDescription = commentMatch [ 3 ] ;
181
- commentLines . splice ( i , 1 ) ;
182
- i -- ;
183
- break ;
197
+ let paramDescription = "" ;
198
+
199
+ // remove @param lines for the current param
200
+ commentLines = commentLines . filter ( commentLine => {
201
+ let commentMatch = commentLine . match ( paramRegex ) ;
202
+ if ( commentMatch ) {
203
+
204
+ const commentParamName = ( commentMatch [ 2 ] || commentMatch [ 3 ] ) || ""
205
+ const commentParamType = commentMatch [ 1 ] || ""
206
+
207
+ if ( paramName . trim ( ) . toLowerCase ( ) === commentParamName . trim ( ) . toLowerCase ( ) ) {
208
+ // same parameter name - use these details
209
+ if ( commentParamType ) {
210
+ paramType = commentParamType . trim ( ) ;
211
+ paramDescription = commentMatch [ 4 ] || paramDescription
212
+ }
213
+ return false
214
+ }
184
215
}
185
- }
216
+ return true
217
+ } )
186
218
187
219
let paramLine = ` * @param {${ paramType } } `
188
220
if ( param . defaultValue ) {
189
221
let start = param . defaultValue . range . start ;
190
222
let end = param . defaultValue . range . end ;
191
- let defaultValue = parserLines [ start . line - 1 ] . slice ( start . character , end . character ) ;
223
+ let defaultValue = parserLines [ start . line ] . slice ( start . character , end . character ) ;
192
224
paramLine += `[${ paramName } =${ defaultValue } ]`
193
- } else {
194
- paramLine += paramName ;
195
225
}
196
- if ( paramDescription ) paramLine += ` - ${ paramDescription } ` ;
197
- commentLines . push ( paramLine ) ;
226
+ else {
227
+
228
+ paramLine += paramName
229
+ }
230
+
231
+ if ( paramDescription ) {
232
+ paramLine += ` ${ paramOrReturnDescriptionHelper ( paramDescription ) } ` ;
233
+ }
234
+ output . push ( paramLine ) ;
198
235
}
199
- if ( func . name . text [ 0 ] === '_' || func . accessModifier === "Private" ) {
200
- commentLines . push ( ' * @access private' ) ;
236
+
237
+ if ( func . name . text [ 0 ] === '_' || func . accessModifier ?. kind === "Private" ) {
238
+ output . push ( ' * @access private' ) ;
201
239
}
202
240
203
241
let returnLine = ` * @return {${ getTypeName ( func . func . returns ) } }`
204
242
// Find the return line in the comments
205
243
for ( var i = 0 ; i < commentLines . length ; i ++ ) {
206
244
let commentMatch = commentLines [ i ] . match ( returnRegex ) ;
207
- if ( commentMatch && commentMatch [ 1 ] ) {
208
- returnLine = ` * @return {${ getTypeName ( func . func . returns ) } } - ${ commentMatch [ 1 ] } ` ;
209
-
245
+ if ( commentMatch ) {
246
+ let commentReturnType = getTypeName ( func . func . returns )
247
+ if ( commentMatch [ 1 ] && commentMatch [ 1 ] . trim ( ) . toLowerCase ( ) == getTypeName ( func . func . returns ) . toLowerCase ) {
248
+ // there is a return type given, and it matches the type of the function
249
+ commentReturnType = commentMatch [ 1 ] . trim ( )
250
+ }
251
+ returnLine = ` * @return {${ commentReturnType } }` ;
252
+ if ( commentMatch [ 2 ] ) {
253
+ returnLine += " " + paramOrReturnDescriptionHelper ( commentMatch [ 2 ] )
254
+ }
255
+ // remove the original comment @return line
210
256
commentLines . splice ( i , 1 ) ;
211
- break ;
212
257
}
213
258
}
214
259
215
- commentLines . push ( returnLine ) ;
216
- commentLines . push ( getMemberOf ( moduleName , namespaceName ) ) ;
260
+
261
+ const totalOutput = [ ...commentLines , ...output ]
262
+ totalOutput . push ( returnLine ) ;
263
+ totalOutput . push ( getMemberOf ( moduleName , namespaceName ) ) ;
217
264
218
265
if ( func . overrides ) {
219
- commentLines . push ( ` * @override` ) ;
266
+ totalOutput . push ( ` * @override` ) ;
220
267
}
221
268
222
269
const funcName = func . name . text
223
- let funcDeclaration = `function ${ funcName } () {}; \n`
270
+ let funcDeclaration = `function ${ funcName } ( ${ paramNameList . join ( ", " ) } ) { }; \n`
224
271
if ( func instanceof bs . ClassMethodStatement ) {
225
272
if ( funcName . toLowerCase ( ) === "new" ) {
226
- commentLines . push ( " * @constructor" )
227
- funcDeclaration = `constructor() {}; \n`
273
+ totalOutput . push ( " * @constructor" )
274
+ funcDeclaration = `constructor(${ paramNameList . join ( ", " ) } ) { }; \n`
228
275
}
229
276
else {
230
- funcDeclaration = `${ funcName } () {}; \n`
277
+ funcDeclaration = `${ funcName } ( ${ paramNameList . join ( ", " ) } ) { }; \n`
231
278
}
232
279
}
233
- commentLines . push ( ' */' ) ;
234
- output . push ( commentLines . join ( '\n' ) ) ;
280
+ totalOutput . push ( ' */' ) ;
235
281
236
- output . push ( funcDeclaration ) ;
282
+ totalOutput . push ( funcDeclaration ) ;
237
283
if ( namespaceName ) {
238
- output . push ( `${ namespaceName } .${ funcName } = ${ funcName } ;` )
284
+ totalOutput . push ( `${ namespaceName } .${ funcName } = ${ funcName } ; ` )
239
285
}
240
286
241
- return output . join ( '\n' )
287
+ return totalOutput . join ( '\n' )
242
288
}
243
289
244
290
/**
@@ -248,20 +294,17 @@ function processFunction(comment, func, moduleName = "", namespaceName = "") {
248
294
*
249
295
* @param {bs.CommentStatement } comment the comment in the line above this field
250
296
* @param {bs.ClassFieldStatement } field the field to process
251
- * @returns {string } the property tag for the class this field is in
297
+ * @return {string } the property tag for the class this field is in
252
298
*/
253
299
function processClassField ( comment , field ) {
254
- const output = [ ]
255
- if ( field . accessModifier && field . accessModifier === "Private" ) {
300
+ if ( field . accessModifier ?. kind === "Private" ) {
256
301
return ""
257
302
}
258
303
let description = "" ;
259
304
if ( comment ) {
260
305
description = comment . text . replace ( bsMeaningfulCommentRegex , '$1' ) ;
261
306
}
262
- output . push ( ` * @property {${ getTypeName ( field . type ) } } ${ field . name . text } ${ description } ` )
263
-
264
- return output . join ( '\n' )
307
+ return ` * @property { ${ getTypeName ( field . type ) } } ${ field . name . text } ${ description } ` ;
265
308
}
266
309
267
310
@@ -282,11 +325,10 @@ function processClass(comment, klass, moduleName = "", namespaceName = "") {
282
325
let commentLines = convertCommentTextToJsDocLines ( comment ) ;
283
326
const klassCode = groupStatements ( klass . body )
284
327
285
-
286
328
let parentClassName = "" , extendsLine = ""
287
329
if ( klass . parentClassName ) {
288
330
parentClassName = klass . parentClassName . getName ( )
289
- extendsLine = ` * @extends ${ klass . parentClassName . getName ( ) } `
331
+ extendsLine = ` * @extends ${ klass . parentClassName . getName ( ) } `
290
332
}
291
333
292
334
for ( var i = 0 ; i < commentLines . length ; i ++ ) {
@@ -324,7 +366,7 @@ function processClass(comment, klass, moduleName = "", namespaceName = "") {
324
366
325
367
output . push ( '}\n' )
326
368
if ( namespaceName ) {
327
- output . push ( `${ namespaceName } .${ klassName } = ${ klassName } ;` )
369
+ output . push ( `${ namespaceName } .${ klassName } = ${ klassName } ; ` )
328
370
}
329
371
return output . join ( '\n' )
330
372
}
@@ -352,15 +394,15 @@ function processNamespace(comment, namespace, moduleName = "", parentNamespaceNa
352
394
let commentLines = convertCommentTextToJsDocLines ( comment ) ;
353
395
354
396
commentLines . push ( getMemberOf ( moduleName , parentNamespaceName ) ) ;
355
- commentLines . push ( ` * @namespace ${ namespaceName } ` )
397
+ commentLines . push ( ` * @namespace ${ namespaceName } ` )
356
398
commentLines . push ( ' */' ) ;
357
399
358
400
output . push ( commentLines . join ( '\n' ) ) ;
359
401
if ( parentNamespaceName ) {
360
402
output . push ( `${ parentNamespaceName } .namespaceName = {}` )
361
403
}
362
404
else {
363
- output . push ( `var ${ namespaceName } = {};` ) ;
405
+ output . push ( `var ${ namespaceName } = {}; ` ) ;
364
406
}
365
407
namespacesCreated . push ( namespaceName )
366
408
}
@@ -432,6 +474,8 @@ exports.handlers = {
432
474
output . push ( `/** @module ${ moduleName } */` ) ;
433
475
}
434
476
output . push ( processStatements ( statements , moduleName ) )
477
+
435
478
e . source = output . join ( '\n' ) ;
479
+ // console.log(e.source)
436
480
}
437
481
} ;
0 commit comments