( name: string, context: ResolutionContext, fromFile?: string, )
| 671 | const MIDDLEWARE_KINDS: ReadonlySet<string> = new Set(['function', 'method', 'variable', 'constant', 'class']); |
| 672 | |
| 673 | function resolveMiddleware( |
| 674 | name: string, |
| 675 | context: ResolutionContext, |
| 676 | fromFile?: string, |
| 677 | ): string | null { |
| 678 | // Try exact name first — the calling file's own first. A middleware is a |
| 679 | // declaration (a function, a value, a class), never the file's import of a |
| 680 | // package (`import cors from 'cors'`) nor a heading or a module node. |
| 681 | const candidates = context.getNodesByName(name) |
| 682 | .filter((n) => MIDDLEWARE_KINDS.has(n.kind)) |
| 683 | .sort((a, b) => Number(b.filePath === fromFile) - Number(a.filePath === fromFile)); |
| 684 | const match = candidates.find((n) => |
| 685 | n.name.toLowerCase() === name.toLowerCase() || |
| 686 | n.name.toLowerCase() === name.replace(/Middleware$/i, '').toLowerCase() |
| 687 | ); |
| 688 | if (match) return match.id; |
| 689 | |
| 690 | // Try without Middleware suffix |
| 691 | const baseName = name.replace(/Middleware$/i, ''); |
| 692 | if (baseName !== name) { |
| 693 | const baseCandidates = context.getNodesByName(baseName).filter((n) => MIDDLEWARE_KINDS.has(n.kind)); |
| 694 | const MIDDLEWARE_DIRS = ['/middleware/', '/middlewares/']; |
| 695 | const preferred = baseCandidates.filter((n) => |
| 696 | MIDDLEWARE_DIRS.some((d) => n.filePath.includes(d)) |
| 697 | ); |
| 698 | if (preferred.length > 0) return preferred[0]!.id; |
| 699 | if (baseCandidates.length > 0) return baseCandidates[0]!.id; |
| 700 | } |
| 701 | |
| 702 | return null; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Resolve controller method using name-based lookup |
no test coverage detected