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-tier2-configurable.md

View on GitHub
# ESLint Tier 2: Configurable Rules

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

## Tier 2: Configurable Rules

When no built-in rule exists but the pattern is generic (ban a function, ban a property, ban a syntax construct):

### Option A: `no-restricted-globals`

Ban specific global functions or variables.

**Example: Ban setTimeout/setInterval**
```javascript
// eslint.config.js
module.exports = [
  {
    files: ["**/lwc/**/*.js"],
    rules: {
      "no-restricted-globals": ["error",
        {
          name: "setTimeout",
          message: "Use LWC lifecycle hooks instead of setTimeout"
        },
        {
          name: "setInterval",
          message: "Use LWC lifecycle hooks instead of setInterval"
        }
      ]
    }
  }
];
```

**Example: Ban window.localStorage**
```javascript
rules: {
  "no-restricted-globals": ["error",
    {
      name: "localStorage",
      message: "Use Salesforce state management instead of localStorage"
    }
  ]
}
```

### Option B: `no-restricted-syntax`

Ban specific AST patterns using ESLint selectors.

**Example: Ban eval() calls**
```javascript
rules: {
  "no-restricted-syntax": ["error",
    {
      selector: "CallExpression[callee.name='eval']",
      message: "eval() is forbidden for security reasons"
    }
  ]
}
```

**Example: Ban innerHTML property access**
```javascript
rules: {
  "no-restricted-syntax": ["error",
    {
      selector: "MemberExpression[property.name='innerHTML']",
      message: "innerHTML is forbidden - use textContent or render() for XSS prevention"
    }
  ]
}
```

**Example: Ban for-in loops**
```javascript
rules: {
  "no-restricted-syntax": ["error",
    {
      selector: "ForInStatement",
      message: "for-in loops are forbidden - use for-of or Object.keys()"
    }
  ]
}
```

#### ESLint Selector Syntax Cheat Sheet

| Pattern | Selector | Example |
|---|---|---|
| Function call | `CallExpression[callee.name='functionName']` | `eval()`, `alert()` |
| Property access | `MemberExpression[property.name='propName']` | `obj.innerHTML` |
| Statement type | `ForInStatement`, `WithStatement` | for-in, with statements |
| Operator | `BinaryExpression[operator='==']` | `==` instead of `===` |
| Literal value | `Literal[value='string']` | Specific string literals |

**Full selector docs:** https://eslint.org/docs/latest/extend/selectors

### Option C: `no-restricted-properties`

Ban specific object properties.

**Example: Ban Object.prototype methods**
```javascript
rules: {
  "no-restricted-properties": ["error",
    {
      object: "Object",
      property: "setPrototypeOf",
      message: "setPrototypeOf is forbidden for performance reasons"
    }
  ]
}
```
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