* The failed python attribute-chain refs that an edit to `touched` makes * resolvable now — what a sync retries after a factory or a base class * changed in another file (`pythonAttrRefResolves` answers each one). * * The candidates are the refs in the files that import a touched file
(touched: string[], ceiling: number = 2000)
| 1590 | * resolves `Client` in the touched file. |
| 1591 | */ |
| 1592 | pythonAttrRetryCandidates(touched: string[], ceiling: number = 2000): UnresolvedReference[] | null { |
| 1593 | const touchedSet = new Set(touched); |
| 1594 | const closure = this.queries.getPythonImporterClosure(touched).filter((f) => !touchedSet.has(f)); |
| 1595 | // One answer per (calling symbol, name): the same call written twice in |
| 1596 | // one function resolves the same way, so it is asked — and counted — once. |
| 1597 | const keyOf = (ref: { fromNodeId: string; referenceName: string }) => `${ref.fromNodeId}\0${ref.referenceName}`; |
| 1598 | const resolvable = (refs: UnresolvedReference[]) => { |
| 1599 | const answers = new Map<string, boolean>(); |
| 1600 | return refs.filter((ref) => { |
| 1601 | if (!ref.filePath || !ref.language) return false; |
| 1602 | const key = keyOf(ref); |
| 1603 | let ok = answers.get(key); |
| 1604 | if (ok === undefined) answers.set(key, (ok = pythonAttrRefResolves(ref as UnresolvedRef, this.context))); |
| 1605 | return ok; |
| 1606 | }); |
| 1607 | }; |
| 1608 | const candidates = this.queries.getFailedPythonAttrRefKeys(closure); |
| 1609 | const load = (keys: typeof candidates) => this.queries.getUnresolvedReferencesByIds(keys.map((k) => k.id)); |
| 1610 | if (new Set(candidates.map(keyOf)).size <= ceiling) return resolvable(load(candidates)); |
| 1611 | |
| 1612 | const assigned = new Set<string>(); |
| 1613 | const defined = new Set<string>(); |
| 1614 | const subclassing = new Set<string>(); |
| 1615 | const calling = new Set<string>(); |
| 1616 | for (const file of touched) { |
| 1617 | const source = this.context.readFile(file); |
| 1618 | if (source) { |
| 1619 | const text = stripCommentsForRegex(source, 'python'); |
| 1620 | for (const m of text.matchAll(/\b(?:self|cls)\.(\w+)\s*(?::[^=\n]*)?=(?!=)|^[ \t]+(\w+)\s*(?::[^=\n]*)?=(?!=)/gm)) { |
| 1621 | assigned.add((m[1] ?? m[2])!); |
| 1622 | } |
| 1623 | } |
| 1624 | for (const n of this.context.getNodesInFile(file)) { |
| 1625 | if (n.kind === 'class' || n.kind === 'function' || n.kind === 'method') defined.add(n.name); |
| 1626 | } |
| 1627 | for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['extends'], ['class'])) subclassing.add(f); |
| 1628 | for (const f of this.queries.getPythonFilesWithEdgesInto(file, ['calls', 'instantiates'], ['function', 'method', 'class'])) { |
| 1629 | calling.add(f); |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | const callees = new Map<string, Map<string, Set<string>>>(); // file → attr → callee names |
| 1634 | const assignedCallees = (file: string, attr: string): Set<string> => { |
| 1635 | let byAttr = callees.get(file); |
| 1636 | if (!byAttr) { |
| 1637 | byAttr = new Map(); |
| 1638 | const source = this.context.readFile(file); |
| 1639 | const text = source ? stripCommentsForRegex(source, 'python') : ''; |
| 1640 | for (const m of text.matchAll(/\b(\w+)\s*(?::[^=\n]*)?=(?!=)\s*(?:await\s+)?(?:[\w.]*\.)?(\w+)\s*\(/g)) { |
| 1641 | let set = byAttr.get(m[1]!); |
| 1642 | if (!set) byAttr.set(m[1]!, (set = new Set())); |
| 1643 | set.add(m[2]!); |
| 1644 | } |
| 1645 | callees.set(file, byAttr); |
| 1646 | } |
| 1647 | return byAttr.get(attr) ?? new Set(); |
| 1648 | }; |
| 1649 |
no test coverage detected