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

Function withSubjectType

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

* The class (or record or interface) the project declares that a `with` * statement's subject is a value of, read as Delphi reads it: `Self`; a * local or parameter of the calling routine, by its declared type; a member * of the routine's class, its unit or the units it uses, by its declared * t

(subjects: Subject[], p: number, cls: Node | null, ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

1459 * one typed is the `p`-th, and the ones after it are around it.
1460 */
1461function withSubjectType(subjects: Subject[], p: number, cls: Node | null, ref: UnresolvedRef, context: ResolutionContext): Node | null {
1462 const { statement, k } = subjects[p]!;
1463 const memo = memos(context).subjectTypes;
1464 const key = `${ref.filePath}|${statement.start}|${k}`;
1465 if (memo.has(key)) return memo.get(key)!;
1466 // Guards a cycle while the subjects around it are typed.
1467 memo.set(key, null);
1468 let type: Node | null = null;
1469 let text = statement.subjects[k]!;
1470 while (text.startsWith('(') && closingBracket(text, 0) === text.length - 1) text = text.slice(1, -1).trim();
1471 const cast = /^(?:.*\S)\s+as\s+&?([A-Za-z_][\w.]*)$/i.exec(text);
1472 if (cast) {
1473 type = classNamed(cast[1]!, ref.filePath!, context);
1474 } else {
1475 const links = subjectLinks(text);
1476 // A member of a subject around this one, the innermost first.
1477 const outer = (lower: string): Node | null => {
1478 for (let q = p + 1; q < subjects.length; q++) {
1479 const around = withSubjectType(subjects, q, cls, ref, context);
1480 const member = around && memberOfValue(around, lower, ref.filePath!, context);
1481 if (member) return member;
1482 }
1483 return null;
1484 };
1485 if (links) type = typeOfLinks(links, cls, ref, context, outer);
1486 }
1487 memo.set(key, type);
1488 return type;
1489}
1490
1491/** The offset of the bracket that closes the one at `open` (`(`/`[`), or -1. */
1492function closingBracket(text: string, open: number): number {

Callers 2

withSubjectMemberFunction · 0.85
outerFunction · 0.85

Calls 9

memosFunction · 0.85
subjectLinksFunction · 0.85
typeOfLinksFunction · 0.85
hasMethod · 0.80
closingBracketFunction · 0.70
classNamedFunction · 0.70
getMethod · 0.65
execMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected