From 000c98627628c8790bc2c606d83635b37e84fc56 Mon Sep 17 00:00:00 2001 From: Colby Mchenry Date: Thu, 1 Oct 2026 03:56:54 +0000 Subject: [PATCH] fix(express): a middleware candidate is a declaration, never an import node (#2244) #2243 put the calling file's candidates first, which let resolveMiddleware pick the file's own `import cors from 'cors'` node; candidates are now functions, methods, values and classes only. Co-authored-by: Claude Opus 5.5 --- src/resolution/frameworks/express.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/resolution/frameworks/express.ts b/src/resolution/frameworks/express.ts index ec8b21d5..f816117e 100644 --- a/src/resolution/frameworks/express.ts +++ b/src/resolution/frameworks/express.ts @@ -480,13 +480,20 @@ function isMiddlewareName(name: string): boolean { /** * Resolve middleware reference using name-based lookup */ +/** Node kinds a middleware can be declared as. */ +const MIDDLEWARE_KINDS: ReadonlySet = new Set(['function', 'method', 'variable', 'constant', 'class']); + function resolveMiddleware( name: string, context: ResolutionContext, fromFile?: string, ): string | null { - // Try exact name first — the calling file's own first. - const candidates = context.getNodesByName(name).sort((a, b) => Number(b.filePath === fromFile) - Number(a.filePath === fromFile)); + // Try exact name first — the calling file's own first. A middleware is a + // declaration (a function, a value, a class), never the file's import of a + // package (`import cors from 'cors'`) nor a heading or a module node. + const candidates = context.getNodesByName(name) + .filter((n) => MIDDLEWARE_KINDS.has(n.kind)) + .sort((a, b) => Number(b.filePath === fromFile) - Number(a.filePath === fromFile)); const match = candidates.find((n) => n.name.toLowerCase() === name.toLowerCase() || n.name.toLowerCase() === name.replace(/Middleware$/i, '').toLowerCase() @@ -496,7 +503,7 @@ function resolveMiddleware( // Try without Middleware suffix const baseName = name.replace(/Middleware$/i, ''); if (baseName !== name) { - const baseCandidates = context.getNodesByName(baseName); + const baseCandidates = context.getNodesByName(baseName).filter((n) => MIDDLEWARE_KINDS.has(n.kind)); const MIDDLEWARE_DIRS = ['/middleware/', '/middlewares/']; const preferred = baseCandidates.filter((n) => MIDDLEWARE_DIRS.some((d) => n.filePath.includes(d))