( filePath: string, query: string, projectNameTokens?: Set<string>, isDeprioritized?: boolean, )
| 388 | * Higher score = more relevant path |
| 389 | */ |
| 390 | export function scorePathRelevance( |
| 391 | filePath: string, |
| 392 | query: string, |
| 393 | projectNameTokens?: Set<string>, |
| 394 | isDeprioritized?: boolean, |
| 395 | ): number { |
| 396 | const prepared = pathRelevanceQuery(query, projectNameTokens); |
| 397 | if (prepared.empty) return 0; |
| 398 | |
| 399 | const pathLower = filePath.toLowerCase(); |
| 400 | const fileName = path.basename(filePath).toLowerCase(); |
| 401 | const dirName = path.dirname(filePath).toLowerCase(); |
| 402 | let score = 0; |
| 403 | |
| 404 | for (const { subtokens, count } of prepared.words) { |
| 405 | // Exact filename match (strongest) |
| 406 | if (subtokens.some((t) => fileName.includes(t))) score += 10 * count; |
| 407 | // Directory match |
| 408 | if (subtokens.some((t) => dirName.includes(t))) score += 5 * count; |
| 409 | // General path match |
| 410 | else if (subtokens.some((t) => pathLower.includes(t))) score += 3 * count; |
| 411 | } |
| 412 | |
| 413 | // Deprioritize test files unless the query is explicitly about tests, and |
| 414 | // apply the same -15 to a path the project declared peripheral (#982). |
| 415 | // |
| 416 | // Two deliberate asymmetries, both pinned by tests: |
| 417 | // - the built-in test/fixture penalty is waived for a test-y query, because |
| 418 | // the tool inferred that classification; a `deprioritize` pattern is a |
| 419 | // standing statement by the project, so it is NOT waived. The name-bonus |
| 420 | // damping at the call site is what keeps such a tree findable. |
| 421 | // - a path that is both is docked ONCE, not twice. |
| 422 | const offTarget = (!prepared.isTestQuery && isTestFile(filePath)) || isDeprioritized === true; |
| 423 | if (offTarget) { |
| 424 | score -= 15; |
| 425 | } |
| 426 | |
| 427 | return score; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Check if a file path looks like a test file |
no test coverage detected