* Resolve a SubBlockConfig builder function to its field definitions. * Handles `return [...]`, `return {...}`, and `blocks.push(...)` patterns. * Searches `utilsContent` first, then `primaryContent`.
( funcName: string, utilsContent: string, primaryContent?: string )
| 3331 | * Searches `utilsContent` first, then `primaryContent`. |
| 3332 | */ |
| 3333 | function resolveSubBlockBuilderFunction( |
| 3334 | funcName: string, |
| 3335 | utilsContent: string, |
| 3336 | primaryContent?: string |
| 3337 | ): TriggerConfigField[] { |
| 3338 | const UI_ONLY_IDS = new Set(['webhookUrlDisplay', 'triggerInstructions', 'selectedTriggerId']) |
| 3339 | |
| 3340 | for (const content of [utilsContent, primaryContent ?? '']) { |
| 3341 | if (!content) continue |
| 3342 | const funcRegex = new RegExp(`(?:export\\s+)?function\\s+${funcName}\\s*\\(`) |
| 3343 | const funcMatch = funcRegex.exec(content) |
| 3344 | if (!funcMatch) continue |
| 3345 | |
| 3346 | // Find the closing ')' of the parameter list, then the '{' that opens the function body. |
| 3347 | // Using just indexOf('{') would pick up '{' inside object-type parameters. |
| 3348 | const openParen = content.indexOf('(', funcMatch.index) |
| 3349 | if (openParen === -1) continue |
| 3350 | const closeParen = findMatchingClose(content, openParen, '(', ')') |
| 3351 | if (closeParen === -1) continue |
| 3352 | const bodyStart = content.indexOf('{', closeParen) |
| 3353 | if (bodyStart === -1) continue |
| 3354 | const bodyEnd = findMatchingClose(content, bodyStart) |
| 3355 | if (bodyEnd === -1) continue |
| 3356 | |
| 3357 | const funcBody = content.substring(bodyStart + 1, bodyEnd - 1) |
| 3358 | |
| 3359 | // Pattern 1: `return [...]` |
| 3360 | const returnArrayMatch = /\breturn\s*\[/.exec(funcBody) |
| 3361 | if (returnArrayMatch) { |
| 3362 | const arrayStart = funcBody.indexOf('[', returnArrayMatch.index) |
| 3363 | const arrayEnd = findMatchingClose(funcBody, arrayStart, '[', ']') |
| 3364 | if (arrayEnd !== -1) { |
| 3365 | return parseSubBlockArrayContent( |
| 3366 | funcBody.substring(arrayStart + 1, arrayEnd - 1), |
| 3367 | UI_ONLY_IDS, |
| 3368 | content |
| 3369 | ) |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | // Pattern 2: `return { ... }` (single object) |
| 3374 | const returnObjMatch = /\breturn\s*\{/.exec(funcBody) |
| 3375 | if (returnObjMatch) { |
| 3376 | const objStart = funcBody.indexOf('{', returnObjMatch.index) |
| 3377 | const objEnd = findMatchingClose(funcBody, objStart) |
| 3378 | if (objEnd !== -1) { |
| 3379 | const field = parseSubBlockObject( |
| 3380 | funcBody.substring(objStart, objEnd), |
| 3381 | UI_ONLY_IDS, |
| 3382 | content |
| 3383 | ) |
| 3384 | return field ? [field] : [] |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | // Pattern 3: `blocks.push({...})` |
| 3389 | const pushFields: TriggerConfigField[] = [] |
| 3390 | const pushRegex = /\bblocks\.push\s*\(/g |
no test coverage detected