Rules Reference
Built-in Rules
AI Guard includes 5 built-in rules that detect common AI-generated code patterns:
1. excessive-comments
Detects files where the ratio of comment lines to total lines exceeds the configured threshold.
| Property | Value |
|---|---|
| Rule Name | excessive-comments |
| Default Threshold | 15% (0.15) |
| Configurable | Yes (maxCommentsRatio) |
| Severity | error |
Example violation:
// This function processes user data// It takes a user object as input// Then it validates the data// After validation it returns the resultfunction processUser(user) { // First we check if user exists if (!user) { // No user provided return null; } // Continue processing...}
// Line 1: [excessive-comments] Comment ratio 45% exceeds threshold of 15%How to fix: Remove redundant comments and let code speak for itself.
2. obvious-comments
Flags comments that simply restate what the code does, adding no value.
| Property | Value |
|---|---|
| Rule Name | obvious-comments |
| Pattern Detection | AST-based + heuristics |
| Configurable | No |
| Severity | error |
Common obvious comment patterns:
// Loop through usersfor (const user of users) { // Active users only if (user.isActive) { // Add to active list activeUsers.push(user); }}
// All three comments are obvious - the code is self-explanatoryHow to fix: Remove comments that don't add information beyond what's in the code.
3. generic-names
Detects use of generic variable and function names that don't convey meaning.
| Property | Value |
|---|---|
| Rule Name | generic-names |
| Default List | data, result, item, temp, value, obj, arr, info, content, response |
| Configurable | Yes (forbidGenericNames) |
| Severity | error |
Example violations:
// Bad: generic namesasync function fetchData() { const response = await api.get("/users"); const data = response.data; const result = processData(data); return result;}
// Good: descriptive namesasync function fetchUsers() { const apiResponse = await api.get("/users"); const users = apiResponse.data; const validatedUsers = validateUsers(users); return validatedUsers;}How to fix: Use domain-specific, descriptive names that convey the data's purpose.
4. large-functions
Flags functions that exceed the maximum line count threshold.
| Property | Value |
|---|---|
| Rule Name | large-functions |
| Default Threshold | 60 lines |
| Configurable | Yes (maxFunctionLines) |
| Severity | error |
Example violation:
function processOrder(order) { // 80 lines of code... // Multiple responsibilities // Hard to test and maintain}
// Line 1: [large-functions] Function 'processOrder' has 80 lines (max: 60)How to fix: Break large functions into smaller, focused functions with single responsibilities.
5. ai-patterns
Detects phrases commonly used by AI code generators that indicate machine-generated content.
| Property | Value |
|---|---|
| Rule Name | ai-patterns |
| Default Patterns | this function, we will, we need to, let's, in order to, going to, simply, just |
| Configurable | Yes (aiPatterns.forbid) |
| Severity | error |
Common AI phrase examples:
// This function will process the user datafunction processUserData(user) { // First, we need to validate the input if (!user) { return null; }
// Next step: transform into output format const transformed = transform(user);
// Finally, in order to complete the process, we return the result return transformed;}
// Multiple AI-typical phrases detectedHow to fix: Rewrite comments in a more natural, human style, or remove them if they're not needed.
Configuring Rules
Adjust rule thresholds in your configuration file:
export default defineConfig({ // Allow more comments in documentation-heavy projects maxCommentsRatio: 0.20,
// Stricter function length for microservices maxFunctionLines: 40,
// Add project-specific generic names to avoid forbidGenericNames: [ 'data', 'result', 'item', 'temp', 'value', 'myVar', // custom 'helper', // custom ],
// Add custom AI patterns specific to your LLM aiPatterns: { forbid: [ 'this function', 'we will', 'we need to', 'as mentioned above', // custom 'it is worth noting', // custom ], },});