forcedotcom/sf-skills / plugins/builder/salesforce-code-quality/skills/dx-code-analyzer-custom-rule-create
dx-code-analyzer-custom-rule-create
VerifiedCreate custom Code Analyzer rules for Regex (pattern matching), PMD (XPath/AST for Apex and metadata XML), and ESLint (LWC/JavaScript/TypeScript). Use when users want to enforce coding standards, ban patterns, detect hardcoded values, govern metadata, or add rules not in the built-in set. TRIGGER when: user says 'create a rule', 'ban System.debug', 'enforce naming convention', 'detect hardcoded IDs', 'custom rule', 'xpath rule', 'regex rule', 'add a PMD rule', 'enforce a policy', 'create a check for', 'flag this pattern', 'make a rule that catches', 'metadata rule', 'check permissions', 'enforce API version', 'eslint rule', 'lwc rule', 'override rule threshold', 'customize complexity', or describes a pattern to enforce. DO NOT TRIGGER when: user wants to run a scan (use dx-code-analyzer-run), configure engines (use dx-code-analyzer-configure), or explain existing rules (use dx-code-analyzer-run).
references/xpath-patterns-governor-limits.md
View on GitHub# XPath Patterns for Governor Limits
[← Back to XPath Patterns Index](xpath-patterns.md)
## Governor Limit Patterns
### SOQL inside loops
⚠️ **Must scope to `/BlockStatement//`** — ForEachStatement has the iterable SOQL as a direct child alongside BlockStatement. Without this, `for (Contact c : [SELECT...])` is a false positive.
**ForEachStatement child structure (from ast-dump):**
```text
ForEachStatement
├── VariableDeclarationStatements ← loop variable declaration
├── VariableExpression ← loop variable reference
├── BlockStatement ← LOOP BODY — only match here
└── SoqlExpression (or VariableExpression) ← ITERABLE — do NOT flag
```
```xpath
//ForEachStatement/BlockStatement//SoqlExpression
|
//ForLoopStatement/BlockStatement//SoqlExpression
|
//WhileLoopStatement/BlockStatement//SoqlExpression
```
**Catches:** `for (Id accId : ids) { Account acc = [SELECT ...]; }` (SOQL in body)
**Does NOT catch (correct):** `for (Contact c : [SELECT Id FROM Contact]) { ... }` (SOQL as iterable)
### DML inside loops
Same principle — scope to BlockStatement:
```xpath
//ForEachStatement/BlockStatement//DmlInsertStatement
|
//ForEachStatement/BlockStatement//DmlUpdateStatement
|
//ForEachStatement/BlockStatement//DmlDeleteStatement
|
//ForEachStatement/BlockStatement//DmlUpsertStatement
|
//ForLoopStatement/BlockStatement//DmlInsertStatement
|
//ForLoopStatement/BlockStatement//DmlUpdateStatement
|
//ForLoopStatement/BlockStatement//DmlDeleteStatement
|
//ForLoopStatement/BlockStatement//DmlUpsertStatement
|
//WhileLoopStatement/BlockStatement//DmlInsertStatement
|
//WhileLoopStatement/BlockStatement//DmlUpdateStatement
|
//WhileLoopStatement/BlockStatement//DmlDeleteStatement
|
//WhileLoopStatement/BlockStatement//DmlUpsertStatement
```
**Shorter variant** (ForEach only, most common):
```xpath
//ForEachStatement/BlockStatement//DmlInsertStatement
| //ForEachStatement/BlockStatement//DmlUpdateStatement
| //ForEachStatement/BlockStatement//DmlDeleteStatement
| //ForEachStatement/BlockStatement//DmlUpsertStatement
```
### Database methods inside loops
```xpath
//ForEachStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.query']
|
//ForEachStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.insert']
|
//ForEachStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.update']
|
//ForEachStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.delete']
|
//ForLoopStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.query']
|
//WhileLoopStatement/BlockStatement//MethodCallExpression[@FullMethodName='Database.query']
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |