feat: Negation patterns (#24)

This commit is contained in:
Felix Rieseberg 2025-06-28 16:11:32 -07:00 • committed by GitHub
commit e192e488c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -60,60 +60,89 @@ export function readDxtIgnorePatterns(baseDir: string): string[] {
}
}
/**
* Tests if a file path matches a given pattern
*/
function matchesPattern(
pattern: string,
filePath: string,
fileName: string,
): boolean {
if (pattern.includes("*")) {
// Enhanced glob matching to handle directory paths like "temp/*"
let patternToMatch = pattern;
// Handle patterns like "dir/" by converting to "dir/**"
if (pattern.endsWith("/")) {
patternToMatch = pattern + "**";
}
// Convert glob pattern to regex, with case sensitivity
const regexPattern =
"^" +
patternToMatch
.replace(/\./g, "\\.") // Escape dots
.replace(/\*\*/g, ".*") // ** matches anything including /
.replace(/\*/g, "[^/]*") + // * matches anything except /
"$";
const regex = new RegExp(regexPattern);
// Test full path
if (regex.test(filePath)) return true;
// Test filename
if (regex.test(fileName)) return true;
// Check if any part of the path matches for patterns like "node_modules"
const pathParts = filePath.split(sep);
for (const part of pathParts) {
if (regex.test(part)) return true;
}
} else {
// Exact match
if (fileName === pattern) return true;
// Check if any part of the path matches
if (filePath.includes(pattern)) return true;
// Handle directory patterns like "tests/"
if (pattern.endsWith("/") && filePath.startsWith(pattern)) {
return true;
}
}
return false;
}
export function shouldExclude(
filePath: string,
additionalPatterns: string[] = [],
): boolean {
const fileName = basename(filePath);
const allPatterns = [...EXCLUDE_PATTERNS, ...additionalPatterns];
const negationPatterns = allPatterns.filter((p) => p.startsWith("!"));
const exclusionPatterns = allPatterns.filter((p) => !p.startsWith("!"));
for (const pattern of allPatterns) {
if (pattern.includes("*")) {
// Enhanced glob matching to handle directory paths like "temp/*"
let patternToMatch = pattern;
let shouldExcludeFile = false;
for (const pattern of exclusionPatterns) {
if (matchesPattern(pattern, filePath, fileName)) {
shouldExcludeFile = true;
break;
}
}
// Handle patterns like "dir/" by converting to "dir/**"
if (pattern.endsWith("/")) {
patternToMatch = pattern + "**";
}
// Convert glob pattern to regex, with case sensitivity
const regexPattern =
"^" +
patternToMatch
.replace(/\./g, "\\.") // Escape dots
.replace(/\*\*/g, ".*") // ** matches anything including /
.replace(/\*/g, "[^/]*") + // * matches anything except /
"$";
const regex = new RegExp(regexPattern);
// Test full path
if (regex.test(filePath)) return true;
// Test filename
if (regex.test(fileName)) return true;
// Check if any part of the path matches for patterns like "node_modules"
const pathParts = filePath.split(sep);
for (const part of pathParts) {
if (regex.test(part)) return true;
}
} else {
// Exact match
if (fileName === pattern) return true;
// Check if any part of the path matches
if (filePath.includes(pattern)) return true;
// Handle directory patterns like "tests/"
if (pattern.endsWith("/") && filePath.startsWith(pattern)) {
return true;
// If excluded, check if any negation pattern un-excludes it
if (shouldExcludeFile) {
for (const pattern of negationPatterns) {
const negatedPattern = pattern.slice(1);
if (matchesPattern(negatedPattern, filePath, fileName)) {
// File should not be excluded (negation pattern matched)
return false;
}
}
}
return false;
return shouldExcludeFile;
}
export function getAllFiles(