* The routine nested in the calling routine, or in one around it, that a * bare name there names (`procedure Inner;` between `procedure TFoo.Bar;` * and its `begin`, `TFoo::Bar::Inner`): the innermost one declared above the * call, the calling routine itself among them, or declared `forward` abov
(lower: string, ref: UnresolvedRef, context: ResolutionContext)
| 861 | * method, which is no node — is the routine's whose header is above it. |
| 862 | */ |
| 863 | function nestedRoutineInScope(lower: string, ref: UnresolvedRef, context: ResolutionContext): Found | null { |
| 864 | // Most names no nested routine has: settled before any other lookup. |
| 865 | const named = pascalNamed(lower, context); |
| 866 | if (!named.some((n) => n.kind === 'function' && n.qualifiedName.includes('::'))) return null; |
| 867 | const caller = routineNode(ref.fromNodeId, context); |
| 868 | const fromHeader = caller ? null : headerRoutineAt(ref.filePath!, ref.line, context); |
| 869 | const memo = memos(context).nestedInScope; |
| 870 | const key = `${ref.fromNodeId}|${fromHeader ?? ''}|${lower}`; |
| 871 | if (memo.has(key)) return memo.get(key)!; |
| 872 | let found: Found | null = null; |
| 873 | if ((caller && caller.language === 'pascal') || fromHeader) { |
| 874 | const chain = (caller?.qualifiedName ?? fromHeader!).toLowerCase(); |
| 875 | let best: Node | null = null; |
| 876 | let depth = -1; |
| 877 | for (const n of named) { |
| 878 | // A routine whose header is above the code is no node: what is nested in it is one by its name alone. |
| 879 | const nested = caller ? isNestedRoutine(n, context) : n.kind === 'function' && n.language === 'pascal' && n.qualifiedName.includes('::'); |
| 880 | if (n.filePath !== ref.filePath || !nested) continue; |
| 881 | if (n.startLine > ref.line && !declaredForward(n, ref.line, context)) continue; |
| 882 | const outer = ownerOf(n); |
| 883 | // Nested in the caller, or in a routine the caller is nested in. |
| 884 | if (chain !== outer && !chain.startsWith(`${outer}::`)) continue; |
| 885 | if (outer.length > depth || (outer.length === depth && n.startLine < best!.startLine)) { |
| 886 | best = n; |
| 887 | depth = outer.length; |
| 888 | } |
| 889 | } |
| 890 | if (best) found = { node: best, resolvedBy: 'exact-match', confidence: 0.95 }; |
| 891 | } |
| 892 | memo.set(key, found); |
| 893 | return found; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Whether a routine nested in another is declared `forward` above a line |
no test coverage detected