Files
claude-mem/src/utils/project-filter.ts
T
Alex Newman a0dd516cd5 fix: resolve all 301 error handling anti-patterns across codebase
Systematic cleanup of every error handling anti-pattern detected by the
automated scanner. 289 issues fixed via code changes, 12 approved with
specific technical justifications.

Changes across 90 files:
- GENERIC_CATCH (141): Added instanceof Error type discrimination
- LARGE_TRY_BLOCK (82): Extracted helper methods to narrow try scope to ≤10 lines
- NO_LOGGING_IN_CATCH (65): Added logger/console calls for error visibility
- CATCH_AND_CONTINUE_CRITICAL_PATH (10): Added throw/return or approved overrides
- ERROR_STRING_MATCHING (2): Approved with rationale (no typed error classes)
- ERROR_MESSAGE_GUESSING (1): Replaced chained .includes() with documented pattern array
- PROMISE_CATCH_NO_LOGGING (1): Added logging to .catch() handler

Also fixes a detector bug where nested try/catch inside a catch block
corrupted brace-depth tracking, causing false positives.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-19 19:57:00 -07:00

75 lines
2.2 KiB
TypeScript

/**
* Project Filter Utility
*
* Provides glob-based path matching for project exclusion.
* Supports: ~ (home), * (any chars except /), ** (any path), ? (single char)
*/
import { homedir } from 'os';
/**
* Convert a glob pattern to a regular expression
* Supports: ~ (home dir), * (any non-slash), ** (any path), ? (single char)
*/
function globToRegex(pattern: string): RegExp {
// Expand ~ to home directory
let expanded = pattern.startsWith('~')
? homedir() + pattern.slice(1)
: pattern;
// Normalize path separators to forward slashes
expanded = expanded.replace(/\\/g, '/');
// Escape regex special characters except * and ?
let regex = expanded.replace(/[.+^${}()|[\]\\]/g, '\\$&');
// Convert glob patterns to regex:
// ** matches any path (including /)
// * matches any characters except /
// ? matches single character except /
regex = regex
.replace(/\*\*/g, '<<<GLOBSTAR>>>') // Temporary placeholder
.replace(/\*/g, '[^/]*') // * = any non-slash
.replace(/\?/g, '[^/]') // ? = single non-slash
.replace(/<<<GLOBSTAR>>>/g, '.*'); // ** = anything
return new RegExp(`^${regex}$`);
}
/**
* Check if a path matches any of the exclusion patterns
*
* @param projectPath - Current working directory (absolute path)
* @param exclusionPatterns - Comma-separated glob patterns (e.g., "~/kunden/*,/tmp/*")
* @returns true if path should be excluded
*/
export function isProjectExcluded(projectPath: string, exclusionPatterns: string): boolean {
if (!exclusionPatterns || !exclusionPatterns.trim()) {
return false;
}
// Normalize cwd path separators
const normalizedProjectPath = projectPath.replace(/\\/g, '/');
// Parse comma-separated patterns
const patternList = exclusionPatterns
.split(',')
.map(p => p.trim())
.filter(Boolean);
for (const pattern of patternList) {
try {
const regex = globToRegex(pattern);
if (regex.test(normalizedProjectPath)) {
return true;
}
} catch (error: unknown) {
// Invalid pattern, skip it
console.warn(`[project-filter] Invalid exclusion pattern "${pattern}":`, error instanceof Error ? error.message : String(error));
continue;
}
}
return false;
}