Regex
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.
The regex engine comes with these bundled rules that can search your code for a variety of issues.
Rule Name | Default Severity | Tags | Description |
---|---|---|---|
NoTrailingWhitespace | 5 | Recommended, CodeStyle | Detects trailing whitespace (tabs or spaces) at the end of lines of code and lines that are only whitespace. |
AvoidOldSalesforceApiVersions | 2 | Recommended, Security | Detects usages of Salesforce API versions that are 3 or more years old. |
AvoidTermsWithImplicitBias | 5 | Recommended | Detects usage of terms that reinforce implicit bias. |
AvoidGetHeapSizeInLoop | 2 | Recommended, Performance | Detects usage of Limits.getHeapSize() in loops |
MinVersionForAbstractVirtualClassesWithPrivateMethod | 2 | Recommended | Detects private methods within abstract/virtual classes when the corresponding API version of the class is less than v61.0. |
Run this command to view detailed information about all the Regex rules:
For information on how to modify rule settings, such as their severity or tags, see Customize Your Configuration.
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:
Here are all the properties you can configure:
Property Name | Required? | Description | Default Value | Possible Values |
---|---|---|---|---|
regex | Yes | The regular expression that triggers a violation when matched against the contents of a file. | N/A | |
file_extensions | Yes | The extensions of the files in your workspace that you want to test the regular expression against. | N/A | |
description | Yes | A description of the rule's purpose and what it does. | N/A | |
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'] |
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
.
After you add this rule to the code-analyzer.yml
file, you refer to it when running the rules
and run
CLI commands like this:
Field | Type | Description |
---|---|---|
disable_engine | boolean | Whether to turn off the 'regex' engine so that it is not included when running Code Analyzer commands. Default value is false . |
custom_rules | object | Custom rules to be added to the 'regex' engine of the format custom_rules.{rule_name}.{rule_property_name} = {value} where:
{} .---- [Example usage]: --------------------- |