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

Function constantText

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

Source from the content-addressed store, hash-verified

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