The file of the unit a name names, nearest to `fromFile` when several units share it.
(lower: string, fromFile: string, context: ResolutionContext)
| 969 | |
| 970 | /** The file of the unit a name names, nearest to `fromFile` when several units share it. */ |
| 971 | function unitFileNamed(lower: string, fromFile: string, context: ResolutionContext): string | null { |
| 972 | const m = memos(context); |
| 973 | const key = `${fromFile}|${lower}`; |
| 974 | if (m.unitFiles.has(key)) return m.unitFiles.get(key)!; |
| 975 | if (!m.unitsByName) { |
| 976 | const units = new Map<string, string[]>(); |
| 977 | const add = (name: string, file: string): void => { |
| 978 | const files = units.get(name); |
| 979 | if (files) files.push(file); |
| 980 | else units.set(name, [file]); |
| 981 | }; |
| 982 | const headed = new Set<string>(); |
| 983 | for (const n of context.getNodesByKind('module')) { |
| 984 | if (n.language !== 'pascal') continue; |
| 985 | add(n.name.toLowerCase(), n.filePath); |
| 986 | headed.add(n.filePath); |
| 987 | } |
| 988 | // A unit's file is named after it, which Delphi enforces: one whose |
| 989 | // `unit` header the parser lost to an error is the unit its file names. |
| 990 | for (const file of context.getAllFiles()) { |
| 991 | if (headed.has(file) || !/\.pas$/i.test(file)) continue; |
| 992 | add(file.slice(file.lastIndexOf('/') + 1, -'.pas'.length).toLowerCase(), file); |
| 993 | } |
| 994 | m.unitsByName = units; |
| 995 | } |
| 996 | const file = nearest(m.unitsByName.get(lower) ?? [], fromFile); |
| 997 | m.unitFiles.set(key, file); |
| 998 | return file; |
| 999 | } |
| 1000 | |
| 1001 | /** A class (or record or interface) a type name names, seen from `fromFile`. */ |
| 1002 | function classNamed(typeName: string, fromFile: string, context: ResolutionContext): Node | null { |
no test coverage detected