Skip to content

Commit b751bc1

Browse files
committed
Added Extends case for classes
1 parent 9671403 commit b751bc1

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

utils/convert.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,5 @@ fs.mkdirSync(path.join(__dirname, '../docs/reference'), { recursive: true });
615615
fs.writeFileSync(path.join(__dirname, '../docs/reference/data.json'), JSON.stringify(converted, null, 2));
616616
fs.writeFileSync(path.join(__dirname, '../docs/reference/data.min.json'), JSON.stringify(converted));
617617
buildParamDocs(JSON.parse(JSON.stringify(converted)));
618+
619+
module.exports= { getAllEntries };

utils/generate-types.js

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const { getAllEntries } = require("./convert");
34

45
// Read docs.json
56
const data = JSON.parse(fs.readFileSync(path.join(__dirname, '../docs/data.json')));
67

7-
// Flatten and organize data structure
8-
function getEntries(entry) {
9-
if (!entry) return [];
10-
if (!entry.members) return [entry];
11-
12-
return [
13-
entry,
14-
...getAllEntries(entry.members.global || []),
15-
...getAllEntries(entry.members.inner || []),
16-
...getAllEntries(entry.members.instance || []),
17-
...getAllEntries(entry.members.events || []),
18-
...getAllEntries(entry.members.static || [])
19-
];
20-
}
21-
22-
function getAllEntries(arr) {
23-
return arr.flatMap(getEntries);
24-
}
258
const organized = {
269
modules: {},
2710
classes: {},
2811
classitems: [],
2912
consts: {}
3013
};
3114

15+
// Add this helper function at the top with other helpers
16+
function normalizeClassName(className) {
17+
if (!className || className === 'p5') return 'p5';
18+
return className.startsWith('p5.') ? className : `p5.${className}`;
19+
}
20+
3221
// Organize data into structured format
3322
function organizeData(data) {
3423
const allData = getAllEntries(data);
@@ -54,6 +43,8 @@ function organizeData(data) {
5443
if (entry.kind === 'class') {
5544
const { module, submodule } = getModuleInfo(entry);
5645
const className = entry.name;
46+
const extendsTag = entry.tags?.find(tag => tag.title === 'extends');
47+
5748
organized.classes[className] = {
5849
name: className,
5950
description: extractDescription(entry.description),
@@ -63,7 +54,8 @@ function organizeData(data) {
6354
optional: param.type?.type === 'OptionalType'
6455
})),
6556
module,
66-
submodule
57+
submodule,
58+
extends: extendsTag?.name || null
6759
};
6860
}
6961
});
@@ -72,11 +64,20 @@ function organizeData(data) {
7264
allData.forEach(entry => {
7365
if (entry.kind === 'function' || entry.kind === 'property') {
7466
const { module, submodule, forEntry } = getModuleInfo(entry);
67+
68+
// Normalize memberof and forEntry
69+
let memberof = entry.memberof;
70+
if (memberof && memberof !== 'p5' && !memberof.startsWith('p5.')) {
71+
memberof = 'p5.' + memberof;
72+
}
73+
74+
let normalizedForEntry = forEntry;
75+
if (forEntry && forEntry !== 'p5' && !forEntry.startsWith('p5.')) {
76+
normalizedForEntry = 'p5.' + forEntry;
77+
}
78+
7579
// Use memberof if available, fallback to forEntry, then default to 'p5'
76-
const className = entry.memberof || forEntry || 'p5';
77-
78-
// Create the class entry if it doesn't exist
79-
// if (!organized.classes[className]) {console.log(`returning for ${className}`); return};
80+
const className = normalizeClassName(memberof || normalizedForEntry || 'p5');
8081

8182
const isStatic = entry.path?.[0]?.scope === 'static';
8283
// Handle overloads
@@ -356,18 +357,28 @@ function generateClassDeclaration(classDoc, organizedData) {
356357
}
357358
if (classDoc.tags) {
358359
if (description) {
359-
output += ' *\n'; // Add separator between description and tags
360+
output += ' *\n';
360361
}
361362
classDoc.tags.forEach(tag => {
362363
if (tag.description) {
363364
const tagDesc = extractDescription(tag.description);
365+
364366
output += formatJSDocComment(`@${tag.title} ${tagDesc}`, 0) + '\n';
365367
}
366368
});
367369
}
368370
output += ' */\n';
369371
}
370372

373+
// Get the parent class if it exists
374+
const parentClass = classDoc.extends;
375+
const extendsClause = parentClass ? ` extends ${parentClass}` : '';
376+
377+
// Start class declaration with inheritance if applicable
378+
const fullClassName = normalizeClassName(classDoc.name);
379+
const classDocName = fullClassName.replace('p5.', '');
380+
output += `class ${classDocName}${extendsClause} {\n`;
381+
371382
// Add constructor if there are parameters
372383
if (classDoc.params?.length > 0) {
373384
output += ' constructor(';
@@ -378,9 +389,10 @@ function generateClassDeclaration(classDoc, organizedData) {
378389
}
379390

380391
// Get all class items for this class
381-
const classDocName = classDoc.name.startsWith('p5.') ? classDoc.name.substring(3) : classDoc.name;
382-
383-
const classItems = organizedData.classitems.filter(item => item.class === classDocName);
392+
const classItems = organizedData.classitems.filter(item =>
393+
item.class === fullClassName ||
394+
item.class === fullClassName.replace('p5.', '')
395+
);
384396

385397
// Separate static and instance members
386398
const staticItems = classItems.filter(item => item.isStatic);
@@ -445,10 +457,16 @@ function generateDeclarationFile(items, filePath, organizedData) {
445457
// Find the class documentation if it exists
446458
const classDoc = items.find(item => item.kind === 'class');
447459
if (classDoc) {
448-
const classDocName = classDoc.name.startsWith('p5.') ? classDoc.name.substring(3) : classDoc.name;
460+
const fullClassName = normalizeClassName(classDoc.name);
461+
const classDocName = fullClassName.replace('p5.', '');
462+
let parentClass = classDoc.tags?.find(tag => tag.title === 'extends')?.name;
463+
if (parentClass) {
464+
parentClass = parentClass.replace('p5.', '');
465+
}
466+
const extendsClause = parentClass ? ` extends ${parentClass}` : '';
449467

450468
// Start class declaration
451-
output += ` class ${classDocName} {\n`;
469+
output += ` class ${classDocName}${extendsClause} {\n`;
452470

453471
// Add constructor if there are parameters
454472
if (classDoc.params?.length > 0) {
@@ -458,27 +476,27 @@ function generateDeclarationFile(items, filePath, organizedData) {
458476
.join(', ');
459477
output += ');\n\n';
460478
}
461-
462-
// Get all members that belong to this class
479+
480+
// Get all class items for this class
463481
const classItems = organizedData.classitems.filter(item =>
464-
item.memberof === classDoc.name || item.class === classDocName
482+
item.class === fullClassName ||
483+
item.class === fullClassName.replace('p5.', '')
465484
);
466485

467486
// Separate static and instance members
468487
const staticItems = classItems.filter(item => item.isStatic);
469488
const instanceItems = classItems.filter(item => !item.isStatic);
470-
489+
471490
// Generate static members
472491
staticItems.forEach(item => {
473492
output += generateMethodDeclarations(item, true);
474493
});
475-
494+
476495
// Generate instance members
477496
instanceItems.forEach(item => {
478497
output += generateMethodDeclarations(item, false);
479498
});
480-
481-
// Close class declaration
499+
482500
output += ' }\n\n';
483501
}
484502

0 commit comments

Comments
 (0)