Sub topic 2
The engines behind the rules
This chapter assumes you have used the Salesforce CLI and know basic Apex and JavaScript. You don't need to install anything yet. The next chapter covers installation.
What an engine is
Code Analyzer doesn't contain one big checker. It is a coordinator that runs several existing analysis tools and merges their findings into one report. Each tool is called an engine. Every rule belongs to exactly one engine, and each engine looks at certain kinds of files.
The engines
| Engine | What it checks | Typical findings |
|---|---|---|
| `pmd` | Apex, Visualforce, and other source files | DML in loops, hard-coded IDs, security and performance rules |
| `eslint` | JavaScript and TypeScript, including Lightning Web Components and Aura | Unused variables, unsafe patterns, LWC best-practice rules |
| `retire-js` | JavaScript libraries, such as those in static resources | Libraries with known vulnerabilities |
| `regex` | Plain text patterns | Simple checks such as trailing whitespace |
| `flow` | Salesforce Flows | Flow best-practice and risk checks |
| `cpd` | Duplicated code across files | Copy-pasted blocks |
Two points help when you read results later:
- Most engines are rule-based. PMD and ESLint parse code into a syntax tree and match rules against it. They look at one file at a time.
- RetireJS and CPD work differently. RetireJS compares library versions against a vulnerability database. CPD compares code against other code and doesn't apply a rule to the logic itself.
A separate graph engine (SFGE) follows data flow across methods and files. Its rules need more setup and run more slowly, so a later chapter covers it on its own.
Seeing which engine owns a rule
The --rule-selector flag accepts an engine name. You can use it to list only that engine's rules:
sf code-analyzer rules --rule-selector pmd
sf code-analyzer rules --rule-selector eslint
sf code-analyzer rules --rule-selector retire-jsThe output includes an engine column. When a scan reports a violation, the same column tells you which tool raised it. That tells you where to look for documentation, and where to look for the cause of a false positive.
You can combine an engine with a tag using a colon. For example, pmd:Security selects only the PMD rules tagged Security. The next sections cover tags and selectors in detail.
Common mistakes
- Expecting one engine to cover everything. A scan of only Apex files won't produce ESLint findings, because ESLint has nothing to analyze. If a rule seems to be missing, check that the workspace contains files that engine understands.
- Searching for a rule by name only. Two engines can have similar rules. Use the engine name to tell them apart.
- Treating CPD output like rule output. Duplication is reported against the duplicated code, not against a specific mistake.
Exercise
A scan of a project that contains only Apex classes reports no ESLint violations. Is that a sign that ESLint failed? Which command would you run to list only the ESLint rules?
Show answer
No. ESLint analyzes JavaScript and TypeScript, so a project with only Apex classes gives it nothing to check. To list its rules, run sf code-analyzer rules --rule-selector eslint.