* Recursively iterate over an object and call a function for each leaf node. * Leaf node paths are dot-separated (e.g. "background.light").
( obj: Record<string, any>, fn: (path: string, value: any) => void, prefix = '', depth = 0, findings?: Finding[], rootPath?: string )
| 410 | * Leaf node paths are dot-separated (e.g. "background.light"). |
| 411 | */ |
| 412 | function forEachLeaf( |
| 413 | obj: Record<string, any>, |
| 414 | fn: (path: string, value: any) => void, |
| 415 | prefix = '', |
| 416 | depth = 0, |
| 417 | findings?: Finding[], |
| 418 | rootPath?: string |
| 419 | ) { |
| 420 | if (depth > MAX_TOKEN_NESTING_DEPTH) { |
| 421 | if (findings && rootPath) { |
| 422 | // Check if we've already reported this rootPath to avoid spamming |
| 423 | if (!findings.some((f) => f.path === rootPath && f.message.includes('nesting depth'))) { |
| 424 | findings.push({ |
| 425 | severity: 'error', |
| 426 | path: rootPath, |
| 427 | message: `Token nesting depth exceeds maximum allowed depth of ${MAX_TOKEN_NESTING_DEPTH}.`, |
| 428 | }); |
| 429 | } |
| 430 | } |
| 431 | return; |
| 432 | } |
| 433 | for (const [key, value] of Object.entries(obj)) { |
| 434 | const fullPath = prefix ? `${prefix}.${key}` : key; |
| 435 | if (value !== null && typeof value === 'object' && !Array.isArray(value)) { |
| 436 | forEachLeaf(value, fn, fullPath, depth + 1, findings, rootPath); |
| 437 | } else { |
| 438 | fn(fullPath, value); |
| 439 | } |
| 440 | } |
| 441 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…