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-structure.md
View on GitHub# XPath Patterns for Code Structure, Tests, and Naming
[← Back to XPath Patterns Index](xpath-patterns.md)
## Code Structure Patterns
### DML without try-catch
```xpath
//DmlInsertStatement[not(ancestor::TryCatchFinallyBlockStatement)]
|
//DmlUpdateStatement[not(ancestor::TryCatchFinallyBlockStatement)]
|
//DmlDeleteStatement[not(ancestor::TryCatchFinallyBlockStatement)]
|
//DmlUpsertStatement[not(ancestor::TryCatchFinallyBlockStatement)]
```
**AST evidence:** DML nodes sit inside `TryCatchFinallyBlockStatement` when wrapped in try-catch.
To exclude test classes:
```xpath
//DmlInsertStatement[
not(ancestor::TryCatchFinallyBlockStatement)
and not(ancestor::UserClass[ModifierNode[@Test = true()]])
]
```
### Empty catch blocks (swallowed exceptions)
```xpath
//CatchBlockStatement[BlockStatement[not(*)]]
```
**AST evidence:** `CatchBlockStatement` contains a `BlockStatement`. Empty body = `BlockStatement` with no children.
### Nested if statements (max depth 3)
```xpath
//IfBlockStatement[
ancestor::IfBlockStatement[
ancestor::IfBlockStatement[
ancestor::IfBlockStatement
]
]
]
```
### Methods with too many parameters
```xpath
//Method[@Arity >= 5 and @Constructor = false()]
```
**AST evidence:** `Method[@Arity]` gives parameter count directly.
### Logic in trigger (should delegate to handler)
For trigger files (UserTrigger node):
```xpath
//UserTrigger/BlockStatement//DmlInsertStatement
|
//UserTrigger/BlockStatement//DmlUpdateStatement
|
//UserTrigger/BlockStatement//SoqlExpression
|
//UserTrigger/BlockStatement//MethodCallExpression[@FullMethodName='Database.query']
```
---
## Test Quality Patterns
### Test method without assertions
```xpath
//Method[ModifierNode[@Test = true()]]
/BlockStatement[
not(.//MethodCallExpression[
contains(@FullMethodName, 'System.assert')
or contains(@FullMethodName, 'Assert.')
])
]
```
**Catches:** Test methods with no `System.assert*` or `Assert.*` calls anywhere in their body.
### Test method without System.runAs
```xpath
//Method[ModifierNode[@Test = true()]]
/BlockStatement[not(.//RunAsBlockStatement)]
```
**AST evidence:** `System.runAs(user) { ... }` becomes `RunAsBlockStatement` in the AST.
### Test using seeAllData=true
```xpath
//Annotation[@Name='IsTest']/AnnotationParameter[@Name='seeAllData' and @Value='true']
```
---
## Naming Convention Patterns
### Class name doesn't match file (non-test)
This is better enforced by the built-in rule, but the XPath pattern for a specific convention:
```xpath
//UserClass[@Nested = false() and not(starts-with(@Image, 'Test')) and not(ends-with(@Image, 'Test'))]
```
### Method naming (non-standard)
```xpath
//Method[
@Constructor = false()
and not(ModifierNode[@Test = true()])
and not(starts-with(@Image, 'get'))
and not(starts-with(@Image, 'set'))
and not(starts-with(@Image, 'is'))
and matches(@Image, '^[A-Z]')
]
```
**Catches:** Methods starting with uppercase in non-test code (Apex convention is camelCase).
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: 321k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 321k | 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: 197k | Updated | Claude CodeCodex | Verified | Install |