|
| 1 | +import type { Invocations } from "./types.ts"; |
| 2 | +import { PHP_INVOCATION_QUERY } from "./queries.ts"; |
| 3 | +import type { PHPIncluseResolver } from "../incluseResolver/index.ts"; |
| 4 | +import type Parser from "tree-sitter"; |
| 5 | +import type { ExportedSymbol } from "../exportResolver/types.ts"; |
| 6 | +import { SymbolNode } from "../registree/types.ts"; |
| 7 | + |
| 8 | +export class PHPInvocationResolver { |
| 9 | + incluseResolver: PHPIncluseResolver; |
| 10 | + |
| 11 | + constructor(incluseResolver: PHPIncluseResolver) { |
| 12 | + this.incluseResolver = incluseResolver; |
| 13 | + } |
| 14 | + |
| 15 | + getInvocationsForNode( |
| 16 | + node: Parser.SyntaxNode, |
| 17 | + filepath: string, |
| 18 | + symbolname: string | undefined = undefined, |
| 19 | + ): Invocations { |
| 20 | + const currentfile = this.incluseResolver.registree.registry.files.get( |
| 21 | + filepath, |
| 22 | + )!; |
| 23 | + const availableSymbols = this.incluseResolver |
| 24 | + .resolveImports(currentfile)?.resolved; |
| 25 | + const localSymbols = currentfile.symbols; |
| 26 | + const unresolved = new Set<string>(); |
| 27 | + const resolved = new Map<string, ExportedSymbol[]>(); |
| 28 | + const captures = PHP_INVOCATION_QUERY.captures(node); |
| 29 | + for (const capture of captures) { |
| 30 | + const name = capture.node.text; |
| 31 | + // if the symbol name is the same as the one we are looking at, skip it |
| 32 | + if (symbolname && name === symbolname) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + if (availableSymbols && availableSymbols.has(name)) { |
| 36 | + const availableSymbol = availableSymbols.get(name); |
| 37 | + if (!availableSymbol) { |
| 38 | + unresolved.add(name); |
| 39 | + continue; |
| 40 | + } |
| 41 | + resolved.set(name, availableSymbol); |
| 42 | + } else if (localSymbols && localSymbols.has(name)) { |
| 43 | + const localSymbol = localSymbols.get(name); |
| 44 | + if (!localSymbol) { |
| 45 | + unresolved.add(name); |
| 46 | + continue; |
| 47 | + } |
| 48 | + resolved.set(name, localSymbol); |
| 49 | + } else if (capture.name === "qualified") { |
| 50 | + const qualSymbol = this.incluseResolver.registree.tree.findNode(name); |
| 51 | + if (qualSymbol && qualSymbol instanceof SymbolNode) { |
| 52 | + resolved.set(name, qualSymbol.symbols); |
| 53 | + } else { |
| 54 | + unresolved.add(name); |
| 55 | + } |
| 56 | + } else { |
| 57 | + unresolved.add(name); |
| 58 | + } |
| 59 | + } |
| 60 | + return { |
| 61 | + resolved, |
| 62 | + unresolved, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + getInvocationsForFile(filepath: string): Invocations { |
| 67 | + const file = this.incluseResolver.registree.registry.files.get(filepath); |
| 68 | + if (!file) { |
| 69 | + throw new Error(`File not found: ${filepath}`); |
| 70 | + } |
| 71 | + return this.getInvocationsForNode(file.rootNode, file.path); |
| 72 | + } |
| 73 | +} |
0 commit comments