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

Function configHrefExpression

src/resolution/frameworks/react.ts:335–368  ·  view source on GitHub ↗
(expr: string, fromFile: string, context: ResolutionContext)

Source from the content-addressed store, hash-verified

333 * that is a whole segment stays a hole. Null for anything else.
334 */
335export function configHrefExpression(expr: string, fromFile: string, context: ResolutionContext): string | null {
336 const m = /^\s*([A-Za-z_$][\w$]*)((?:\s*\??\.\s*[A-Za-z_$][\w$]*)+)\s*(\((?:[^()]|\([^()]*\))*\))?\s*$/.exec(expr);
337 if (!m) return null;
338 const root = m[1]!;
339 const keys = m[2]!.split('.').map((k) => k.replace(/[?\s]/g, '')).filter(Boolean);
340 let file = fromFile;
341 let name = root;
342 const mapping = context.getImportMappings(fromFile, 'tsx').find((x) => x.localName === root) ??
343 context.getImportMappings(fromFile, 'typescript').find((x) => x.localName === root);
344 if (mapping) {
345 const resolved = resolveImportPath(mapping.source, fromFile, 'typescript', context);
346 if (!resolved) return null;
347 file = resolved;
348 if (mapping.exportedName && mapping.exportedName !== 'default' && mapping.exportedName !== '*') name = mapping.exportedName;
349 }
350 const decl = nodesNamed(context, file, name).find((n) => n.name === name && (n.kind === 'constant' || n.kind === 'variable'));
351 if (!decl) return null;
352 const lines = context.readFile(file)?.split('\n') ?? [];
353 const text = lines.slice(decl.startLine - 1, decl.endLine).join('\n');
354 const open = text.indexOf('{', Math.max(0, text.search(new RegExp(`\\b${escapeRegExp(name)}\\b`))));
355 if (open < 0) return null;
356 let value = readObjectValue(text, open, keys);
357 if (value === null) return null;
358 // A function's value is what it returns: `(id: string) => \`/app/…\``, `() => { return '/'; }`.
359 if (m[3] !== undefined) {
360 const body = /^(?:async\s+)?(?:\([^()]*(?:\([^()]*\)[^()]*)*\)|[A-Za-z_$][\w$]*)\s*(?::\s*[^=]+?)?=>\s*/.exec(value);
361 if (!body) return null;
362 value = value.slice(body[0].length).trim();
363 if (value.startsWith('{')) value = /\breturn\s+([`'"][\s\S]*?[`'"])\s*;?\s*}/.exec(value)?.[1] ?? '';
364 }
365 if (!/^[`'"]/.test(value)) return null;
366 // `/auth/login${redirectTo ? … : ''}`: a hole glued to a segment is a suffix, not a segment.
367 return value.startsWith('`') ? dropGluedTemplateHoles(value) : value;
368}
369
370/** Remove each `${…}` of a template literal that is not a whole path segment. */
371function dropGluedTemplateHoles(template: string): string {

Callers 2

reactRouterLinkEdgesFunction · 0.90
resolveFunction · 0.90

Calls 11

resolveImportPathFunction · 0.90
nodesNamedFunction · 0.90
escapeRegExpFunction · 0.90
readObjectValueFunction · 0.85
dropGluedTemplateHolesFunction · 0.85
mapMethod · 0.80
getImportMappingsMethod · 0.80
searchMethod · 0.80
execMethod · 0.65
readFileMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected