From 1703fce6507aad8fa1d283f7735c1399a48dbb14 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Wed, 18 Jun 2025 15:40:38 -0700 Subject: [PATCH] Update getSystemMessageWithRules.ts --- core/llm/rules/getSystemMessageWithRules.ts | 89 ++++++++++++++++++--- 1 file changed, 79 insertions(+), 10 deletions(-) diff --git a/core/llm/rules/getSystemMessageWithRules.ts b/core/llm/rules/getSystemMessageWithRules.ts index 00532bbed..1c4f0f814 100644 --- a/core/llm/rules/getSystemMessageWithRules.ts +++ b/core/llm/rules/getSystemMessageWithRules.ts @@ -149,6 +149,13 @@ const isGlobalRule = (rule: RuleWithSource): boolean => { return false; }; +/** + * Determines if a rule should be applied based on its alwaysApply property and file path matching + * + * @param rule - The rule to check + * @param filePaths - Array of file paths to check against the rule's globs + * @returns true if the rule should be applied, false otherwise + */ /** * Determines if a rule should be applied based on its alwaysApply property and file path matching * @@ -200,31 +207,93 @@ export const shouldApplyRule = ( return false; } - // If we have globs, check if any files in this directory match them - if (rule.globs) { + // Check both globs and patterns + const hasGlobs = rule.globs !== undefined; + const hasPatterns = rule.patterns !== undefined; + + // If we have both globs and patterns, check if any files match both + if (hasGlobs && hasPatterns) { + return filesInRuleDirectory.some( + (filePath) => + matchesGlobs(filePath, rule.globs) && + matchesPatterns(filePath, rule.patterns), + ); + } + + // If we only have globs, check those + if (hasGlobs) { return filesInRuleDirectory.some((filePath) => matchesGlobs(filePath, rule.globs), ); } - // No globs but files are in this directory, so apply the rule + // If we only have patterns, check those + if (hasPatterns) { + return filesInRuleDirectory.some((filePath) => + matchesPatterns(filePath, rule.patterns), + ); + } + + // No globs or patterns but files are in this directory, so apply the rule return true; } // For root-level rules: - // If alwaysApply is explicitly false, only apply if there are globs AND they match + // If alwaysApply is explicitly false, only apply if there are globs AND/OR patterns AND they match if (rule.alwaysApply === false) { - if (!rule.globs) { + const hasGlobs = rule.globs !== undefined; + const hasPatterns = rule.patterns !== undefined; + + if (!hasGlobs && !hasPatterns) { return false; } - return filePaths.some((path) => matchesGlobs(path, rule.globs)); + + // If we have both globs and patterns, check if any files match both + if (hasGlobs && hasPatterns) { + return filePaths.some( + (filePath) => + matchesGlobs(filePath, rule.globs) && + matchesPatterns(filePath, rule.patterns), + ); + } + + // If we only have globs, check those + if (hasGlobs) { + return filePaths.some((filePath) => matchesGlobs(filePath, rule.globs)); + } + + // If we only have patterns, check those + if (hasPatterns) { + return filePaths.some((filePath) => + matchesPatterns(filePath, rule.patterns), + ); + } } - // Default behavior for root rules with globs: - // - Only apply if they match the globs - if (rule.globs) { - return filePaths.some((path) => matchesGlobs(path, rule.globs)); + // Default behavior for root rules with globs and/or patterns: + const hasGlobs = rule.globs !== undefined; + const hasPatterns = rule.patterns !== undefined; + + // If we have both globs and patterns, check if any files match both + if (hasGlobs && hasPatterns) { + return filePaths.some( + (filePath) => + matchesGlobs(filePath, rule.globs) && + matchesPatterns(filePath, rule.patterns), + ); + } + + // If we only have globs, check those + if (hasGlobs) { + return filePaths.some((filePath) => matchesGlobs(filePath, rule.globs)); + } + + // If we only have patterns, check those + if (hasPatterns) { + return filePaths.some((filePath) => + matchesPatterns(filePath, rule.patterns), + ); } // This point should not be reached as we've handled all cases above