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/eslint-tier3-custom-plugins.md

View on GitHub
# ESLint Tier 3: Custom Plugins

[← Back to ESLint Rules Discovery](eslint-rules-discovery.md)

## Tier 3: Custom Plugins (LAST RESORT)

Only create a custom ESLint plugin when:
- ❌ No built-in rule exists (checked Tier 1)
- ❌ No configurable rule can express the pattern (checked Tier 2 — see [eslint-tier2-configurable.md](eslint-tier2-configurable.md))
- ✅ The pattern requires checking multiple AST nodes with complex logic
- ✅ The pattern is domain-specific to your codebase

**Examples that justify custom plugins:**
- "Flag LWC components with @api properties but no input validation in connectedCallback"
- "Detect imperative Apex calls without error handling (import + .then() without .catch())"
- "Enforce naming conventions on specific decorator patterns"

**See:** [eslint-custom-plugins.md](eslint-custom-plugins.md) for the plugin creation guide.

---

## Tier-by-Tier Worked Examples

Tier 2 worked examples (banning globals, restricting syntax, restricting properties) live in [eslint-tier2-configurable.md](eslint-tier2-configurable.md). The examples below cover Tier 1 (built-in rules and the LWC plugin) so the discovery → configure → validate flow is documented end-to-end.

### Example 1: "Ban console.log" (Tier 1 — built-in)

**User Request:** "Create a rule to ban console.log in LWC"

**Discovery:**
```bash
$ sf code-analyzer rules --rule-selector eslint | grep -i console
eslint:no-console  disallow the use of console
```

**Result:** Built-in rule exists. ✅

**Configuration:**
```javascript
// eslint.config.js
module.exports = [
  {
    files: ["**/lwc/**/*.js"],
    rules: {
      "no-console": "error"
    }
  }
];
```

**Validation:**
```bash
sf code-analyzer rules --rule-selector eslint:no-console
# Should show: eslint:no-console | error | ...
```

**Test:**
```bash
sf code-analyzer run --rule-selector eslint:no-console --target lwc/
```

---

### Example 2: "Ban innerHTML" (Tier 1 — LWC plugin)

**User Request:** "Ban innerHTML in LWC for XSS prevention"

**Discovery:**
```bash
$ sf code-analyzer rules --rule-selector eslint | grep -i inner
@lwc/lwc/no-inner-html  disallow use of innerHTML
```

**Result:** LWC plugin rule exists. ✅

**Configuration:**
```javascript
// eslint.config.js
module.exports = [
  {
    files: ["**/lwc/**/*.js"],
    rules: {
      "@lwc/lwc/no-inner-html": "error"
    }
  }
];
```

**Validation:**
```bash
sf code-analyzer rules --rule-selector eslint:@lwc/lwc/no-inner-html
```

---

### Example 3: "Enforce === over ==" (Tier 1 — built-in)

**User Request:** "Enforce strict equality checks"

**Discovery:**
```bash
$ sf code-analyzer rules --rule-selector eslint | grep -i equal
eslint:eqeqeq  require the use of === and !==
```

**Result:** Built-in rule `eqeqeq` exists. ✅

**Configuration:**
```javascript
rules: {
  "eqeqeq": ["error", "always"]
}
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated 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: 198kUpdated Claude CodeCodexVerifiedInstall