1
1
'use strict' ;
2
2
const bs = require ( 'brighterscript' ) ;
3
3
const path = require ( 'path' ) ;
4
-
5
4
const jsCommentStartRegex = / ^ [ \s ] * (?: \/ \* + ) ? [ \s ] * ( .* ) / g
6
5
const bsMeaningfulCommentRegex = / ^ [ \s ] * (?: ' | R E M ) [ \s ] * \* * [ \s ] * ( .* ) / g
7
6
const paramRegex = / @ p a r a m \s + (?: { ( [ ^ } ] * ) } ) ? \s + (?: \[ ( \w + ) .* \] | ( \w + ) ) [ \s - \s | \s ] * ( .* ) /
8
7
const returnRegex = / @ r e t u r n s ? \s * ( { (?: [ ^ } ] * ) } ) ? \s * ( .* ) /
9
8
const extendsRegex = / @ e x t e n d s /
9
+ const moduleRegex = / @ m o d u l e ( [ ^ \* \s ] + ) /
10
+ const opts = env && env . opts || { }
11
+ const pluginOpts = opts [ 'brighterscript-jsdocs-plugin' ] || { } ;
12
+
13
+ if ( pluginOpts . addModule === undefined || pluginOpts . addModule === null ) {
14
+ pluginOpts . addModule = true
15
+ }
16
+
17
+
10
18
11
19
/** @type {string[] } */
12
20
const namespacesCreated = [ ]
13
21
22
+ /** @type {string[] } */
23
+ const modulesCreated = [ ]
24
+
14
25
/** @type {string[] } */
15
26
let parserLines = [ ]
16
27
@@ -109,14 +120,28 @@ function getCommentForStatement(comments, stmt) {
109
120
}
110
121
111
122
function getMemberOf ( moduleName = "" , namespaceName = "" ) {
112
- const memberOf = namespaceName || moduleName ;
113
-
123
+ const memberOf = namespaceName || moduleName . replace ( / \. / g , '/' ) ;
124
+ const memberType = /*namespaceName ? "" : */ "module:"
114
125
if ( memberOf ) {
115
- return ( ` * @memberof module: ${ memberOf } ` ) ;
126
+ return ` * @memberof! ${ memberType } ${ memberOf . replace ( / \. / g , '/' ) } ` ;
116
127
}
117
128
return ""
118
129
}
119
130
131
+ /**
132
+ *
133
+ * @param {string } moduleName
134
+ * @returns
135
+ */
136
+ function getModuleLineComment ( moduleName = "" ) {
137
+ const modifiedModuleName = moduleName . replace ( / \. / g, '/' )
138
+ if ( ! modifiedModuleName || modulesCreated . includes ( modifiedModuleName ) ) {
139
+ return ""
140
+ }
141
+ modulesCreated . push ( modifiedModuleName ) ;
142
+ return [ `/**` , ` * @module ${ modifiedModuleName } ` , ` */` ] . join ( '\n' ) ;
143
+ }
144
+
120
145
/**
121
146
* Convert a comment statement text to Js Doc Lines
122
147
* This will return a string[] with each line of a block comment
@@ -134,13 +159,16 @@ function convertCommentTextToJsDocLines(comment) {
134
159
// * Comment here
135
160
commentLines . push ( ...comment . text . split ( '\n' ) . map ( ( line , i ) => {
136
161
return line . replace ( bsMeaningfulCommentRegex , '$1' ) ;
137
- } ) . map ( line => line . trim ( ) ) . map ( ( line , i , lines ) => {
138
- if ( i === 0 ) {
139
- line = line . replace ( jsCommentStartRegex , '$1' ) ;
140
- }
141
- line = line . replace ( / \* * \/ \s * / g, "" )
142
- return " * " + line ;
143
- } ) )
162
+ } ) . map ( line => line . trim ( ) )
163
+ . filter ( ( line ) => {
164
+ return ! line . includes ( '@module' )
165
+ } ) . map ( ( line , i , lines ) => {
166
+ if ( i === 0 ) {
167
+ line = line . replace ( jsCommentStartRegex , '$1' ) ;
168
+ }
169
+ line = line . replace ( / \* * \/ \s * / g, "" )
170
+ return " * " + line ;
171
+ } ) )
144
172
}
145
173
return commentLines
146
174
}
@@ -333,7 +361,7 @@ function processClass(comment, klass, moduleName = "", namespaceName = "") {
333
361
let parentClassName = "" , extendsLine = ""
334
362
if ( klass . parentClassName ) {
335
363
parentClassName = klass . parentClassName . getName ( )
336
- extendsLine = ` * @extends ${ klass . parentClassName . getName ( ) } `
364
+ extendsLine = ` * @extends ${ klass . parentClassName . getName ( bs . ParseMode . BrighterScript ) } `
337
365
}
338
366
339
367
for ( var i = 0 ; i < commentLines . length ; i ++ ) {
@@ -371,6 +399,7 @@ function processClass(comment, klass, moduleName = "", namespaceName = "") {
371
399
372
400
output . push ( '}\n' )
373
401
if ( namespaceName ) {
402
+ // output.push(...['/**', ' * @class', ' */']);
374
403
output . push ( `${ namespaceName } .${ klassName } = ${ klassName } ; ` )
375
404
}
376
405
return output . join ( '\n' )
@@ -389,32 +418,52 @@ function processClass(comment, klass, moduleName = "", namespaceName = "") {
389
418
function processNamespace ( comment , namespace , moduleName = "" , parentNamespaceName ) {
390
419
391
420
const output = [ ] ;
392
- let namespaceName = namespace . name ;
393
-
394
- if ( parentNamespaceName ) {
395
- namespaceName = parentNamespaceName + "." + namespaceName
421
+ const namespaceParts = namespace . name . split ( '.' ) ;
422
+ const namespaceNames = [ ]
423
+ let namespaceNameChain = ""
424
+ for ( const namespacePart of namespaceParts ) {
425
+ if ( namespaceNameChain . length > 0 ) {
426
+ namespaceNameChain += '.'
427
+ }
428
+ namespaceNameChain += namespacePart ;
429
+ namespaceNames . push ( namespaceNameChain )
396
430
}
397
- if ( ! namespacesCreated . includes ( namespaceName ) ) {
398
- // have not created this namespace yet
399
- let commentLines = convertCommentTextToJsDocLines ( comment ) ;
400
-
401
- // if (namespaceName !== moduleName) {
402
- commentLines . push ( getMemberOf ( moduleName , parentNamespaceName ) ) ;
403
- // }
404
- commentLines . push ( ` * @namespace ${ namespaceName } ` )
405
- commentLines . push ( ' */' ) ;
406
-
407
- output . push ( commentLines . join ( '\n' ) ) ;
431
+ let index = 0
432
+ let previousNamespace = ""
433
+ for ( const namespaceName of namespaceNames ) {
434
+ let subNamespace = namespaceName
408
435
if ( parentNamespaceName ) {
409
- output . push ( ` ${ parentNamespaceName } .namespaceName = {}` )
436
+ subNamespace = parentNamespaceName + "." + namespaceName
410
437
}
411
- else {
412
- output . push ( `var ${ namespaceName } = {}; ` ) ;
438
+ if ( ! namespacesCreated . includes ( subNamespace ) ) {
439
+ // have not created this namespace yet
440
+
441
+ output . push ( getModuleLineComment ( subNamespace ) ) ;
442
+ let commentLines = convertCommentTextToJsDocLines ( comment ) ;
443
+ if ( subNamespace != moduleName ) {
444
+ commentLines . push ( getMemberOf ( previousNamespace ) ) ;
445
+ }
446
+ //commentLines.push(` * @namespace {object } ${subNamespace} `)
447
+ commentLines . push ( ' */' ) ;
448
+
449
+ output . push ( commentLines . join ( '\n' ) ) ;
450
+ /*
451
+ if (parentNamespaceName || index > 0) {
452
+ output.push(`${subNamespace} = {}`)
453
+ }
454
+ else {
455
+ output.push(`var ${subNamespace} = {}; `);
456
+ }*/
457
+ namespacesCreated . push ( subNamespace )
413
458
}
414
- namespacesCreated . push ( namespaceName )
459
+ previousNamespace = subNamespace
460
+ index ++
415
461
}
416
-
417
- output . push ( processStatements ( namespace . body . statements , moduleName , namespaceName ) )
462
+ let totalNamespace = namespace . name
463
+ if ( parentNamespaceName ) {
464
+ totalNamespace = parentNamespaceName + "." + totalNamespace
465
+ }
466
+ output . push ( processStatements ( namespace . body . statements , moduleName , totalNamespace ) )
418
467
return output . join ( '\n' ) ;
419
468
}
420
469
@@ -472,19 +521,20 @@ exports.handlers = {
472
521
const statements = parseResult . statements
473
522
474
523
// Add our module to the top of the file if it doesn't exist. If it does find out the name
475
- const moduleMatch = e . source . match ( / @ m o d u l e ( [ ^ \* \s ] + ) / ) ;
476
- let moduleName = "" ;
524
+ const moduleMatch = e . source . match ( moduleRegex ) ;
477
525
const output = [ ] ;
478
- if ( moduleMatch ) {
479
- moduleName = moduleMatch [ 1 ] ;
480
- } else {
481
- moduleName = path . parse ( e . filename ) . name . replace ( / \. / g, '_' ) ;
526
+ let moduleName = "" ;
527
+ if ( pluginOpts . addModule ) {
528
+ if ( moduleMatch ) {
529
+ moduleName = moduleMatch [ 1 ] ;
530
+ } else {
531
+ moduleName = path . parse ( e . filename ) . name . split ( '.' ) [ 0 ] . replace ( / \. / g, '_' ) ;
532
+ }
533
+ output . push ( getModuleLineComment ( moduleName ) ) ;
482
534
}
483
- output . push ( `/** @module ${ moduleName } */` ) ;
484
-
485
535
output . push ( processStatements ( statements , moduleName ) )
486
536
487
537
e . source = output . join ( '\n' ) ;
488
- //console.log(e.source)
538
+ // console.log(e.source)
489
539
}
490
540
} ;
0 commit comments