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

Function constantText

src/resolution/frameworks/angular-router.ts:489–547  ·  view source on GitHub ↗
(expr: string, fromFile: string, context: ResolutionContext)

Source from the content-addressed store, hash-verified

487 * read from the file the chain's root is imported from (or declared in).
488 */
489export function constantText(expr: string, fromFile: string, context: ResolutionContext): string | null {
490 const source = context.getNodesByKind('route');
491 let entry = constantTexts.get(context);
492 if (!entry || entry.source !== source) constantTexts.set(context, (entry = { source, memo: new Map() }));
493 const memo = entry.memo;
494 const key = `${fromFile}\0${expr}`;
495 if (memo.has(key)) return memo.get(key)!;
496 // `const { create } = internalRoutes.accounts.subRoutes;` / `const accounts = internalRoutes.accounts;`
497 // make `create.path` a chain rooted at the import.
498 const aliased = localAlias(expr, fromFile, context);
499 if (aliased !== null && aliased !== expr) {
500 const value = constantText(aliased, fromFile, context);
501 memo.set(key, value);
502 return value;
503 }
504 const [root, ...chain] = expr.split('.').map((part) => part.trim());
505 let value: string | null = null;
506 const file = root ? declaringFile(root, fromFile, context) : null;
507 const content = file && root ? declarationSource(root, file, context, 0) : null;
508 if (content && root && chain.length > 0) {
509 const safe = stripCommentsForRegex(content, 'typescript');
510 const readChain = (open: number, links: readonly string[]): string | null => {
511 let start = open;
512 let end = matchBracket(safe, start);
513 for (let i = 0; i < links.length && end > start; i++) {
514 const field = readFields(safe, start, end).get(links[i]!);
515 if (!field) return null;
516 if (i === links.length - 1) return field.text.trim();
517 start = safe.indexOf('{', field.at);
518 end = start < 0 ? -1 : matchBracket(safe, start);
519 }
520 return null;
521 };
522 const decl = new RegExp(String.raw`\b(?:const|let)\s+${escapeRegExp(root)}\s*(?::[^=]+)?=\s*\{`).exec(safe);
523 const enumDecl = decl ? null : new RegExp(String.raw`\benum\s+${escapeRegExp(root)}\s*\{`).exec(safe);
524 const classDecl = decl || enumDecl ? null : new RegExp(String.raw`\bclass\s+${escapeRegExp(root)}\b[^{]*\{`).exec(safe);
525 if (decl) value = readChain(decl.index + decl[0].length - 1, chain);
526 else if (enumDecl && chain.length === 1) {
527 // `enum Paths { Board = 'board' }`
528 const open = enumDecl.index + enumDecl[0].length - 1;
529 const body = safe.slice(open, Math.max(open, matchBracket(safe, open)));
530 value = new RegExp(String.raw`\b${escapeRegExp(chain[0]!)}\s*=\s*(['"\`][^'"\`]*['"\`])`).exec(body)?.[1] ?? null;
531 } else if (classDecl) {
532 // `class ProjectConst { static readonly IssueId = 'issueId' }`, and
533 // `class RouterUtil { static Configuration = { Visualizer: 'visualizer' } }`.
534 const open = classDecl.index + classDecl[0].length - 1;
535 const close = matchBracket(safe, open);
536 const body = close > open ? safe.slice(open, close) : '';
537 const field = new RegExp(String.raw`\bstatic\s+(?:readonly\s+)?${escapeRegExp(chain[0]!)}\s*(?::[^=;]+)?=\s*`).exec(body);
538 if (field) {
539 const at = open + field.index + field[0].length;
540 if (safe[at] === '{') value = chain.length > 1 ? readChain(at, chain.slice(1)) : null;
541 else if (chain.length === 1) value = /^(['"\`])[^'"\`]*\1/.exec(safe.slice(at))?.[0] ?? null;
542 }
543 }
544 }
545 memo.set(key, value);
546 return value;

Callers 2

constantValueFunction · 0.85
destinationOfFunction · 0.85

Calls 13

stripCommentsForRegexFunction · 0.90
escapeRegExpFunction · 0.90
matchBracketFunction · 0.90
localAliasFunction · 0.85
declaringFileFunction · 0.85
declarationSourceFunction · 0.85
readChainFunction · 0.85
hasMethod · 0.80
mapMethod · 0.80
getNodesByKindMethod · 0.65
getMethod · 0.65
execMethod · 0.65

Tested by

no test coverage detected