improv: naming
This commit is contained in:
parent
77989095ba
commit
7f541da7ec
9 changed files with 46 additions and 47 deletions
2
core/index.d.ts
vendored
2
core/index.d.ts
vendored
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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)",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export type CreateRuleBlockArgs = Pick<
|
|||
Required<RuleWithSource>,
|
||||
"rule" | "description" | "alwaysApply" | "name"
|
||||
> &
|
||||
Pick<RuleWithSource, "globs" | "patterns">;
|
||||
Pick<RuleWithSource, "globs" | "regex">;
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue