* Resolve a component reference using name-based lookup
( name: string, fromFile: string, context: ResolutionContext )
| 2416 | * Resolve a component reference using name-based lookup |
| 2417 | */ |
| 2418 | function resolveComponent( |
| 2419 | name: string, |
| 2420 | fromFile: string, |
| 2421 | context: ResolutionContext |
| 2422 | ): string | null { |
| 2423 | const candidates = context.getNodesByName(name); |
| 2424 | if (candidates.length === 0) return null; |
| 2425 | |
| 2426 | const components = candidates.filter((n) => COMPONENT_KINDS.has(n.kind)); |
| 2427 | if (components.length === 0) return null; |
| 2428 | |
| 2429 | // Prefer the referencing file's folder, or one below it |
| 2430 | const sameDir = inReferencingFolder(fromFile, components, (n) => n.filePath); |
| 2431 | if (sameDir.length > 0) return sameDir[0]!.id; |
| 2432 | |
| 2433 | // Prefer component directories |
| 2434 | const COMPONENT_DIRS = ['/components/', '/src/components/', '/app/components/', '/pages/', '/src/pages/', '/views/', '/src/views/']; |
| 2435 | const preferred = components.filter((n) => |
| 2436 | COMPONENT_DIRS.some((d) => n.filePath.includes(d)) |
| 2437 | ); |
| 2438 | if (preferred.length > 0) return preferred[0]!.id; |
| 2439 | |
| 2440 | // No positional signal: only an UNAMBIGUOUS name may resolve. Returning |
| 2441 | // components[0] here picked an arbitrary same-named class anywhere in the |
| 2442 | // repo (#764) — let the name-matcher's proximity scoring decide instead. |
| 2443 | return components.length === 1 ? components[0]!.id : null; |
| 2444 | } |
| 2445 | |
| 2446 | /** JS/TS (and their JSX dialects): modules where a cross-file name needs an import. */ |
| 2447 | function isEsmLanguage(language?: string): boolean { |
no test coverage detected