(apis: ReadonlyArray<JSDocApi>)
| 2302 | } |
| 2303 | |
| 2304 | function createJSDocApiSeeLinkResolver(apis: ReadonlyArray<JSDocApi>) { |
| 2305 | const byModuleAndName = new Map<string, JSDocApi>() |
| 2306 | const byLocalName = new Map<string, Array<JSDocApi>>() |
| 2307 | const bySourceRange = new Map<string, JSDocApi>() |
| 2308 | const bySourceFile = new Map<string, Array<JSDocApi>>() |
| 2309 | for (const api of apis) { |
| 2310 | byModuleAndName.set(`${api.moduleName}:${api.apiName}`, api) |
| 2311 | bySourceRange.set(sourceRangeKey(api.source.file, api.source.range), api) |
| 2312 | const sourceApis = bySourceFile.get(api.source.file) ?? [] |
| 2313 | sourceApis.push(api) |
| 2314 | bySourceFile.set(api.source.file, sourceApis) |
| 2315 | for (const name of new Set([api.apiName, api.localName])) { |
| 2316 | const existing = byLocalName.get(name) ?? [] |
| 2317 | existing.push(api) |
| 2318 | byLocalName.set(name, existing) |
| 2319 | } |
| 2320 | } |
| 2321 | const resolveSymbol = (link: ParsedInlineLink): JSDocApiSeeLinkResolution | undefined => { |
| 2322 | if (link.symbol === undefined) return undefined |
| 2323 | const symbol = link.symbol |
| 2324 | const exact = bySourceRange.get(sourceRangeKey(symbol.file, symbol.range)) |
| 2325 | if (exact !== undefined) return { _tag: "Resolved", apiId: exact.id, apiFqn: exact.apiFqn } |
| 2326 | const names = seeTargetNames(link) |
| 2327 | const candidates = (bySourceFile.get(symbol.file) ?? []).filter((api) => |
| 2328 | apiMatchesSeeTarget(api, names) && |
| 2329 | (rangeContains(symbol.range, api.source.range) || |
| 2330 | rangeContains(api.source.range, symbol.range) || |
| 2331 | rangeOverlaps(symbol.range, api.source.range)) |
| 2332 | ).sort((self, that) => rangeWidth(self.source.range) - rangeWidth(that.source.range)) |
| 2333 | const unique = Array.from(new Map(candidates.map((candidate) => [candidate.id, candidate])).values()) |
| 2334 | if (unique.length === 1) return { _tag: "Resolved", apiId: unique[0].id, apiFqn: unique[0].apiFqn } |
| 2335 | if (unique.length > 1) { |
| 2336 | return { _tag: "Unresolved", reason: "ambiguous", candidates: unique.map((candidate) => candidate.id) } |
| 2337 | } |
| 2338 | const sameFileByName = (bySourceFile.get(symbol.file) ?? []).filter((api) => apiMatchesSeeTarget(api, names)) |
| 2339 | const uniqueByName = Array.from(new Map(sameFileByName.map((candidate) => [candidate.id, candidate])).values()) |
| 2340 | if (uniqueByName.length === 1) { |
| 2341 | return { _tag: "Resolved", apiId: uniqueByName[0].id, apiFqn: uniqueByName[0].apiFqn } |
| 2342 | } |
| 2343 | if (uniqueByName.length > 1) { |
| 2344 | return { _tag: "Unresolved", reason: "ambiguous", candidates: uniqueByName.map((candidate) => candidate.id) } |
| 2345 | } |
| 2346 | return undefined |
| 2347 | } |
| 2348 | return ( |
| 2349 | context: { readonly moduleName: string; readonly namespacePath: ReadonlyArray<string> }, |
| 2350 | link: ParsedInlineLink |
| 2351 | ): JSDocApiSeeLinkResolution => { |
| 2352 | const symbolResolution = resolveSymbol(link) |
| 2353 | if (symbolResolution !== undefined) return symbolResolution |
| 2354 | const targets = new Set<string>() |
| 2355 | for (const target of normalizeSeeTarget(link.target)) targets.add(target) |
| 2356 | if (link.text !== null) { |
| 2357 | for (const target of normalizeSeeTarget(link.text)) targets.add(target) |
| 2358 | } |
| 2359 | for (const target of targets) { |
| 2360 | const sameModule = byModuleAndName.get(`${context.moduleName}:${target}`) |
| 2361 | if (sameModule !== undefined) return { _tag: "Resolved", apiId: sameModule.id, apiFqn: sameModule.apiFqn } |
no test coverage detected