Sub topic 2
The engines behind the rules
This chapter assumes you have used Apex and either Lightning Web Components or JavaScript, and that you can run the sf command from a terminal. You don't need the plugin installed yet. The next chapter covers that.
Rules come from engines
Code Analyzer doesn't contain one big list of checks. It is a wrapper that runs several existing analysis tools and combines their findings into one report. Each tool is called an engine. An engine is a separate analyzer that knows how to read certain file types and applies its own rules to them.
Every rule belongs to exactly one engine. Each violation in a report carries the engine name, the rule name, a severity, and a file location. When you use rule selectors, override severities, or ignore a finding, you will refer to rules by engine and name. Knowing which engine owns a rule tells you what the rule can see, how it was written, and what it needs to run.
The engines
| Engine name | Analyzes | Typical findings |
|---|---|---|
| `pmd` | Apex, Visualforce, and XML metadata | Governor-limit risks, security issues, bad practices, code style |
| `eslint` | JavaScript, TypeScript, and LWC and Aura code | Language errors, LWC-specific mistakes, unsafe patterns |
| `retire-js` | Bundled third-party JavaScript libraries | Libraries with known vulnerabilities |
| `regex` | Any text file | Simple text patterns such as trailing whitespace |
| `flow` | Flow metadata files | Flow design and performance problems |
| `cpd` | Many languages | Duplicated blocks of code |
| `sfge` | Apex | Data-flow problems such as unchecked paths to database operations |
The exact set of engines, and which are on by default, changes between plugin releases. Treat this table as the shape of the landscape. To see what your installed version provides, list its rules, as shown later in this chapter.
PMD
PMD is a static analyzer that parses source code into a syntax tree and matches rules against that tree. In a Salesforce project it is the engine you will see most often, because it covers Apex. The AccountUpdater class from this tutorial's running example has an update statement inside a loop. A PMD rule finds that pattern because it can see that a DML statement is a child of a loop node.
PMD rules carry tags such as Security, Performance, BestPractices, ErrorProne, and CodeStyle. You can select rules by tag, for example pmd:Security.
ESLint
ESLint is the standard linter for JavaScript. Code Analyzer runs it with Salesforce-oriented rule sets, so it covers Lightning Web Components, Aura components, and plain JavaScript or TypeScript in your project. If your repository has its own ESLint configuration, check how your plugin version treats it, because the engine's behavior around existing configuration files has changed across releases.
RetireJS
RetireJS doesn't inspect your logic. It compares JavaScript libraries it finds, such as a minified jQuery file in a static resource, against a database of known vulnerable versions. A finding means "this library version has a published vulnerability". The fix is usually to upgrade the library, not to edit your own code.
This is why ignore patterns matter. Third-party code under staticresources is exactly what RetireJS inspects, while other engines would only produce noise there.
Regex
The regex engine applies text patterns to files without parsing them. It is fast and works on any file type, but it has no understanding of structure. Its rules are narrow, such as trailing whitespace. Regex rules also have a place when you need an organization-specific check, for example forbidding a particular string.
Flow
The Flow engine analyzes Flow metadata files (.flow-meta.xml). Flows are declarative, so PMD's Apex rules never see them. This engine fills that gap with checks on how a flow is built.
CPD
CPD stands for Copy/Paste Detector. It looks for repeated blocks of code, so its findings are about duplication, not about a specific bad construct. Because duplication is a property of several places in your code at once, a CPD violation usually points at one block and mentions where its duplicates live.
Graph engine (SFGE)
SFGE is the Salesforce Graph Engine. Unlike the others, it follows data and control flow across methods and classes. It is a heavier tool, and a later chapter covers when to use it. For now, remember that it is slower than file-by-file engines and is not part of the quick default scan.
How a scan uses engines
A scan chooses rules first, and the rules decide which engines run. If your selection matches no ESLint rules, ESLint never starts.
Two practical results follow from this model:
- A narrow selector is also a faster scan, because unused engines are skipped.
- Engines fail independently. If one engine can't start, for example because a required runtime is missing, you get an error about that engine. The other engines are not at fault.
Seeing what each engine offers
The rules command lists rules, and the --rule-selector flag filters them. A bare engine name selects all of that engine's rules.
sf code-analyzer rules --rule-selector pmd
sf code-analyzer rules --rule-selector eslintYou can combine an engine with a tag, which narrows the list to rules that satisfy both:
sf code-analyzer rules --rule-selector pmd:SecurityTo combine selectors, repeat the flag. The result is every rule matched by either selector:
sf code-analyzer rules --rule-selector pmd:Security --rule-selector eslint:RecommendedThe listing shows the engine, rule name, severity, and tags for each rule. Read it before you write selectors. It tells you which tags exist for your plugin version, which saves guessing.
Runtime requirements
Engines are implemented in different languages, so they need different things on your machine:
- PMD, CPD, and SFGE run on Java, so a supported Java version must be installed.
- ESLint and RetireJS run on Node.js, which you already have if the Salesforce CLI was installed through npm.
- The Flow engine may have its own prerequisites, depending on the plugin version.
Check the plugin's documentation for the exact versions your release needs. A missing runtime usually produces an error that names the engine, which is your cue to fix the environment and not the code.
Turning engines off
If an engine is not useful for your project, you can disable it in code-analyzer.yml. A project with no Visualforce or LWC might not need ESLint, for example.
# code-analyzer.yml
engines:
eslint:
disable_engine: trueDisabling an engine removes all its rules from every scan that uses this file. Prefer a rule selector when you only want to skip it for one run. Prefer the configuration file when the decision is permanent for the project. A later chapter covers the file in full.
Common mistakes
- Expecting one engine to cover everything. PMD won't flag a vulnerable library, and RetireJS won't flag a loop with DML. If a problem you expected is missing, check that the engine that owns such rules actually ran.
- Reading a CPD finding like a PMD finding. A duplication report means "this block appears more than once", not "this line is wrong".
- Treating the engine list as fixed. Engine names, default sets, and tags differ between releases. Run
sf code-analyzer rulesagainst your installed plugin before relying on a name from documentation. - Scanning third-party code. Vendored libraries produce a large number of findings that you can't reasonably fix. Exclude them instead of lowering severities.
Exercise
Your team's scan reports no findings for a minified jQuery file in staticresources, yet you know that file is an old, vulnerable version. You ran sf code-analyzer run --workspace force-app --rule-selector pmd. What is the likely cause, and what change would fix it?
Show answer
The pmd selector only runs PMD rules, and PMD does not check JavaScript libraries for known vulnerabilities. That job belongs to the retire-js engine, which never started. Add a selector that includes it, for example --rule-selector pmd --rule-selector retire-js, or use Recommended if it covers that engine in your plugin version. You can confirm with sf code-analyzer rules --rule-selector retire-js.
Exercise
A scan fails with an error about one engine while the other engines are listed as working. Which is more likely: a bug in your Apex, or a missing runtime for that engine? What command-line step would you take first?
Show answer
A missing or unsupported runtime for that engine is more likely, because engines run and fail independently and the error names the engine. Check that the runtime the engine needs, such as Java for PMD, is installed and on your path. As a workaround, you can run with a selector that excludes that engine while you fix the environment.