@@ -77,9 +77,7 @@ function organizeData(data) {
77
77
78
78
// Create the class entry if it doesn't exist
79
79
// if (!organized.classes[className]) {console.log(`returning for ${className}`); return};
80
-
81
- // Check for static methods - directly check path[0].scope
82
- // Todo: handle static methods
80
+
83
81
const isStatic = entry . path ?. [ 0 ] ?. scope === 'static' ;
84
82
// Handle overloads
85
83
const overloads = entry . overloads ?. map ( overload => ( {
@@ -122,7 +120,7 @@ function organizeData(data) {
122
120
} ;
123
121
}
124
122
fs . writeFileSync ( "./consts.json" , JSON . stringify ( organized . consts , null , 2 ) , 'utf8' ) ;
125
- } ) ;
123
+ } ) ;
126
124
127
125
return organized ;
128
126
}
@@ -193,11 +191,12 @@ function normalizeTypeName(type) {
193
191
const primitiveTypes = {
194
192
'String' : 'string' ,
195
193
'Number' : 'number' ,
194
+ 'Integer' : 'number' ,
196
195
'Boolean' : 'boolean' ,
197
196
'Void' : 'void' ,
198
197
'Object' : 'object' ,
199
- 'Array' : 'array ' ,
200
- 'Function' : 'function '
198
+ 'Array' : 'Array ' ,
199
+ 'Function' : 'Function '
201
200
} ;
202
201
203
202
return primitiveTypes [ type ] || type ;
@@ -226,8 +225,10 @@ function generateTypeFromTag(param) {
226
225
return 'any' ;
227
226
case 'RecordType' :
228
227
return 'object' ;
229
- case 'ObjectType' :
230
- return 'object' ;
228
+ case 'StringLiteralType' :
229
+ return `'${ param . type . value } '` ;
230
+ case 'UndefinedLiteralType' :
231
+ return 'undefined' ;
231
232
default :
232
233
return 'any' ;
233
234
}
@@ -288,6 +289,60 @@ function generateFunctionDeclaration(funcDoc) {
288
289
return output ;
289
290
}
290
291
292
+ // Helper function to generate method declarations
293
+ function generateMethodDeclarations ( item , isStatic = false ) {
294
+ let output = '' ;
295
+
296
+ // Add JSDoc comment
297
+ if ( item . description ) {
298
+ output += ' /**\n' ;
299
+ const itemDesc = extractDescription ( item . description ) ;
300
+ output += formatJSDocComment ( itemDesc , 2 ) + '\n' ;
301
+ if ( item . params ?. length > 0 ) {
302
+ output += ' *\n' ;
303
+ item . params . forEach ( param => {
304
+ const paramDesc = extractDescription ( param . description ) ;
305
+ output += formatJSDocComment ( `@param ${ paramDesc } ` , 2 ) + '\n' ;
306
+ } ) ;
307
+ }
308
+ if ( item . returns ) {
309
+ output += ' *\n' ;
310
+ const returnDesc = extractDescription ( item . returns [ 0 ] ?. description ) ;
311
+ output += formatJSDocComment ( `@return ${ returnDesc } ` , 2 ) + '\n' ;
312
+ }
313
+ output += ' */\n' ;
314
+ }
315
+
316
+ if ( item . kind === 'function' ) {
317
+ const staticPrefix = isStatic ? 'static ' : '' ;
318
+
319
+ // If there are overloads, generate all overload signatures first
320
+ if ( item . overloads ?. length > 0 ) {
321
+ item . overloads . forEach ( overload => {
322
+ const params = ( overload . params || [ ] )
323
+ . map ( param => generateParamDeclaration ( param ) )
324
+ . join ( ', ' ) ;
325
+ const returnType = overload . returns ?. [ 0 ] ?. type
326
+ ? generateTypeFromTag ( overload . returns [ 0 ] )
327
+ : 'void' ;
328
+ output += ` ${ staticPrefix } ${ item . name } (${ params } ): ${ returnType } ;\n` ;
329
+ } ) ;
330
+ }
331
+
332
+ // Generate the implementation signature
333
+ const params = ( item . params || [ ] )
334
+ . map ( param => generateParamDeclaration ( param ) )
335
+ . join ( ', ' ) ;
336
+ output += ` ${ staticPrefix } ${ item . name } (${ params } ): ${ item . returnType } ;\n\n` ;
337
+ } else {
338
+ // Handle properties
339
+ const staticPrefix = isStatic ? 'static ' : '' ;
340
+ output += ` ${ staticPrefix } ${ item . name } : ${ item . returnType } ;\n\n` ;
341
+ }
342
+
343
+ return output ;
344
+ }
345
+
291
346
// Generate class declaration
292
347
function generateClassDeclaration ( classDoc , organizedData ) {
293
348
let output = '' ;
@@ -324,102 +379,21 @@ function generateClassDeclaration(classDoc, organizedData) {
324
379
325
380
// Get all class items for this class
326
381
const classDocName = classDoc . name . startsWith ( 'p5.' ) ? classDoc . name . substring ( 3 ) : classDoc . name ;
382
+
327
383
const classItems = organizedData . classitems . filter ( item => item . class === classDocName ) ;
328
384
329
385
// Separate static and instance members
330
386
const staticItems = classItems . filter ( item => item . isStatic ) ;
331
-
332
387
const instanceItems = classItems . filter ( item => ! item . isStatic ) ;
333
388
334
- // Add static methods first
389
+ // Generate static members
335
390
staticItems . forEach ( item => {
336
-
337
- if ( item . description ) {
338
- output += ' /**\n' ;
339
- const itemDesc = extractDescription ( item . description ) ;
340
- output += formatJSDocComment ( itemDesc , 2 ) + '\n' ;
341
- if ( item . params ?. length > 0 ) {
342
- output += ' *\n' ;
343
- item . params . forEach ( param => {
344
- const paramDesc = extractDescription ( param . description ) ;
345
- output += formatJSDocComment ( `@param ${ paramDesc } ` , 2 ) + '\n' ;
346
- } ) ;
347
- }
348
- if ( item . returns ) {
349
- output += ' *\n' ;
350
- const returnDesc = extractDescription ( item . returns [ 0 ] ?. description ) ;
351
- output += formatJSDocComment ( `@return ${ returnDesc } ` , 2 ) + '\n' ;
352
- }
353
- output += ' */\n' ;
354
- }
355
-
356
- if ( item . kind === 'function' ) {
357
- // Handle function overloads
358
- if ( item . overloads ) {
359
- item . overloads . forEach ( overload => {
360
- const params = ( overload . params || [ ] )
361
- . map ( param => generateParamDeclaration ( param ) )
362
- . join ( ', ' ) ;
363
- const returnType = overload . returns ?. [ 0 ] ?. type
364
- ? generateTypeFromTag ( overload . returns [ 0 ] )
365
- : 'void' ;
366
- output += ` static ${ item . name } (${ params } ): ${ returnType } ;\n` ;
367
- } ) ;
368
- output += '\n' ;
369
- } else {
370
- const params = ( item . params || [ ] )
371
- . map ( param => generateParamDeclaration ( param ) )
372
- . join ( ', ' ) ;
373
- output += ` static ${ item . name } (${ params } ): ${ item . returnType } ;\n\n` ;
374
- }
375
- } else {
376
- output += ` static ${ item . name } : ${ item . returnType } ;\n\n` ;
377
- }
391
+ output += generateMethodDeclarations ( item , true ) ;
378
392
} ) ;
379
393
380
- // Add instance members
394
+ // Generate instance members
381
395
instanceItems . forEach ( item => {
382
- if ( item . description ) {
383
- output += ' /**\n' ;
384
- const itemDesc = extractDescription ( item . description ) ;
385
- output += formatJSDocComment ( itemDesc , 2 ) + '\n' ;
386
- if ( item . params ?. length > 0 ) {
387
- output += ' *\n' ;
388
- item . params . forEach ( param => {
389
- const paramDesc = extractDescription ( param . description ) ;
390
- output += formatJSDocComment ( `@param ${ paramDesc } ` , 2 ) + '\n' ;
391
- } ) ;
392
- }
393
- if ( item . returns ) {
394
- output += ' *\n' ;
395
- const returnDesc = extractDescription ( item . returns [ 0 ] ?. description ) ;
396
- output += formatJSDocComment ( `@return ${ returnDesc } ` , 2 ) + '\n' ;
397
- }
398
- output += ' */\n' ;
399
- }
400
-
401
- if ( item . kind === 'function' ) {
402
- // Handle function overloads
403
- if ( item . overloads ) {
404
- item . overloads . forEach ( overload => {
405
- const params = ( overload . params || [ ] )
406
- . map ( param => generateParamDeclaration ( param ) )
407
- . join ( ', ' ) ;
408
- const returnType = overload . returns ?. [ 0 ] ?. type
409
- ? generateTypeFromTag ( overload . returns [ 0 ] )
410
- : 'void' ;
411
- output += ` ${ item . name } (${ params } ): ${ returnType } ;\n` ;
412
- } ) ;
413
- output += '\n' ;
414
- } else {
415
- const params = ( item . params || [ ] )
416
- . map ( param => generateParamDeclaration ( param ) )
417
- . join ( ', ' ) ;
418
- output += ` ${ item . name } (${ params } ): ${ item . returnType } ;\n\n` ;
419
- }
420
- } else {
421
- output += ` ${ item . name } : ${ item . returnType } ;\n\n` ;
422
- }
396
+ output += generateMethodDeclarations ( item , false ) ;
423
397
} ) ;
424
398
425
399
output += '}\n\n' ;
@@ -468,29 +442,68 @@ function generateDeclarationFile(items, filePath, organizedData) {
468
442
// Begin module declaration
469
443
output += `declare module '${ moduleName } ' {\n` ;
470
444
471
- // Add all item declarations
445
+ // Find the class documentation if it exists
446
+ const classDoc = items . find ( item => item . kind === 'class' ) ;
447
+ if ( classDoc ) {
448
+ const classDocName = classDoc . name . startsWith ( 'p5.' ) ? classDoc . name . substring ( 3 ) : classDoc . name ;
449
+
450
+ // Start class declaration
451
+ output += ` class ${ classDocName } {\n` ;
452
+
453
+ // Add constructor if there are parameters
454
+ if ( classDoc . params ?. length > 0 ) {
455
+ output += ' constructor(' ;
456
+ output += classDoc . params
457
+ . map ( param => generateParamDeclaration ( param ) )
458
+ . join ( ', ' ) ;
459
+ output += ');\n\n' ;
460
+ }
461
+
462
+ // Get all members that belong to this class
463
+ const classItems = organizedData . classitems . filter ( item =>
464
+ item . memberof === classDoc . name || item . class === classDocName
465
+ ) ;
466
+
467
+ // Separate static and instance members
468
+ const staticItems = classItems . filter ( item => item . isStatic ) ;
469
+ const instanceItems = classItems . filter ( item => ! item . isStatic ) ;
470
+
471
+ // Generate static members
472
+ staticItems . forEach ( item => {
473
+ output += generateMethodDeclarations ( item , true ) ;
474
+ } ) ;
475
+
476
+ // Generate instance members
477
+ instanceItems . forEach ( item => {
478
+ output += generateMethodDeclarations ( item , false ) ;
479
+ } ) ;
480
+
481
+ // Close class declaration
482
+ output += ' }\n\n' ;
483
+ }
484
+
485
+ // Add remaining items that aren't part of the class
472
486
items . forEach ( item => {
473
- switch ( item . kind ) {
474
- case 'class' :
475
- output += generateClassDeclaration ( item , organizedData ) ;
476
- break ;
477
- case 'function' :
478
- output += generateFunctionDeclaration ( item ) ;
479
- break ;
480
- case 'constant' :
481
- case 'typedef' :
482
- const constData = organizedData . consts [ item . name ] ;
483
- if ( constData ) {
484
- if ( constData . description ) {
485
- output += ` /**\n * ${ constData . description } \n */\n` ;
487
+ if ( item . kind !== 'class' && ( ! item . memberof || item . memberof !== classDoc ?. name ) ) {
488
+ switch ( item . kind ) {
489
+ case 'function' :
490
+ output += generateFunctionDeclaration ( item ) ;
491
+ break ;
492
+ case 'constant' :
493
+ case 'typedef' :
494
+ const constData = organizedData . consts [ item . name ] ;
495
+ if ( constData ) {
496
+ if ( constData . description ) {
497
+ output += ` /**\n * ${ constData . description } \n */\n` ;
498
+ }
499
+ if ( constData . kind === 'constant' ) {
500
+ output += ` const ${ constData . name } : ${ constData . type } ;\n\n` ;
501
+ } else {
502
+ output += ` type ${ constData . name } = ${ constData . type } ;\n\n` ;
503
+ }
486
504
}
487
- if ( constData . kind === 'constant' ) {
488
- output += ` const ${ constData . name } : ${ constData . type } ;\n\n` ;
489
- } else {
490
- output += ` type ${ constData . name } = ${ constData . type } ;\n\n` ;
491
- }
492
- }
493
- break ;
505
+ break ;
506
+ }
494
507
}
495
508
} ) ;
496
509
@@ -539,7 +552,7 @@ function generateAllDeclarationFiles() {
539
552
) ;
540
553
541
554
// Generate the declaration file content
542
- const declarationContent = generateDeclarationFile ( items , filePath , organized ) ;
555
+ const declarationContent = generateDeclarationFile ( items , filePath , organizedData ) ;
543
556
544
557
// Create directory if it doesn't exist
545
558
fs . mkdirSync ( path . dirname ( dtsPath ) , { recursive : true } ) ;
0 commit comments