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 NameDefault SeverityTagsDescription
NoTrailingWhitespace5Recommended, CodeStyleDetects trailing whitespace (tabs or spaces) at the end of lines of code and lines that are only whitespace.
AvoidOldSalesforceApiVersions2Recommended, SecurityDetects usages of Salesforce API versions that are 3 or more years old.
AvoidTermsWithImplicitBias5RecommendedDetects usage of terms that reinforce implicit bias.
AvoidGetHeapSizeInLoop2Recommended, PerformanceDetects usage of Limits.getHeapSize() in loops
MinVersionForAbstractVirtualClassesWithPrivateMethod2RecommendedDetects 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 NameRequired?DescriptionDefault ValuePossible Values
regexYesThe regular expression that triggers a violation when matched against the contents of a file.N/A
file_extensionsYesThe extensions of the files in your workspace that you want to test the regular expression against.N/A
descriptionYesA description of the rule's purpose and what it does.N/A
violation_messageNoThe 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}
severityNoThe 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
tagsNoThe 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:

FieldTypeDescription
disable_enginebooleanWhether to turn off the 'regex' engine so that it is not included when running Code Analyzer commands. Default value is false.
custom_rulesobjectCustom rules to be added to the 'regex' engine of the format custom_rules.{rule_name}.{rule_property_name} = {value} where:
  • {rule_name} is the name you would like to give to your custom rule
  • {rule_property_name} is the name of one of the rule properties. You may specify the following rule properties:
    • 'regex' - The regular expression that triggers a violation when matched against the contents of a file.
    • 'file_extensions' - The extensions of the files that you would like to test the regular expression against.
    • 'description' - A description of the rule's purpose
    • 'violation_message' - [Optional] The message emitted when a rule violation occurs.
      • This message is intended to help the user understand the violation.
      • Default: 'A match of the regular expression {regex} was found for rule {rule_name}: {description}'
    • 'severity' - [Optional] The severity level to apply to this rule by default.
      • Possible values: 1 or 'Critical', 2 or 'High', 3 or 'Moderate', 4 or 'Low', 5 or 'Info'
      • Default: 3
    • 'tags' - [Optional] The string array of tag values to apply to this rule by default.
      • Default: ['Recommended']
Default value is {}.
---- [Example usage]: ---------------------
engines:
regex:
custom_rules:
"NoTodoComments":
regex: /\/\/[ \t]*TODO/gi
file_extensions: [".apex", ".cls", ".trigger"]
description: "Prevents TODO comments from being in apex code."
violation_message: "A comment with a TODO statement was found. Please remove TODO statements from your apex code."
severity: "Info"
tags: ["TechDebt"]
-------------------------------------------