The file of the unit a name names, nearest to `fromFile` when several units share it.
(lower: string, fromFile: string, context: ResolutionContext)
| 2505 | |
| 2506 | /** The file of the unit a name names, nearest to `fromFile` when several units share it. */ |
| 2507 | function unitFileNamed(lower: string, fromFile: string, context: ResolutionContext): string | null { |
| 2508 | const m = memos(context); |
| 2509 | const key = `${fromFile}|${lower}`; |
| 2510 | if (m.unitFiles.has(key)) return m.unitFiles.get(key)!; |
| 2511 | if (!m.unitsByName) { |
| 2512 | const units = new Map<string, string[]>(); |
| 2513 | const add = (name: string, file: string): void => { |
| 2514 | const files = units.get(name); |
| 2515 | if (files) files.push(file); |
| 2516 | else units.set(name, [file]); |
| 2517 | }; |
| 2518 | const headed = new Set<string>(); |
| 2519 | for (const n of context.getNodesByKind('module')) { |
| 2520 | if (n.language !== 'pascal') continue; |
| 2521 | add(n.name.toLowerCase(), n.filePath); |
| 2522 | headed.add(n.filePath); |
| 2523 | } |
| 2524 | // A unit's file is named after it, which Delphi enforces: one whose |
| 2525 | // `unit` header the parser lost to an error is the unit its file names. |
| 2526 | for (const file of context.getAllFiles()) { |
| 2527 | if (headed.has(file) || !/\.pas$/i.test(file)) continue; |
| 2528 | add(file.slice(file.lastIndexOf('/') + 1, -'.pas'.length).toLowerCase(), file); |
| 2529 | } |
| 2530 | m.unitsByName = units; |
| 2531 | } |
| 2532 | const file = nearest(m.unitsByName.get(lower) ?? [], fromFile); |
| 2533 | m.unitFiles.set(key, file); |
| 2534 | return file; |
| 2535 | } |
| 2536 | |
| 2537 | /** A class (or record or interface) a type name names, seen from `fromFile`. */ |
| 2538 | function classNamed(typeName: string, fromFile: string, context: ResolutionContext): Node | null { |
no test coverage detected