* Resolve a component reference using name-based lookup
( name: string, fromFile: string, context: ResolutionContext )
| 2438 | * Resolve a component reference using name-based lookup |
| 2439 | */ |
| 2440 | function resolveComponent( |
| 2441 | name: string, |
| 2442 | fromFile: string, |
| 2443 | context: ResolutionContext |
| 2444 | ): string | null { |
| 2445 | const candidates = context.getNodesByName(name); |
| 2446 | if (candidates.length === 0) return null; |
| 2447 | |
| 2448 | const components = candidates.filter((n) => COMPONENT_KINDS.has(n.kind)); |
| 2449 | if (components.length === 0) return null; |
| 2450 | |
| 2451 | // Prefer the referencing file's folder, or one below it |
| 2452 | const sameDir = inReferencingFolder(fromFile, components, (n) => n.filePath); |
| 2453 | if (sameDir.length > 0) return sameDir[0]!.id; |
| 2454 | |
| 2455 | // Prefer component directories |
| 2456 | const COMPONENT_DIRS = ['/components/', '/src/components/', '/app/components/', '/pages/', '/src/pages/', '/views/', '/src/views/']; |
| 2457 | const preferred = components.filter((n) => |
| 2458 | COMPONENT_DIRS.some((d) => n.filePath.includes(d)) |
| 2459 | ); |
| 2460 | if (preferred.length > 0) return preferred[0]!.id; |
| 2461 | |
| 2462 | // No positional signal: only an UNAMBIGUOUS name may resolve. Returning |
| 2463 | // components[0] here picked an arbitrary same-named class anywhere in the |
| 2464 | // repo (#764) — let the name-matcher's proximity scoring decide instead. |
| 2465 | return components.length === 1 ? components[0]!.id : null; |
| 2466 | } |
| 2467 | |
| 2468 | /** JS/TS (and their JSX dialects): modules where a cross-file name needs an import. */ |
| 2469 | function isEsmLanguage(language?: string): boolean { |
no test coverage detected