From 7f541da7ecaec0dee26d51218ccb22fa534e3dc4 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Mon, 23 Jun 2025 09:23:00 -0700 Subject: [PATCH] improv: naming --- core/index.d.ts | 2 +- core/llm/rules/getSystemMessageWithRules.ts | 44 +++++++++---------- .../rules/getSystemMessageWithRules.vitest.ts | 20 ++++----- core/tools/definitions/createRuleBlock.ts | 2 +- core/tools/implementations/createRuleBlock.ts | 8 ++-- docs/docs/customize/deep-dives/rules.mdx | 1 + .../src/markdown/createMarkdownRule.ts | 10 ++--- .../src/markdown/markdownToRule.ts | 4 +- packages/config-yaml/src/schemas/index.ts | 2 +- 9 files changed, 46 insertions(+), 47 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index 1c7f336f2..3dde3bc78 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1576,7 +1576,7 @@ export interface RuleWithSource { slug?: string; source: RuleSource; globs?: string | string[]; - patterns?: string | string[]; + regex?: string | string[]; rule: string; description?: string; ruleFile?: string; diff --git a/core/llm/rules/getSystemMessageWithRules.ts b/core/llm/rules/getSystemMessageWithRules.ts index 62756923e..65c04f2a6 100644 --- a/core/llm/rules/getSystemMessageWithRules.ts +++ b/core/llm/rules/getSystemMessageWithRules.ts @@ -54,33 +54,33 @@ const matchesGlobs = ( }; /** - * Checks if file content matches any of the provided regex patterns + * Checks if file content matches any of the provided regex regex * * @param fileContent - The content of the file to check - * @param patterns - A single regex pattern string or array of regex pattern strings - * @returns true if the content matches any pattern (or if no patterns are provided), false otherwise + * @param regex - A single regex pattern string or array of regex pattern strings + * @returns true if the content matches any pattern (or if no regex is provided), false otherwise */ const contentMatchesPatterns = ( fileContent: string, - patterns: string | string[], + regex: string | string[], ): boolean => { // Handle single string pattern - if (typeof patterns === "string") { + if (typeof regex === "string") { try { - const regex = new RegExp(patterns); - return regex.test(fileContent); + const expression = new RegExp(regex); + return expression.test(fileContent); } catch (e) { - console.error(`Invalid regex pattern: ${patterns}`, e); + console.error(`Invalid regex pattern: ${regex}`, e); return false; } } - // Handle array of patterns - if (Array.isArray(patterns)) { - if (patterns.length === 0) return true; + // Handle array of regex + if (Array.isArray(regex)) { + if (regex.length === 0) return true; // Content must match at least one pattern - return patterns.some((pattern) => { + return regex.some((pattern) => { try { const regex = new RegExp(pattern); return regex.test(fileContent); @@ -145,11 +145,11 @@ const isGlobalRule = (rule: RuleWithSource): boolean => { return true; } - // Root-level rules with no globs or patterns are implicitly global + // Root-level rules with no globs or regex are implicitly global if ( isRootLevelRule(rule) && !rule.globs && - !rule.patterns && + !rule.regex && rule.alwaysApply !== false ) { return true; @@ -176,18 +176,18 @@ const checkGlobsAndPatterns = ({ return false; } - // Now check for pattern matches in file contents if patterns are specified - if (rule.patterns) { - // Check if any of the matching files also match the content patterns + // Now check for pattern matches in file contents if regex are specified + if (rule.regex) { + // Check if any of the matching files also match the content regex return matchingFiles.some((filePath) => { const content = fileContents[filePath]; - // If we don't have the content, we can't check patterns + // If we don't have the content, we can't check regex if (!content) return false; - return contentMatchesPatterns(content, rule.patterns!); + return contentMatchesPatterns(content, rule.regex!); }); } - // If we have no patterns or if we couldn't check patterns (no content), + // If we have no regex or if we couldn't check regex (no content), // just go with the glob matches return matchingFiles.length > 0; }; @@ -248,11 +248,11 @@ export const shouldApplyRule = ( }); } - // If alwaysApply is explicitly false, we need to check globs and/or patterns + // If alwaysApply is explicitly false, we need to check globs and/or regex if ( rule.alwaysApply === false && rule.globs === undefined && - rule.patterns === undefined + rule.regex === undefined ) { return false; } diff --git a/core/llm/rules/getSystemMessageWithRules.vitest.ts b/core/llm/rules/getSystemMessageWithRules.vitest.ts index 8425c8324..0b4444cd1 100644 --- a/core/llm/rules/getSystemMessageWithRules.vitest.ts +++ b/core/llm/rules/getSystemMessageWithRules.vitest.ts @@ -259,7 +259,7 @@ describe("Content pattern matching", () => { const reactComponentRule: RuleWithSource = { name: "React Component Rule", rule: "Follow React component standards", - patterns: "export (function|const) \\w+.*\\(", + regex: "export (function|const) \\w+.*\\(", source: "rules-block", ruleFile: "src/components/rules.md", }; @@ -286,12 +286,12 @@ describe("Content pattern matching", () => { ).toBe(false); }); - it("should support multiple regex patterns as an array", () => { - // Rule with multiple patterns + it("should support multiple regex regex as an array", () => { + // Rule with multiple regex const multiPatternRule: RuleWithSource = { name: "Multi-Pattern Rule", rule: "Apply to functions or classes", - patterns: ["function\\s+\\w+", "class\\s+\\w+"], + regex: ["function\\s+\\w+", "class\\s+\\w+"], source: "rules-block", ruleFile: "src/utils/rules.md", }; @@ -324,13 +324,13 @@ describe("Content pattern matching", () => { ).toBe(false); }); - it("should apply rules based on both globs and patterns when both are specified", () => { + it("should apply rules based on both globs and regex when both are specified", () => { // Rule with both glob and pattern restrictions const typescriptClassRule: RuleWithSource = { name: "TypeScript Class Rule", rule: "Follow class standards in TypeScript", globs: "**/*.ts", - patterns: "class\\s+\\w+", + regex: "class\\s+\\w+", source: "rules-block", ruleFile: "src/models/rules.md", }; @@ -369,7 +369,7 @@ describe("Content pattern matching", () => { const arrowFunctionRule: RuleWithSource = { name: "Arrow Function Rule", rule: "Use arrow functions for better this binding", - patterns: "const\\s+\\w+\\s*=\\s*\\(", + regex: "const\\s+\\w+\\s*=\\s*\\(", source: "rules-block", ruleFile: "src/utils/rules.md", }; @@ -426,12 +426,12 @@ describe("Content pattern matching", () => { expect(nonApplicableRules).toHaveLength(0); }); - it("should handle invalid regex patterns gracefully", () => { + it("should handle invalid regex regex gracefully", () => { // Rule with invalid regex pattern const invalidRegexRule: RuleWithSource = { name: "Invalid Regex Rule", rule: "This rule has an invalid regex pattern", - patterns: "([unclosed parenthesis", + regex: "([unclosed parenthesis", source: "rules-block", ruleFile: "src/utils/rules.md", }; @@ -452,7 +452,7 @@ describe("Content pattern matching", () => { const nestedPatternRule: RuleWithSource = { name: "Nested Pattern Rule", rule: "Apply only to model files with interfaces", - patterns: "interface\\s+\\w+", + regex: "interface\\s+\\w+", source: "rules-block", ruleFile: "src/models/rules.md", }; diff --git a/core/tools/definitions/createRuleBlock.ts b/core/tools/definitions/createRuleBlock.ts index 2afb19d70..dfb6294ae 100644 --- a/core/tools/definitions/createRuleBlock.ts +++ b/core/tools/definitions/createRuleBlock.ts @@ -37,7 +37,7 @@ export const createRuleBlock: Tool = { description: "Optional file patterns to which this rule applies (e.g. ['**/*.{ts,tsx}'] or ['src/**/*.ts', 'tests/**/*.ts'])", }, - patterns: { + regex: { type: "string", description: "Optional regex patterns to match against file content. Rule applies only to files whose content matches the pattern (e.g. 'useEffect' for React hooks or '\\bclass\\b' for class definitions)", diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts index cfa60a5a3..06921f770 100644 --- a/core/tools/implementations/createRuleBlock.ts +++ b/core/tools/implementations/createRuleBlock.ts @@ -7,7 +7,7 @@ export type CreateRuleBlockArgs = Pick< Required, "rule" | "description" | "alwaysApply" | "name" > & - Pick; + Pick; export const createRuleBlockImpl: ToolImpl = async ( args: CreateRuleBlockArgs, @@ -19,9 +19,9 @@ export const createRuleBlockImpl: ToolImpl = async ( globs: args.globs, }; - // Add patterns if provided - if (args.patterns) { - options.patterns = args.patterns; + // Add regex if provided + if (args.regex) { + options.regex = args.regex; } const fileContent = createRuleMarkdown(args.name, args.rule, options); diff --git a/docs/docs/customize/deep-dives/rules.mdx b/docs/docs/customize/deep-dives/rules.mdx index 147606f85..bb0730afd 100644 --- a/docs/docs/customize/deep-dives/rules.mdx +++ b/docs/docs/customize/deep-dives/rules.mdx @@ -63,6 +63,7 @@ Rules blocks can be simple text, written in YAML configuration files, or as Mark - `name` (**required** for YAML): A display name/title for the rule - `globs` (optional): When files are provided as context that match this glob pattern, the rule will be included. This can be either a single pattern (e.g., `"**/*.{ts,tsx}"`) or an array of patterns (e.g., `["src/**/*.ts", "tests/**/*.ts"]`). +- `regex` (optional): When files are provided as context and their content matches this regex pattern, the rule will be included. This can be either a single pattern (e.g., `"^import .* from '.*';$"`) or an array of patterns (e.g., `["^import .* from '.*';$", "^export .* from '.*';$"]`). - `description` (optional): A description for the rule. Agents may read this description when `alwaysApply` is false to determine whether the rule should be pulled into context. - `alwaysApply`: true - Always include the rule, regardless of file context - `alwaysApply`: false - Included if globs exist AND match file context, or the agent decides to pull the rule into context based on it's description diff --git a/packages/config-yaml/src/markdown/createMarkdownRule.ts b/packages/config-yaml/src/markdown/createMarkdownRule.ts index 13cce86f4..0fdf522f8 100644 --- a/packages/config-yaml/src/markdown/createMarkdownRule.ts +++ b/packages/config-yaml/src/markdown/createMarkdownRule.ts @@ -34,7 +34,7 @@ export function createRuleMarkdown( options: { description?: string; globs?: string | string[]; - patterns?: string | string[]; + regex?: string | string[]; alwaysApply?: boolean; } = {}, ): string { @@ -45,11 +45,9 @@ export function createRuleMarkdown( typeof options.globs === "string" ? options.globs.trim() : options.globs; } - if (options.patterns) { - frontmatter.patterns = - typeof options.patterns === "string" - ? options.patterns.trim() - : options.patterns; + if (options.regex) { + frontmatter.regex = + typeof options.regex === "string" ? options.regex.trim() : options.regex; } if (options.description) { diff --git a/packages/config-yaml/src/markdown/markdownToRule.ts b/packages/config-yaml/src/markdown/markdownToRule.ts index 5bed93ac8..85d5b3036 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.ts @@ -7,7 +7,7 @@ import { RuleObject } from "../schemas/index.js"; export interface RuleFrontmatter { globs?: RuleObject["globs"]; - patterns?: RuleObject["patterns"]; + regex?: RuleObject["regex"]; name?: RuleObject["name"]; description?: RuleObject["description"]; alwaysApply?: RuleObject["alwaysApply"]; @@ -57,7 +57,7 @@ export function markdownToRule( name: frontmatter.name ?? packageIdentifierToDisplayName(id), rule: markdown, globs: frontmatter.globs, - patterns: frontmatter.patterns, + regex: frontmatter.regex, description: frontmatter.description, alwaysApply: frontmatter.alwaysApply, }; diff --git a/packages/config-yaml/src/schemas/index.ts b/packages/config-yaml/src/schemas/index.ts index 14d4a7486..7a0a6562b 100644 --- a/packages/config-yaml/src/schemas/index.ts +++ b/packages/config-yaml/src/schemas/index.ts @@ -45,7 +45,7 @@ const ruleObjectSchema = z.object({ rule: z.string(), description: z.string().optional(), globs: z.union([z.string(), z.array(z.string())]).optional(), - patterns: z.union([z.string(), z.array(z.string())]).optional(), + regex: z.union([z.string(), z.array(z.string())]).optional(), alwaysApply: z.boolean().optional(), }); const ruleSchema = z.union([z.string(), ruleObjectSchema]);