Sub topic 4
Listing rules and choosing a rule selector
Before you scan a project, decide which rules the scan should run. Code Analyzer ships with hundreds of rules across several engines, and a rule selector is the expression that picks a subset of them. This chapter shows how to list rules with sf code-analyzer rules and how to write selectors for the rules and run commands.
It assumes you have the code-analyzer plugin installed (see the previous chapter) and that you know the engines behind the rules: PMD, ESLint, RetireJS, Regex, Flow, and CPD. You should be comfortable running sf commands from the root of a Salesforce DX project.
What a rule carries
Every rule has four pieces of metadata that selectors can match on:
- Engine: the tool that runs the rule, such as
pmdoreslint. - Name: the rule's identifier within its engine, such as
AvoidDebugStatements. - Severity: a number from 1 to 5. 1 is Critical, 2 is High, 3 is Moderate, 4 is Low, and 5 is Info.
- Tags: labels that group rules, such as
Recommended,Security,Performance,BestPractices,CodeStyle,Apex, orJavaScript.
A selector is a filter over these four fields. The same selector syntax works for sf code-analyzer rules (which lists matches) and sf code-analyzer run (which executes them). You can test a selector with rules first and reuse it unchanged in run.
Listing rules
The basic command is:
sf code-analyzer rules --rule-selector RecommendedRecommended is a tag that Salesforce applies to a curated set of rules. It is a good starting point because it favors rules with a useful signal-to-noise ratio. If you omit --rule-selector, the command uses Recommended.
To see everything that is available, use all:
sf code-analyzer rules --rule-selector allThe output is a table with the rule name, engine, severity, and tags. For the full description of each rule, ask for the detail view:
sf code-analyzer rules --rule-selector pmd:Security --view detailYou can also write the list to a file with --output-file, which is useful when you want to review or share the rule set.
sf code-analyzer rules --rule-selector Recommended --output-file rules.jsonThe file format follows the file extension. Extensions such as .json and .csv are supported by the rules command.
The selector grammar
A selector is built from terms. Each term matches rules by one field, and operators combine terms.
| Selector | Matches |
|---|---|
| `all` | Every rule from every enabled engine |
| `Recommended` | Rules carrying the `Recommended` tag |
| `pmd` | Every rule from the PMD engine |
| `Security` | Rules tagged `Security`, from any engine |
| `AvoidDebugStatements` | The rule with that name |
| `pmd:Security` | PMD rules tagged `Security` |
| `2` or `High` | Rules with severity 2 |
| `pmd,eslint` | Rules from PMD or ESLint |
| `(pmd,eslint):Security` | Security-tagged rules from either engine |
Three operators matter:
- A colon means "and".
pmd:Securityis the intersection of the PMD rules and the Security rules. - A comma means "or".
pmd,eslintis the union of the two engines. - Parentheses group terms so you can mix the two.
(pmd,eslint):Securityis evaluated as "(PMD or ESLint) and Security".
You can also pass --rule-selector more than once. The results are combined as a union, so these two commands select the same rules:
sf code-analyzer rules --rule-selector pmd:Security --rule-selector eslint:Security
sf code-analyzer rules --rule-selector "pmd:Security,eslint:Security"Use whichever reads better. Repeated flags are easier to maintain in scripts, because each line is one idea.
Quoting selectors in the shell
Parentheses and commas are significant to many shells. Bash treats unquoted parentheses as syntax, so this fails:
sf code-analyzer rules --rule-selector (pmd,eslint):SecurityWrap selectors that contain parentheses in quotes:
sf code-analyzer rules --rule-selector "(pmd,eslint):Security"Quote every selector in scripts and CI files, even simple ones. It costs nothing and prevents surprises when someone later adds an operator.
Choosing a selector by goal
Different jobs call for different selectors. These are common patterns.
Day-to-day development. Use Recommended. It is the default and keeps noise low.
A security review. Select the Security tag across engines:
sf code-analyzer rules --rule-selector SecurityThis pulls in security rules from PMD (for example, rules about SOQL injection and CRUD/FLS checks) and from other engines that tag rules the same way.
Only the worst findings. Select by severity. Note that a bare severity selector matches exactly that severity, not "that severity or worse", so list the levels you want:
sf code-analyzer rules --rule-selector "1,2"For gating a build on severity, the --severity-threshold flag (covered in a later chapter) is usually the better tool, because it filters the results rather than the rules that run.
One language. Language tags such as Apex or JavaScript restrict a scan to rules for that language:
sf code-analyzer rules --rule-selector "Recommended:Apex"This reads as "Recommended and Apex", so you get the recommended Apex rules only.
A single known rule. When you are investigating one finding, select by name to make the output short:
sf code-analyzer rules --rule-selector pmd:AvoidDebugStatements --view detailRules that depend on your project
Some engines choose rules based on the files they see. The ESLint engine, for example, can pick up your project's ESLint configuration, and the Regex engine and others can be customized in code-analyzer.yml. If you run rules outside a project, or without telling it where your code is, the list can differ from what a scan finds. Pass the same workspace you will scan:
sf code-analyzer rules --workspace force-app --rule-selector RecommendedThe --workspace flag is described fully in the next chapters. For now, treat it as part of the rule-listing command whenever you want the list to match a real scan.
Common mistakes
- Expecting
Recommendedto mean "everything good". It is a curated subset. Rules outside it exist and may suit your team. Browse withallonce, then decide. - Using a space after a comma. In a quoted selector, keep terms tight:
"pmd,eslint". Spaces can be treated as part of a term or cause parsing errors, depending on context. - Confusing
:with,.pmd:eslintselects the intersection of two engines, which is empty. You almost certainly wantedpmd,eslint. - Assuming tags are unique to an engine. A tag like
Securityspans engines. Add an engine prefix when you need only one. - Forgetting that a rule selector in the config file is separate. Settings in
code-analyzer.ymlchange how rules behave (severity, tags). They do not choose which rules run. The selector on the command line does that.
Exercise
You want only PMD and ESLint rules that are tagged Security, and you are writing the command in Bash. Write the sf code-analyzer rules command, and explain why pmd:eslint:Security would be the wrong selector.
Show answer
Use sf code-analyzer rules --rule-selector "(pmd,eslint):Security". The comma makes a union of the two engines, the parentheses group that union, and the colon intersects it with the Security tag. The quotes stop Bash from interpreting the parentheses. pmd:eslint:Security uses colons throughout, which means "and", so it asks for rules that belong to both PMD and ESLint and are tagged Security. No rule belongs to two engines, so the result is empty.
Exercise
A teammate runs a scan with --rule-selector Securty (a typo) and the scan reports no violations. What went wrong, and what step would have caught it?
Show answer
The misspelled term matches no tag, engine, or rule name, so the selected rule set is empty and nothing can be reported. Running sf code-analyzer rules with the same selector first would have shown an empty list, which signals that the selector is wrong.