Walk `keys` into the object literal opening at `at`; the final value's source text, or null.
(text: string, at: number, keys: string[])
| 390 | |
| 391 | /** Walk `keys` into the object literal opening at `at`; the final value's source text, or null. */ |
| 392 | function readObjectValue(text: string, at: number, keys: string[]): string | null { |
| 393 | const skipString = (j: number): number => { |
| 394 | const quote = text[j]!; |
| 395 | for (j++; j < text.length && text[j] !== quote; j++) if (text[j] === '\\') j++; |
| 396 | return j + 1; |
| 397 | }; |
| 398 | const skipValue = (j: number): number => { |
| 399 | let depth = 0; |
| 400 | for (; j < text.length; j++) { |
| 401 | const ch = text[j]!; |
| 402 | if (ch === '"' || ch === "'" || ch === '`') { j = skipString(j) - 1; continue; } |
| 403 | if (ch === '{' || ch === '[' || ch === '(') depth++; |
| 404 | else if (ch === '}' || ch === ']' || ch === ')') { if (depth === 0) return j; depth--; } |
| 405 | else if (ch === ',' && depth === 0) return j; |
| 406 | } |
| 407 | return j; |
| 408 | }; |
| 409 | let i = at + 1; |
| 410 | while (i < text.length) { |
| 411 | const m = /^\s*(?:([A-Za-z_$][\w$]*)|["']([^"']+)["'])\s*:\s*/.exec(text.slice(i)); |
| 412 | if (!m) { |
| 413 | const next = skipValue(i); |
| 414 | if (text[next] !== ',') return null; |
| 415 | i = next + 1; |
| 416 | continue; |
| 417 | } |
| 418 | const key = m[1] ?? m[2]!; |
| 419 | const valueAt = i + m[0].length; |
| 420 | const end = skipValue(valueAt); |
| 421 | if (key === keys[0]) { |
| 422 | if (keys.length === 1) return text.slice(valueAt, end).trim(); |
| 423 | return text[valueAt] === '{' ? readObjectValue(text, valueAt, keys.slice(1)) : null; |
| 424 | } |
| 425 | if (text[end] !== ',') return null; |
| 426 | i = end + 1; |
| 427 | } |
| 428 | return null; |
| 429 | } |
| 430 | |
| 431 | /** Walk `keys` into the object literal opening at `at`; the string literal at the end, or null. */ |
| 432 | function readObjectPath(text: string, at: number, keys: string[]): string | null { |
no test coverage detected