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

Function visibleAt

src/resolution/frameworks/react.ts:2198–2222  ·  view source on GitHub ↗

* Picks, of a name's declarations in `file` (where each keyword sits), the * one in scope at `use`: in the innermost function around the use that * declares the name, the last declaration before the use, else the first * after it; else one at the module's top level. A declaration inside another

(use: number | undefined, safe: string, file: string, context: ResolutionContext)

Source from the content-addressed store, hash-verified

2196 * inside one function are not told apart.
2197 */
2198function visibleAt(use: number | undefined, safe: string, file: string, context: ResolutionContext): (keywords: number[]) => number | undefined {
2199 return (keywords) => {
2200 const lineStarts = [0];
2201 for (let i = safe.indexOf('\n'); i !== -1; i = safe.indexOf('\n', i + 1)) lineStarts.push(i + 1);
2202 const offset = (line: number, column: number): number => (lineStarts[line - 1] ?? safe.length) + column;
2203 const fns = context.getNodesInFile(file)
2204 .filter((n) => FN_KINDS.has(n.kind))
2205 .map((n) => ({ start: offset(n.startLine, n.startColumn), end: offset(n.endLine, n.endColumn) }));
2206 // The function a declaration's own value is (`const routes = (user) =>
2207 // [ … ]`) starts after the keyword, so it is not around it.
2208 const around = (at: number) => fns.filter((f) => f.start < at && f.end > at);
2209 const scopes = new Set(use === undefined ? [] : around(use));
2210 let best: number | undefined;
2211 let depth = -1;
2212 keywords.forEach((at, i) => {
2213 const encl = around(at);
2214 if (!encl.every((f) => scopes.has(f))) return;
2215 if (encl.length > depth || (encl.length === depth && use !== undefined && at < use)) {
2216 best = i;
2217 depth = encl.length;
2218 }
2219 });
2220 return best;
2221 };
2222}
2223
2224/**
2225 * The tables the function written at `at` returns: its expression body's

Callers 2

tableNamedFunction · 0.85
exportedTableFunction · 0.85

Calls 6

offsetFunction · 0.85
aroundFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
hasMethod · 0.80
getNodesInFileMethod · 0.65

Tested by

no test coverage detected