The implementation a stub stands for: the first `def` of its scope and name below it that is no stub.
(def: Node, context: ResolutionContext)
| 55 | |
| 56 | /** The implementation a stub stands for: the first `def` of its scope and name below it that is no stub. */ |
| 57 | function implementationOf(def: Node, context: ResolutionContext): string | null { |
| 58 | if (def.language !== 'python' || (def.kind !== 'function' && def.kind !== 'method')) return null; |
| 59 | const named = context.getNodesInFileNamed?.(def.filePath, def.name) ?? |
| 60 | context.getNodesInFile(def.filePath).filter((n) => n.name === def.name); |
| 61 | const scope = named.filter((n) => n.kind === def.kind && n.qualifiedName === def.qualifiedName); |
| 62 | // The one def of its name in its scope: the common case, decided without reading the file. |
| 63 | if (scope.length < 2) return null; |
| 64 | const overload = overloadSpellings(def.filePath, context); |
| 65 | const lines = context.getFileLines?.(def.filePath) ?? context.readFile(def.filePath)?.split(/\r?\n/) ?? []; |
| 66 | if (!isOverloadStub(def, lines, overload)) return null; |
| 67 | const below = scope.filter((n) => n.startLine > def.startLine).sort((a, b) => a.startLine - b.startLine); |
| 68 | return below.find((n) => !isOverloadStub(n, lines, overload))?.id ?? null; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * How a file can write typing's `overload` as a decorator: bare names (`overload`, |
no test coverage detected