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

Function generateTypedefInterface

utils/typescript.mjs:396–445  ·  view source on GitHub ↗
(name, typedefEntry, options = {}, indent = 2)

Source from the content-addressed store, hash-verified

394
395// Generate a TypeScript interface from a typedef with @property fields
396function generateTypedefInterface(name, typedefEntry, options = {}, indent = 2) {
397 const pad = ' '.repeat(indent);
398 const innerPad = ' '.repeat(indent + 2);
399 let output = '';
400
401 if (typedefEntry.description) {
402 const descStr = typeof typedefEntry.description === 'string'
403 ? typedefEntry.description
404 : descriptionStringForTypeScript(typedefEntry.description);
405 if (descStr) {
406 output += `${pad}/**\n`;
407 output += formatJSDocComment(descStr, indent) + '\n';
408 output += `${pad} */\n`;
409 }
410 }
411
412 output += `${pad}interface ${name} {\n`;
413
414 for (const prop of typedefEntry.properties) {
415 // Each prop: { name, type, description, optional }
416 const propName = prop.name;
417 const rawType = prop.type;
418 const isOptional = prop.optional || rawType?.type === 'OptionalType';
419 const optMark = isOptional ? '?' : '';
420
421 if (prop.description) {
422 const propDescStr = typeof prop.description === 'string'
423 ? prop.description.trim()
424 : descriptionStringForTypeScript(prop.description);
425 if (propDescStr) {
426 output += `${innerPad}/** ${propDescStr} */\n`;
427 }
428 }
429
430 if (rawType?.type === 'FunctionType') {
431 // Render FunctionType properties as method signatures instead of arrow properties
432 const sig = convertFunctionTypeForInterface(rawType, options);
433 const arrowIdx = sig.lastIndexOf('=>');
434 const paramsPart = sig.substring(0, arrowIdx).trim();
435 const retPart = sig.substring(arrowIdx + 2).trim();
436 output += `${innerPad}${propName}${paramsPart}: ${retPart};\n`;
437 } else {
438 const tsType = rawType ? convertTypeToTypeScript(rawType, options) : 'any';
439 output += `${innerPad}${propName}${optMark}: ${tsType};\n`;
440 }
441 }
442
443 output += `${pad}}\n\n`;
444 return output;
445}
446
447// Strategy for TypeScript output
448const typescriptStrategy = {

Callers 1

generateTypeDefinitionsFunction · 0.85

Calls 5

formatJSDocCommentFunction · 0.85
convertTypeToTypeScriptFunction · 0.85
trimMethod · 0.80

Tested by

no test coverage detected