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

Function functionBody

src/resolution/frameworks/react.ts:2264–2305  ·  view source on GitHub ↗

* Where the function written at `at` keeps its body: a block, from its `{` to * its `}`, or an arrow's expression, at `start`. Null for no function.

(safe: string, at: number)

Source from the content-addressed store, hash-verified

2262 * its `}`, or an arrow's expression, at `start`. Null for no function.
2263 */
2264function functionBody(safe: string, at: number): { block: boolean; start: number; end: number } | null {
2265 let i = skipSpace(safe, at);
2266 const keyword = (word: string): boolean => {
2267 const re = new RegExp(`${word}\\b`, 'y');
2268 re.lastIndex = i;
2269 if (!re.test(safe)) return false;
2270 i = skipSpace(safe, re.lastIndex);
2271 return true;
2272 };
2273 keyword('async');
2274 const arrow = !keyword('function');
2275 const name = /\*?\s*[A-Za-z_$][\w$]*/y;
2276 if (!arrow) {
2277 name.lastIndex = i;
2278 if (name.test(safe)) i = skipSpace(safe, name.lastIndex);
2279 }
2280 if (safe[i] === '(') {
2281 const close = matchBracket(safe, i);
2282 if (close < 0) return null;
2283 i = skipSpace(safe, close + 1);
2284 } else {
2285 // `user => [ … ]`
2286 const param = /[A-Za-z_$][\w$]*/y;
2287 param.lastIndex = i;
2288 if (!arrow || !param.test(safe)) return null;
2289 i = skipSpace(safe, param.lastIndex);
2290 }
2291 // A return type, up to the arrow or the body: `(): RouteObject[] => [ … ]`.
2292 if (safe[i] === ':') {
2293 const stop = safe.indexOf(arrow ? '=>' : '{', i);
2294 if (stop < 0) return null;
2295 i = stop;
2296 }
2297 if (arrow) {
2298 if (!safe.startsWith('=>', i)) return null;
2299 i = skipSpace(safe, i + 2);
2300 if (safe[i] !== '{') return { block: false, start: i, end: i };
2301 }
2302 if (safe[i] !== '{') return null;
2303 const end = blockEnd(safe, i);
2304 return end < 0 ? null : { block: true, start: i, end };
2305}
2306
2307/** The statements that start a block of their own after a parenthesized head: never a method's. */
2308const BLOCK_HEADS = new Set(['if', 'for', 'while', 'switch', 'catch', 'with']);

Callers 1

returnedTablesFunction · 0.85

Calls 4

matchBracketFunction · 0.90
keywordFunction · 0.85
skipSpaceFunction · 0.70
blockEndFunction · 0.70

Tested by

no test coverage detected