|
| 1 | +import { parse, visit } from "@solidity-parser/parser"; |
| 2 | +import type { SourceUnit, ContractDefinition } from "@solidity-parser/parser/dist/src/ast-types"; |
| 3 | +import { readFile } from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
| 5 | +import { findContractNode } from "./findContractNode"; |
| 6 | +import { QualifiedSymbol } from "./contractToInterface"; |
| 7 | +import { findSymbolImport } from "./findSymbolImport"; |
| 8 | + |
| 9 | +interface InheritanceInfo { |
| 10 | + baseContracts: string[]; |
| 11 | + baseContractImports: Map<string, string>; // baseName -> import path |
| 12 | + symbols: Map<string, { type: "struct" | "enum" | "error"; contract: string }>; |
| 13 | + filePath?: string; // Track where this contract was defined |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Creates a resolver function that can find symbols through contract inheritance chains |
| 18 | + * @param contractPath Path to the contract file |
| 19 | + * @param contractName Name of the contract |
| 20 | + * @returns A function that resolves symbols to their qualified forms |
| 21 | + */ |
| 22 | +export async function createInheritanceResolver( |
| 23 | + contractPath: string, |
| 24 | + contractName: string, |
| 25 | +): Promise<(symbol: string) => QualifiedSymbol | undefined> { |
| 26 | + const resolvedContracts = new Map<string, InheritanceInfo>(); |
| 27 | + const visitedPaths = new Set<string>(); |
| 28 | + |
| 29 | + async function parseContract(filePath: string, targetContractName?: string): Promise<InheritanceInfo | undefined> { |
| 30 | + // Prevent infinite recursion |
| 31 | + const normalizedPath = path.resolve(filePath); |
| 32 | + if (visitedPaths.has(normalizedPath)) { |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + visitedPaths.add(normalizedPath); |
| 36 | + |
| 37 | + try { |
| 38 | + const source = await readFile(filePath, "utf8"); |
| 39 | + |
| 40 | + // Try to parse the source |
| 41 | + let ast: SourceUnit; |
| 42 | + try { |
| 43 | + ast = parse(source); |
| 44 | + } catch (parseError) { |
| 45 | + // If parsing fails, log the error and skip this file |
| 46 | + console.warn(`Warning: Failed to parse ${filePath} for inheritance resolution:`, parseError); |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + |
| 50 | + // If targetContractName is specified, find that specific contract |
| 51 | + // Otherwise, process all contracts in the file |
| 52 | + const contractsToProcess: ContractDefinition[] = []; |
| 53 | + |
| 54 | + if (targetContractName) { |
| 55 | + const contractNode = findContractNode(ast, targetContractName); |
| 56 | + if (contractNode) { |
| 57 | + contractsToProcess.push(contractNode); |
| 58 | + } |
| 59 | + } else { |
| 60 | + visit(ast, { |
| 61 | + ContractDefinition(node) { |
| 62 | + contractsToProcess.push(node); |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + // Process each contract |
| 68 | + for (const contractNode of contractsToProcess) { |
| 69 | + const info: InheritanceInfo = { |
| 70 | + baseContracts: [], |
| 71 | + baseContractImports: new Map(), |
| 72 | + symbols: new Map(), |
| 73 | + filePath: normalizedPath, |
| 74 | + }; |
| 75 | + |
| 76 | + // Extract base contracts |
| 77 | + if (contractNode.baseContracts) { |
| 78 | + for (const base of contractNode.baseContracts) { |
| 79 | + info.baseContracts.push(base.baseName.namePath); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Extract symbols defined in this contract |
| 84 | + visit(contractNode, { |
| 85 | + StructDefinition(node) { |
| 86 | + if (node.name) { |
| 87 | + info.symbols.set(node.name, { type: "struct", contract: contractNode.name }); |
| 88 | + } |
| 89 | + }, |
| 90 | + EnumDefinition(node) { |
| 91 | + if (node.name) { |
| 92 | + info.symbols.set(node.name, { type: "enum", contract: contractNode.name }); |
| 93 | + } |
| 94 | + }, |
| 95 | + CustomErrorDefinition(node) { |
| 96 | + if (node.name) { |
| 97 | + info.symbols.set(node.name, { type: "error", contract: contractNode.name }); |
| 98 | + } |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + resolvedContracts.set(contractNode.name, info); |
| 103 | + |
| 104 | + // Recursively process base contracts |
| 105 | + for (const baseName of info.baseContracts) { |
| 106 | + // First check if it's imported |
| 107 | + const importInfo = findSymbolImport(ast, baseName); |
| 108 | + if (importInfo) { |
| 109 | + // Store the original import path |
| 110 | + info.baseContractImports.set(baseName, importInfo.path); |
| 111 | + |
| 112 | + const importPath = importInfo.path.startsWith(".") |
| 113 | + ? path.resolve(path.dirname(filePath), importInfo.path) |
| 114 | + : importInfo.path; |
| 115 | + |
| 116 | + await parseContract(importPath, baseName); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return targetContractName ? resolvedContracts.get(targetContractName) : undefined; |
| 122 | + } catch (error) { |
| 123 | + // Silently fail if we can't read/parse a file |
| 124 | + return undefined; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Parse the main contract and its inheritance chain |
| 129 | + await parseContract(contractPath, contractName); |
| 130 | + |
| 131 | + // Create the resolver function |
| 132 | + return (symbol: string): QualifiedSymbol | undefined => { |
| 133 | + // Check if symbol is defined in the main contract |
| 134 | + const mainInfo = resolvedContracts.get(contractName); |
| 135 | + if (mainInfo?.symbols.has(symbol)) { |
| 136 | + // Symbol is defined in main contract, no qualification needed |
| 137 | + return { |
| 138 | + symbol, |
| 139 | + sourcePath: contractPath, |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + // Search through inheritance chain |
| 144 | + function searchInheritance(currentContract: string, visited: Set<string> = new Set()): QualifiedSymbol | undefined { |
| 145 | + if (visited.has(currentContract)) { |
| 146 | + return undefined; |
| 147 | + } |
| 148 | + visited.add(currentContract); |
| 149 | + |
| 150 | + const contractInfo = resolvedContracts.get(currentContract); |
| 151 | + if (!contractInfo) { |
| 152 | + return undefined; |
| 153 | + } |
| 154 | + |
| 155 | + // Check each base contract |
| 156 | + for (const baseName of contractInfo.baseContracts) { |
| 157 | + const baseInfo = resolvedContracts.get(baseName); |
| 158 | + if (baseInfo?.symbols.has(symbol)) { |
| 159 | + // Found the symbol in a base contract |
| 160 | + // Get the import path from the main contract to this base contract |
| 161 | + const importPath = mainInfo?.baseContractImports.get(baseName) || `./${baseName}.sol`; |
| 162 | + |
| 163 | + return { |
| 164 | + symbol, |
| 165 | + qualifier: baseName, |
| 166 | + sourcePath: importPath, |
| 167 | + }; |
| 168 | + } |
| 169 | + |
| 170 | + // Recursively search in base's bases |
| 171 | + const result = searchInheritance(baseName, visited); |
| 172 | + if (result) { |
| 173 | + return result; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return undefined; |
| 178 | + } |
| 179 | + |
| 180 | + return searchInheritance(contractName); |
| 181 | + }; |
| 182 | +} |
0 commit comments