MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / readObjectPath

Function readObjectPath

src/resolution/frameworks/react.ts:433–473  ·  view source on GitHub ↗

Walk `keys` into the object literal opening at `at`; the string literal at the end, or null.

(text: string, at: number, keys: string[])

Source from the content-addressed store, hash-verified

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

Callers 1

constantPathValueFunction · 0.85

Calls 2

skipValueFunction · 0.85
execMethod · 0.65

Tested by

no test coverage detected