The Regex engine is new in Code Analyzer v5. The engine uses regular expressions (regex) to search your code base for patterns. This engine is perfect for a quick initial check of your code base to search for specific text or strings. It’s also useful to search for patterns in your code comments; some engines, such as PMD, ignore comments. But it’s a blunt tool: if you need more nuance, such as finding actual coding violations, use a different engine.
The true power of this engine comes with the ease of adding new rules of your own. If you have regular expression patterns that you want to test against your code, you can easily provide these patterns to your Code Analyzer configuration file to be automatically included as rules. You can then run these rules alongside all of the other rules that Code Analyzer offers.
Available Regex Rules
Run this command to view the list of available Regex rules:
For information on how to modify rule settings, such as their severity or tags, see Customize Your Configuration.
Add Custom Regex Rules
Adding custom Regex rules to Code Analyzer is easy: simply add a custom_rules option to the regex engine section of the code-analyzer.yml and specify the required and optional Regex rule properties. The basic pattern looks like this:
The regular expression that triggers a violation when matched against the contents of a file.
Not supported
file_extensions
Yes
The extensions of the files in your workspace that you want to test the regular expression against.
Not supported
description
Yes
A description of the rule’s purpose and what it does.
Not supported
violation_message
No
The message that’s emitted when a rule violation occurs. Write the message to help the user understand the violation, and if possible, how to fix it.
A match of the regular expression {regex} was found for rule {rule_name}: {description}
severity
No
The severity level to apply to this rule by default.
3 (Moderate)
1 or Critical, 2 or High, 3 or Moderate, 4 or Low, 5 or Info
tags
No
The string array of tag values to apply to this rule by default.
['Recommended']
ignore-regex
No
Exclude safe patterns from violations.
Not supported
In this example, the name of the new rule is NoTodoComments. The Regex expression searches for all case-insensitive occurrences of the string TODO in Apex files in your workspace; specifically, the files must have the extension .apex, .cls, or .trigger. The severity of this violation is Info and its tag is TechDebt.
1engines:2 regex:3 custom_rules:4 "NoTodoComments":5 regex: /\/\/[ \t]*TODO/gi6 file_extensions: [".apex", ".cls", ".trigger"]7 description: "Prevents TODO comments from being in apex code."8 violation_message: "A comment with a TODO statement was found. Please remove TODO statements from your apex code."9 severity: "Info"10 tags: ["TechDebt"]
This custom regex rule detects the hardcoded APIs while ignoring the false positives.
Also, as a convenience, Code Analyzer automatically adds the Custom tag to custom rules. Use this command to see all the custom Regex rules; your new rule should be in the list: