MCPcopy Index your code
hub / github.com/simstudioai/sim / resolveConstVariable

Function resolveConstVariable

scripts/generate-docs.ts:3081–3123  ·  view source on GitHub ↗

* Resolve a module-level `const varName = { ... }` declaration. * Handles nested spreads of other const variables (but not property-access values). * Used to expand variable spreads inside builder function return bodies.

(
  varName: string,
  primaryContent: string,
  utilsContent: string,
  depth = 0
)

Source from the content-addressed store, hash-verified

3079 * Used to expand variable spreads inside builder function return bodies.
3080 */
3081function resolveConstVariable(
3082 varName: string,
3083 primaryContent: string,
3084 utilsContent: string,
3085 depth = 0
3086): Record<string, any> {
3087 if (depth > 8) return {}
3088
3089 // Match `const varName = {` (with optional type annotation)
3090 const varRegex = new RegExp(`(?<![.\\w])const\\s+${varName}\\s*(?::[^=]+)?=\\s*\\{`)
3091
3092 for (const content of [primaryContent, utilsContent]) {
3093 const varMatch = varRegex.exec(content)
3094 if (!varMatch) continue
3095
3096 const openBrace = content.indexOf('{', varMatch.index + varMatch[0].length - 1)
3097 if (openBrace === -1) continue
3098
3099 const closeBrace = findMatchingClose(content, openBrace)
3100 if (closeBrace === -1) continue
3101
3102 const varBody = content.substring(openBrace + 1, closeBrace - 1).trim()
3103 const result: Record<string, any> = {}
3104
3105 // Resolve nested variable spreads within this const (no parens = variable reference)
3106 const nestedSpreadRegex = /\.\.\.\s*([a-zA-Z_]\w*)\b(?!\s*\()/g
3107 let nestedMatch: RegExpExecArray | null
3108 while ((nestedMatch = nestedSpreadRegex.exec(varBody)) !== null) {
3109 const nested = resolveConstVariable(nestedMatch[1], primaryContent, utilsContent, depth + 1)
3110 Object.assign(result, nested)
3111 }
3112
3113 // Parse any inline `field: { type, description }` definitions
3114 // (strip spread lines first; property-access values like `foo: bar.baz` are skipped by parser)
3115 const bodyWithoutVarSpreads = varBody.replace(/\.\.\.\s*\w+\b(?!\s*\()\s*,?\s*/g, '')
3116 const inlineOutputs = parseToolOutputsField(bodyWithoutVarSpreads)
3117 Object.assign(result, inlineOutputs)
3118
3119 return result
3120 }
3121
3122 return {}
3123}
3124
3125/**
3126 * Recursively resolve a trigger output builder function.

Callers 1

Calls 3

findMatchingCloseFunction · 0.85
parseToolOutputsFieldFunction · 0.85
replaceMethod · 0.65

Tested by

no test coverage detected