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

Function mapsRouteFields

src/resolution/frameworks/react.ts:1411–1443  ·  view source on GitHub ↗

* True when a `.map` callback renders a ` ` from the item's own route * fields — `{...route}`, `{...rest}` destructured from it, `path={route.path}` * or a `path` destructured from it — so the array it maps is a route table. * A ` ` composes a path the t

(callback: string, tags: ReadonlyArray<{ spreads: string[]; path?: string }>)

Source from the content-addressed store, hash-verified

1409 * not hold, and a callback that renders links or menu entries maps a menu.
1410 */
1411function mapsRouteFields(callback: string, tags: ReadonlyArray<{ spreads: string[]; path?: string }>): boolean {
1412 const head = /^(?:async\s+)?(?:function\b[^(]*)?\(\s*/.exec(callback);
1413 let param: string | undefined;
1414 const rest = new Set<string>();
1415 const paths = new Set<string>();
1416 const bind = (pattern: string): void => {
1417 for (const entry of topLevelEntries(pattern.slice(1, -1))) {
1418 const spread = /^\.\.\.\s*([A-Za-z_$][\w$]*)$/.exec(entry);
1419 if (spread) rest.add(spread[1]!);
1420 const path = /^path\s*(?::\s*([A-Za-z_$][\w$]*))?\s*(?:=[\s\S]*)?$/.exec(entry);
1421 if (path) paths.add(path[1] ?? 'path');
1422 }
1423 };
1424 if (head) {
1425 const at = head[0].length;
1426 if (callback[at] === '{') {
1427 const close = matchBracket(callback, at);
1428 if (close < 0) return false;
1429 bind(callback.slice(at, close + 1));
1430 } else param = /^[A-Za-z_$][\w$]*/.exec(callback.slice(at))?.[0];
1431 } else {
1432 param = /^(?:async\s+)?([A-Za-z_$][\w$]*)\s*=>/.exec(callback)?.[1];
1433 }
1434 if (param) {
1435 // `const { element, ...rest } = route;` in the body.
1436 const destructured = new RegExp(`\\b(?:const|let|var)\\s*(\\{[^{}]*\\})\\s*=\\s*${param.replace(/\$/g, '\\$')}\\s*[;\\n]`, 'g');
1437 for (const m of callback.matchAll(destructured)) bind(m[1]!);
1438 }
1439 const ownPath = param ? new RegExp(`^${param.replace(/\$/g, '\\$')}\\s*\\??\\.\\s*path$`) : null;
1440 return tags.some((t) =>
1441 t.spreads.some((s) => s === param || rest.has(s)) ||
1442 (t.path !== undefined && (ownPath?.test(t.path) || paths.has(t.path))));
1443}
1444
1445/**
1446 * The component tags of a JSX element and of what it nests, outermost first

Callers 1

settleFunction · 0.85

Calls 4

matchBracketFunction · 0.90
hasMethod · 0.80
bindFunction · 0.70
execMethod · 0.65

Tested by

no test coverage detected