Skip to content

Commit 9671403

Browse files
committed
fix data types and class declarations
1 parent c4deacf commit 9671403

File tree

1 file changed

+129
-116
lines changed

1 file changed

+129
-116
lines changed

utils/generate-types.js

Lines changed: 129 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ function organizeData(data) {
7777

7878
// Create the class entry if it doesn't exist
7979
// 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+
8381
const isStatic = entry.path?.[0]?.scope === 'static';
8482
// Handle overloads
8583
const overloads = entry.overloads?.map(overload => ({
@@ -122,7 +120,7 @@ function organizeData(data) {
122120
};
123121
}
124122
fs.writeFileSync("./consts.json", JSON.stringify(organized.consts, null, 2), 'utf8');
125-
});
123+
});
126124

127125
return organized;
128126
}
@@ -193,11 +191,12 @@ function normalizeTypeName(type) {
193191
const primitiveTypes = {
194192
'String': 'string',
195193
'Number': 'number',
194+
'Integer': 'number',
196195
'Boolean': 'boolean',
197196
'Void': 'void',
198197
'Object': 'object',
199-
'Array': 'array',
200-
'Function': 'function'
198+
'Array': 'Array',
199+
'Function': 'Function'
201200
};
202201

203202
return primitiveTypes[type] || type;
@@ -226,8 +225,10 @@ function generateTypeFromTag(param) {
226225
return 'any';
227226
case 'RecordType':
228227
return 'object';
229-
case 'ObjectType':
230-
return 'object';
228+
case 'StringLiteralType':
229+
return `'${param.type.value}'`;
230+
case 'UndefinedLiteralType':
231+
return 'undefined';
231232
default:
232233
return 'any';
233234
}
@@ -288,6 +289,60 @@ function generateFunctionDeclaration(funcDoc) {
288289
return output;
289290
}
290291

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+
291346
// Generate class declaration
292347
function generateClassDeclaration(classDoc, organizedData) {
293348
let output = '';
@@ -324,102 +379,21 @@ function generateClassDeclaration(classDoc, organizedData) {
324379

325380
// Get all class items for this class
326381
const classDocName = classDoc.name.startsWith('p5.') ? classDoc.name.substring(3) : classDoc.name;
382+
327383
const classItems = organizedData.classitems.filter(item => item.class === classDocName);
328384

329385
// Separate static and instance members
330386
const staticItems = classItems.filter(item => item.isStatic);
331-
332387
const instanceItems = classItems.filter(item => !item.isStatic);
333388

334-
// Add static methods first
389+
// Generate static members
335390
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);
378392
});
379393

380-
// Add instance members
394+
// Generate instance members
381395
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);
423397
});
424398

425399
output += '}\n\n';
@@ -468,29 +442,68 @@ function generateDeclarationFile(items, filePath, organizedData) {
468442
// Begin module declaration
469443
output += `declare module '${moduleName}' {\n`;
470444

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
472486
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+
}
486504
}
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+
}
494507
}
495508
});
496509

@@ -539,7 +552,7 @@ function generateAllDeclarationFiles() {
539552
);
540553

541554
// Generate the declaration file content
542-
const declarationContent = generateDeclarationFile(items, filePath, organized);
555+
const declarationContent = generateDeclarationFile(items, filePath, organizedData);
543556

544557
// Create directory if it doesn't exist
545558
fs.mkdirSync(path.dirname(dtsPath), { recursive: true });

0 commit comments

Comments
 (0)