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

Function convertTypeToTypeScript

utils/typescript.mjs:197–346  ·  view source on GitHub ↗
(typeNode, options = {})

Source from the content-addressed store, hash-verified

195
196// TypeScript-specific type conversion from raw type objects
197function convertTypeToTypeScript(typeNode, options = {}) {
198 if (!typeNode) return 'any';
199
200 // Validate that typeNode is always an object
201 if (typeof typeNode !== 'object' || Array.isArray(typeNode)) {
202 throw new Error(`convertTypeToTypeScript expects an object, got: ${typeof typeNode} - ${JSON.stringify(typeNode)}`);
203 }
204
205 const { currentClass = null, isInsideNamespace = false, inGlobalMode = false, isConstantDef = false } = options;
206
207 switch (typeNode.type) {
208 case 'NameExpression': {
209 const typeName = typeNode.name;
210
211 // Handle primitive types
212 const primitiveTypes = {
213 'String': 'string',
214 'Number': 'number',
215 'Integer': 'number',
216 'Boolean': 'boolean',
217 'Void': 'void',
218 'Object': 'object',
219 'Any': 'any',
220 'Array': 'any[]',
221 'Promise': 'Promise<any>',
222 'Function': 'Function',
223 'HTMLElement': 'HTMLElement',
224 'Event': 'Event',
225 'Request': 'Request'
226 };
227
228 if (primitiveTypes[typeName]) {
229 return primitiveTypes[typeName];
230 }
231
232 // Handle self-referential types within the same class
233 if (currentClass && (typeName === `p5.${currentClass}` || typeName === currentClass)) {
234 return currentClass;
235 }
236
237 // If we're inside the p5 namespace, remove p5. prefix from other p5 classes
238 if (isInsideNamespace && typeName.startsWith('p5.')) {
239 if (inGlobalMode) {
240 return 'P5.' + typeName.substring(3);
241 } else {
242 return typeName.substring(3);
243 }
244 }
245
246 // Check if this is a p5 constant/typedef
247 if (constantsLookup.has(typeName)) {
248 const typedefEntry = typedefs[typeName];
249
250 // Use interface name for object-shaped typedefs in all contexts
251 if (typedefEntry && hasTypedefProperties(typedefEntry)) {
252 if (inGlobalMode) {
253 return `P5.${typeName}`;
254 } else if (isInsideNamespace) {

Callers 7

generateTypedefInterfaceFunction · 0.85
generateObjectInterfaceFunction · 0.85
generateParamDeclarationFunction · 0.85
generateClassDeclarationFunction · 0.85
generateTypeDefinitionsFunction · 0.85

Calls 1

hasTypedefPropertiesFunction · 0.85

Tested by

no test coverage detected