Skip to content

forcedotcom/sf-skills / plugins/builder/salesforce-code-quality/skills/dx-code-analyzer-custom-rule-create

dx-code-analyzer-custom-rule-create

Verified

Create 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-method-calls.md

View on GitHub
# XPath Patterns for Method Calls and Annotations

[← Back to XPath Patterns Index](xpath-patterns.md)

## Method Call Patterns

### Ban System.debug (non-test classes)

```xpath
//MethodCallExpression[@FullMethodName='System.debug']
  [not(ancestor::UserClass[ModifierNode[@Test = true()]])]
```

**AST evidence:** `MethodCallExpression` has `@FullMethodName='System.debug'`. Test classes have `ModifierNode[@Test = true()]`.

**Catches:** `System.debug('anything');` in production classes
**Does NOT catch (correct):** Same call inside `@IsTest` classes

### Ban Database.query (dynamic SOQL — SOQL injection risk)

```xpath
//MethodCallExpression[@FullMethodName='Database.query']
```

**AST evidence:** `MethodCallExpression[@FullMethodName='Database.query']` (line 118 of verified AST)

To exclude test classes:
```xpath
//MethodCallExpression[@FullMethodName='Database.query']
  [not(ancestor::UserClass[ModifierNode[@Test = true()]])]
```

### Ban Test.isRunningTest() in production code

```xpath
//MethodCallExpression[@FullMethodName='Test.isRunningTest']
  [not(ancestor::UserClass[ModifierNode[@Test = true()]])]
```

### Ban specific method calls (generic pattern)

```xpath
//MethodCallExpression[@FullMethodName='ClassName.methodName']
```

Common examples:
- `@FullMethodName='System.debug'`
- `@FullMethodName='Database.query'`
- `@FullMethodName='Test.isRunningTest'`
- `@FullMethodName='UserInfo.getUserId'`
- `@FullMethodName='Limits.getQueries'`

---

## Annotation Patterns

### @AuraEnabled without cacheable=true

```xpath
//Method/ModifierNode/Annotation[@Name='AuraEnabled'
  and not(AnnotationParameter[@Name='cacheable' and @Value='true'])]
```

**AST evidence:** `Annotation[@Name='AuraEnabled']` with child `AnnotationParameter[@Name='cacheable'][@Value='true']`

**Catches:** `@AuraEnabled public static ...` (no cacheable)
**Does NOT catch (correct):** `@AuraEnabled(cacheable=true) public static ...`

### @future methods (recommend Queueable instead)

```xpath
//Method/ModifierNode/Annotation[@Name='Future']
```

**AST evidence:** Annotation `@Name='Future'` (note: PMD normalizes `@future` → `Future` in the Name attribute, but `@RawName='future'`)

### @SuppressWarnings usage (audit/ban)

```xpath
//Annotation[@Name='SuppressWarnings']
```

**AST evidence:** `Annotation[@Name='SuppressWarnings']` with `AnnotationParameter[@Name='value'][@Value='PMD.RuleName']`

To flag specific suppressions:
```xpath
//Annotation[@Name='SuppressWarnings']/AnnotationParameter[contains(@Value, 'ApexCRUDViolation')]
```

### @IsTest without testFor parameter

From GitHub issue #2008:
```xpath
//UserClass/ModifierNode/Annotation[
  @Name='IsTest'
  and not(AnnotationParameter[@Name='testFor'])
]
```

### @IsTest class without System.runAs

```xpath
//UserClass[ModifierNode[@Test = true()]]
  /Method[ModifierNode[@Test = true()]]
  /BlockStatement[not(.//RunAsBlockStatement)]
```

**AST evidence:** `RunAsBlockStatement` is the node for `System.runAs(...) { }` blocks.
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 321kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 321kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 321kUpdated Claude CodeCodexVerifiedInstall
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: 197kUpdated Claude CodeCodexVerifiedInstall