Walk `keys` into the object literal opening at `at`; the string literal at the end, or null.
(text: string, at: number, keys: string[])
| 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 { |
| 433 | const skipString = (j: number): number => { |
| 434 | const quote = text[j]!; |
| 435 | for (j++; j < text.length && text[j] !== quote; j++) if (text[j] === '\\') j++; |
| 436 | return j + 1; |
| 437 | }; |
| 438 | const skipValue = (j: number): number => { |
| 439 | let depth = 0; |
| 440 | for (; j < text.length; j++) { |
| 441 | const ch = text[j]!; |
| 442 | if (ch === '"' || ch === "'" || ch === '`') { j = skipString(j) - 1; continue; } |
| 443 | if (ch === '{' || ch === '[' || ch === '(') depth++; |
| 444 | else if (ch === '}' || ch === ']' || ch === ')') { if (depth === 0) return j; depth--; } |
| 445 | else if (ch === ',' && depth === 0) return j; |
| 446 | } |
| 447 | return j; |
| 448 | }; |
| 449 | let i = at + 1; |
| 450 | while (i < text.length) { |
| 451 | const m = /^\s*(?:([A-Za-z_$][\w$]*)|["']([^"']+)["'])\s*:\s*/.exec(text.slice(i)); |
| 452 | if (!m) { |
| 453 | const next = skipValue(i); |
| 454 | if (text[next] !== ',') return null; |
| 455 | i = next + 1; |
| 456 | continue; |
| 457 | } |
| 458 | const key = m[1] ?? m[2]!; |
| 459 | const valueAt = i + m[0].length; |
| 460 | if (key === keys[0]) { |
| 461 | if (keys.length === 1) { |
| 462 | const lit = /^(["'])((?:\\.|(?!\1).)*)\1/.exec(text.slice(valueAt)); |
| 463 | return lit ? lit[2]! : null; |
| 464 | } |
| 465 | return text[valueAt] === '{' ? readObjectPath(text, valueAt, keys.slice(1)) : null; |
| 466 | } |
| 467 | const end = skipValue(valueAt); |
| 468 | if (text[end] !== ',') return null; |
| 469 | i = end + 1; |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * The file a lazily loaded module is, from the file that loads it: by the |
no test coverage detected