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

Function blockValue

src/resolution/frameworks/react.ts:1711–1739  ·  view source on GitHub ↗

* The value a function body hands back from its first top-level `return`, * its declarations bound on the way there. Nested blocks and functions are * stepped over: what is declared or returned in there is theirs.

(body: string, scope: Map<string, Loaded | null>)

Source from the content-addressed store, hash-verified

1709 * stepped over: what is declared or returned in there is theirs.
1710 */
1711function blockValue(body: string, scope: Map<string, Loaded | null>): Loaded | null {
1712 const word = /[A-Za-z_$][\w$]*/y;
1713 for (let i = 0; i < body.length;) {
1714 const ch = body[i]!;
1715 if (ch === '"' || ch === "'" || ch === '`' || ch === '{' || ch === '(' || ch === '[') {
1716 const end = ch === '{' || ch === '(' || ch === '[' ? matchBracket(body, i) : skipString(body, i);
1717 if (end < 0) return null;
1718 i = end + 1;
1719 continue;
1720 }
1721 word.lastIndex = i;
1722 const w = word.exec(body)?.[0];
1723 if (!w) {
1724 i++;
1725 continue;
1726 }
1727 const after = i + w.length;
1728 const statement = i === 0 || !/[\w$.]/.test(body[i - 1]!);
1729 i = after;
1730 if (!statement) continue;
1731 if (w === 'return') return valueOf(body.slice(after, statementEnd(body, after)), scope);
1732 if (w === 'const' || w === 'let' || w === 'var') i = declare(body, after, scope);
1733 else if (w === 'function' || w === 'class') {
1734 const name = /^\s*\*?\s*([A-Za-z_$][\w$]*)/.exec(body.slice(after, after + 128));
1735 if (name) scope.set(name[1]!, null);
1736 }
1737 }
1738 return null;
1739}
1740
1741/** Bind each declarator of a `const`, `let` or `var` whose first pattern starts at `at`; where the declaration ends. */
1742function declare(body: string, at: number, scope: Map<string, Loaded | null>): number {

Callers 1

loaderValueFunction · 0.85

Calls 7

matchBracketFunction · 0.90
skipStringFunction · 0.90
declareFunction · 0.85
valueOfFunction · 0.70
statementEndFunction · 0.70
execMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected