Sub topic 4
Listing rules and choosing a rule selector
This chapter assumes you have installed the Salesforce CLI and the code-analyzer plugin, and that you know what an engine is. Before you scan, you need to decide which rules to run. A rule selector is how you tell Code Analyzer to make that choice.
What a rule selector is
Code Analyzer ships with hundreds of rules across its engines. Running all of them on every scan produces a lot of noise. A rule selector is an expression that picks a subset of those rules.
You pass a selector with the --rule-selector flag. It works with both sf code-analyzer rules (to list rules) and sf code-analyzer run (to scan). Using the same selector in both commands lets you preview exactly what a scan will check.
Listing rules
The rules command prints each rule that matches your selector. It shows the rule name, its engine, its severity, and its tags.
sf code-analyzer rules --rule-selector RecommendedEach rule has these properties:
- Name: the rule's identifier, such as
AvoidDebugStatements. - Engine: the engine that provides the rule, such as
pmdoreslint. - Severity: a level from 1 (Critical) to 5 (Info).
- Tags: labels that group rules, such as
Security,Performance, orRecommended.
Selector syntax
A selector is built from small parts. Each part can be a tag, an engine name, a rule name, or a combination.
| Selector | What it selects |
|---|---|
| `all` | Every rule from every enabled engine |
| `Recommended` | Rules tagged Recommended |
| `Security` | Rules tagged Security, from any engine |
| `pmd` | All rules from the PMD engine |
| `pmd:Security` | PMD rules tagged Security |
| `pmd:AvoidDebugStatements` | One specific PMD rule |
Use a colon to narrow by engine. The part before the colon is the engine, and the part after it is a tag or rule name.
You can also combine parts:
- A comma means "or".
pmd,eslintselects every rule from both engines. - Parentheses group parts.
(pmd,eslint):Securityselects Security rules from either engine. - A colon between two parts means "and".
Security:Recommendedselects rules that carry both tags.
sf code-analyzer rules --rule-selector "(pmd,eslint):Security"Quote selectors that contain parentheses. Your shell would otherwise try to interpret them.
How the selector flows through a scan
Choosing a selector
Pick the narrowest selector that answers your question.
- Starting out or running in CI: use
Recommended. It is a curated set that balances coverage against false positives. - Focused review: use a tag such as
pmd:Securitywhen you are auditing one concern. - Targeted fix: use a single rule name when you are cleaning up one kind of violation across a codebase.
- Broad audit: use
allonly for a one-off review. Expect many findings that you will not act on.
Common errors
- No rules listed. The selector matched nothing, often because of a typo in a tag or rule name. Tags and engine names are case-insensitive in most cases, but rule names should match exactly.
- Shell errors with parentheses. Wrap the selector in quotes.
- A rule you expect is missing. Its engine may be disabled in your configuration. The next chapters cover configuration.
Exercise
You want to scan only for PMD rules tagged Security. Which selector do you use, and how can you check what it selects before scanning?
Show answer
Use pmd:Security. Check it by running sf code-analyzer rules --rule-selector pmd:Security, which lists every rule that the selector matches. Then pass the same selector to sf code-analyzer run.