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

Function generateMethodDeclaration

utils/typescript.mjs:582–636  ·  view source on GitHub ↗
(method, options = {})

Source from the content-addressed store, hash-verified

580}
581
582function generateMethodDeclaration(method, options = {}) {
583 let output = '';
584 const { globalFunction = false } = options;
585
586 const indent = globalFunction ? '' : ' ';
587 const commentIndent = globalFunction ? 0 : 2;
588
589 if (method.description) {
590 output += `${indent}/**\n`;
591 output += formatJSDocComment(method.description, commentIndent) + '\n';
592
593 // Add param docs from first overload
594 if (method.overloads?.[0]?.params) {
595 method.overloads[0].params.forEach(param => {
596 if (param.description) {
597 output += formatJSDocComment(`@param ${param.name} ${param.description}`, commentIndent) + '\n';
598 }
599 });
600 }
601
602 // Add return docs
603 if (method.return?.description) {
604 output += formatJSDocComment(`@returns ${method.return.description}`, commentIndent) + '\n';
605 }
606
607 output += `${indent} */\n`;
608 }
609
610 const staticPrefix = method.static ? 'static ' : '';
611 const declarationPrefix = globalFunction ? 'function ' : `${indent}${staticPrefix}`;
612
613 // Generate overload declarations
614 if (method.overloads && method.overloads.length > 0) {
615 method.overloads.forEach(overload => {
616 const params = (overload.params || [])
617 .map(param => generateParamDeclaration(param, options, overload.params))
618 .join(', ');
619
620 let returnType = 'void';
621 if (overload.chainable && !globalFunction && options.currentClass !== 'p5') {
622 returnType = options.currentClass || 'this';
623 // TODO: Decide what should be chainable. Many of these are accidental / not thought through
624 } else if (overload.return && overload.return.type) {
625 returnType = convertTypeToTypeScript(overload.return.type, options);
626 } else if (method.return && method.return.type) {
627 returnType = convertTypeToTypeScript(method.return.type, options);
628 }
629
630 output += `${declarationPrefix}${method.name}(${params}): ${returnType};\n`;
631 });
632 }
633
634 output += '\n';
635 return output;
636}
637
638function generateClassDeclaration(classData) {
639 let output = '';

Callers 2

generateClassDeclarationFunction · 0.85
generateTypeDefinitionsFunction · 0.85

Calls 3

formatJSDocCommentFunction · 0.85
generateParamDeclarationFunction · 0.85
convertTypeToTypeScriptFunction · 0.85

Tested by

no test coverage detected