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

Function classThroughAliases

src/resolution/pascal-scope.ts:1026–1060  ·  view source on GitHub ↗

* The class `simple` names from `fromFile`, through its type aliases: a walk, * since a name can be an alias several times over (once in each branch of an * `{$IFDEF}`), and the first of them that leads to a class is the answer. `seen` * ends a cycle, so what a step finds can lean on where the wa

(simple: string, fromFile: string, context: ResolutionContext, seen: Set<string>)

Source from the content-addressed store, hash-verified

1024 * classNamed's answer, for where it started, is kept.
1025 */
1026function classThroughAliases(simple: string, fromFile: string, context: ResolutionContext, seen: Set<string>): Node | null {
1027 const named = pascalNamed(simple, context);
1028 const visible = (kinds: ReadonlySet<string>): Node[] => {
1029 // In the unit's own files, then in the unit used last, which declares the
1030 // name Delphi sees; else in the one unit that does.
1031 const candidates = named.filter((n) => kinds.has(n.kind));
1032 for (const file of ownUnitFiles(fromFile, context)) {
1033 const own = candidates.filter((n) => n.filePath === file);
1034 if (own.length > 0) return own;
1035 }
1036 const used = usedUnitFiles(fromFile, context);
1037 for (let i = used.length - 1; i >= 0; i--) {
1038 const there = candidates.filter((n) => n.filePath === used[i]);
1039 if (there.length > 0) return there;
1040 }
1041 return candidates.length > 0 && candidates.every((n) => n.filePath === candidates[0]!.filePath) ? candidates : [];
1042 };
1043 const cls = declarationOf(visible(CLASS_KINDS));
1044 if (cls) return cls;
1045 // A type alias names the class it stands for: `TVTAncestor = TVTAncestorVcl;`,
1046 // and so do a pointer type and a class reference type, whose values reach
1047 // its members (`PVirtualNode = ^TVirtualNode;`, `TSynLogClass = class of
1048 // TSynLog;`, #2550).
1049 for (const alias of visible(ALIAS_KINDS)) {
1050 const line = (fileLines(alias.filePath, context)[alias.startLine - 1] ?? '').replace(/\/\/.*$|\{[^}]*\}/g, '');
1051 const target = /=\s*(?:\^|class\s+of\b)?\s*([A-Za-z_][\w.]*)\s*;/i.exec(line)?.[1]?.split('.').pop()!.toLowerCase();
1052 const key = `${alias.filePath}|${target}`;
1053 if (!target || seen.has(key)) continue;
1054 if (seen.size >= MAX_ALIAS_WALK) return null;
1055 seen.add(key);
1056 const through = classThroughAliases(target, alias.filePath, context, seen);
1057 if (through) return through;
1058 }
1059 return null;
1060}
1061
1062const ALIAS_KINDS: ReadonlySet<string> = new Set(['type_alias']);
1063

Callers 1

classNamedFunction · 0.85

Calls 7

pascalNamedFunction · 0.85
declarationOfFunction · 0.85
visibleFunction · 0.85
fileLinesFunction · 0.85
hasMethod · 0.80
execMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected