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