MCPcopy Index your code
hub / github.com/processing/p5.js / generateTypeDefinitions

Function generateTypeDefinitions

utils/typescript.mjs:709–944  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

707
708// Generate TypeScript definitions
709function generateTypeDefinitions() {
710 let output = '// This file is auto-generated from JSDoc documentation\n\n';
711
712 // First, define all constants at the top level with their actual values
713 const seenConstants = new Set();
714 const p5Constants = processed.classitems.filter(item => {
715 if (item.class === 'p5' && item.itemtype === 'property' && item.name in processed.consts) {
716 // Skip defineProperty, undefined and avoid duplicates
717 if (item.name === 'defineProperty' || !item.name) {
718 return false;
719 }
720 if (seenConstants.has(item.name)) {
721 return false;
722 }
723 // Skip typedefs that have real object shapes
724 if (typedefs[item.name] && hasTypedefProperties(typedefs[item.name])) {
725 return false;
726 }
727 seenConstants.add(item.name);
728 return true;
729 }
730 return false;
731 });
732
733 p5Constants.forEach(constant => {
734 if (constant.description) {
735 output += '/**\n';
736 output += formatJSDocComment(constant.description, 0) + '\n';
737 output += ' */\n';
738 }
739 const type = convertTypeToTypeScript(constant.type, { isInsideNamespace: false, isConstantDef: true });
740 const isMutable = mutableProperties.has(constant.name);
741 const declaration = isMutable ? 'declare let' : 'declare const';
742 output += `${declaration} ${constant.name}: ${type};\n\n`;
743 // Duplicate with a private identifier so we can re-export in the namespace later
744 output += `${declaration} __${constant.name}: typeof ${constant.name};\n\n`;
745 });
746
747 // Generate main p5 class
748 output += 'declare class p5 {\n';
749 output += ' constructor(sketch?: (p: p5) => void, node?: HTMLElement, sync?: boolean);\n\n';
750
751 const p5Options = { currentClass: 'p5', isInsideNamespace: false };
752
753 // Generate p5 static methods
754 const p5StaticMethods = Object.values(processed.classMethods.p5 || {}).filter(method => method.static);
755 p5StaticMethods.forEach(method => {
756 output += generateMethodDeclaration(method, p5Options);
757 });
758
759 // Generate p5 instance methods
760 const p5InstanceMethods = Object.values(processed.classMethods.p5 || {}).filter(method => !method.static);
761 p5InstanceMethods.forEach(method => {
762 output += generateMethodDeclaration(method, p5Options);
763 });
764
765 // Add strands functions to p5 instance
766 const strandsMethods = processStrandsFunctions();

Callers 1

typescript.mjsFile · 0.85

Calls 9

hasTypedefPropertiesFunction · 0.85
formatJSDocCommentFunction · 0.85
convertTypeToTypeScriptFunction · 0.85
processStrandsFunctionsFunction · 0.85
generateTypedefInterfaceFunction · 0.85
generateClassDeclarationFunction · 0.85
filterMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected