1. What is it?
  2. Installation
  3. Usage
  4. Results
  5. Links

What is it?

Introduction: In the dynamic world of Salesforce development, maintaining the highest quality of code is crucial for building scalable, efficient, and error-free applications.

You might read some of my previous articles on PMD and SFDX CLI Scanner in this blog.

This is where the Salesforce Code Analyzer comes to the rescue, with its new capabilities announced.

The Salesforce Code Analyzer is a powerful tool that analyzes your Apex code and provides valuable insights, suggestions, and optimizations to enhance the overall quality and performance of your Salesforce applications.

development efforts with the Salesforce Code Analyzer. Let’s embark on this exciting journey together!

Code Analyzer currently supports the:

Rule EngineDescription
Salesforce Graph EngineDetects security and quality issues in code, as an open-source
PMDAllows for static analysis of code including Apex, and Visualforce
CPDIdentifies blocks of duplication across files
ESLint for LWCEvaluates Salesforce Lightning Web Components
ESLint for Typescript Evaluates any targeted TypeScript (.ts) files
RetireJSAnalyzes a project’s third-party JavaScript dependencies and identifies security vulnerabilities

Installation

As you probably have the Salesforce CLI, then this is candy for you:

sfdx plugins:install @salesforce/sfdx-scanner

Usage

Even Code Analyzer is a SFDX CLI plugin, unfortunately is not integrated in VsCode, but in this video Alba and Ananya made by day, announcing that later this year, we’ll be able to have in our Vscode (6′:47») – btw very recommended video.

Also, in my opinion, the main command sfdx:scanner:run is overwhelming with the huge number of options available:

sfdx scanner:run -t <array> [-c <array>] [-r <array>] [-e <array>] [-f 
 csv|html|json|junit|sarif|table|xml] [-o <string>] [--tsconfig <string>] [--eslintconfig <string>] [--pmdconfig <string>] [--env <string>] [-s <integer> | undefined | [-v | --json]] [--normalize-severity] [--verbose] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

If you want to try it in your code without much of a hustle:

  1. Go to your project and place yourself in the root of your project
  2. Execute: sfdx scanner:run -t .
  3. Don’t miss the point indicating all files

Results

The following is an abstract of the results when I run this command on the magnificient LWC recipes project:

LOCATION                                                                                                      DESCRIPTION                                          CATEGORY       U R L                                                                                                 
 force-app\main\default\classes\AccountController.cls:1                                                          Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\AccountController.cls:3                                                          Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\AccountController.cls:8                                                          Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ApexTypesController.cls:1                                                        Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ApexTypesController.cls:3                                                        Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ApexTypesController.cls:6                                                        Avoid debug statements since they impact on        Performance    https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_performance.html#avoiddebugstatements                 
                                                                                                                 performance                                                                                                                                                             
 force-app\main\default\classes\ApexTypesController.cls:6                                                        Calls to System.debug should specify a logging     Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_bestpractices.html#debugsshoulduselogginglevel        
                                                                                                                 level.                                                                                                                                                                  
 force-app\main\default\classes\ContactController.cls:1                                                          Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ContactController.cls:3                                                          Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ContactController.cls:22                                                         Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ContactController.cls:34                                                         Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ContactController.cls:44                                                         Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\ContactController.cls:55                                                         Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\CustomWrapper.cls:5                                                              Missing ApexDoc @description                       Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\CustomWrapper.cls:7                                                              Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\CustomWrapper.cls:9                                                              Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\CustomWrapper.cls:11                                                             Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\LMSVisualforceController.cls:1                                                   Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\LMSVisualforceController.cls:3                                                   Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\LMSVisualforceController.cls:5                                                   Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\LMSVisualforceController.cls:9                                                   Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\TestAccountController.cls:3                                                      Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\TestAccountController.cls:21                                                     'Assert.areEqual' should have 3 parameters.        Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_bestpractices.html#apexassertionsshouldincludemessage 
 force-app\main\default\classes\TestAccountController.cls:32                                                     'Assert.areEqual' should have 3 parameters.        Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_bestpractices.html#apexassertionsshouldincludemessage 
 force-app\main\default\classes\TestContactController.cls:3                                                      Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\TestContactController.cls:19                                                     Missing ApexDoc comment                            Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\main\default\classes\TestLMSVisualforceController.cls:23                                              Missing ApexDoc @description                       Documentation  https://pmd.github.io/pmd-6.55.0/pmd_rules_apex_documentation.html#apexdoc                            
 force-app\test\jest-mocks\lightning\modal.js:60                                                                 'apis' is defined but never used.                  problem        https://eslint.org/docs/latest/rules/no-unused-vars                                                   
 force-app\main\default\aura\lmsPublisherAuraComponent\lmsPublisherAuraComponentController.js:10                 '$A' is not defined.                               problem        https://eslint.org/docs/latest/rules/no-undef                                                         
 force-app\main\default\lwc\apexWireMethodWithParams\apexWireMethodWithParams.js:18                              Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          
 force-app\main\default\lwc\compositionContactSearch\compositionContactSearch.js:16                              Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          
 force-app\main\default\lwc\dispatchEventHeadlessAction\dispatchEventHeadlessAction.js:24                        Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Empty block statement.                             suggestion     https://eslint.org/docs/latest/rules/no-empty                                                         
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
 force-app\main\default\staticresources\fullCalendar\main.min.js:8                                               Expected a conditional expression and instead saw  problem        https://eslint.org/docs/latest/rules/no-cond-assign                                                   
                                                                                                                 an assignment.                                                                                                                                                          
                                                                                                                 used.                                                                                                                                                                   
 force-app\main\default\lwc\compositionContactSearch\__tests__\compositionContactSearch.test.js:63               Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          
 force-app\main\default\lwc\datatableInlineEditWithUiApi\__tests__\datatableInlineEditWithUiApi.test.js:67       Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          
 force-app\main\default\lwc\ldsNotifyRecordUpdateAvailable\__tests__\ldsNotifyRecordUpdateAvailable.test.js:40   Definition for rule '@lwc/lwc/no-async-operation'  problem                                                                                                              
                                                                                                                 was not found.                                                                                                                                                          

Executed pmd, found 27 violation(s) across 8 file(s).
Executed eslint, found 202 violation(s) across 16 file(s).
Executed retire-js, found 0 violation(s) across 0 file(s).
Rule violations were logged to the console.

More important than the results is what is not visible:

  • Multiples rule engines are executed: PMD, ESlint, etc.
  • You can customize the analysis
  • Can be attached to an CI/CD pipeline even for the pull request common workflow
  • The Salesforce Graph engine detects security issues

It is important to know that Code Analyzer is not unconnected execution of the different rule engines, and for example the Severity is normalized so you can request an analysis based on the same severity across all the rulesets:

Normalized SeverityPMDESLintESLint-LWCESLint-TypeScriptRetire-JSSalesforce Graph Engine
1 (High)122211
2 (Moderate)211122
3 (Low)3, 4, 5   33

As a Salesforce developer or architect, stay tuned and get ready to supercharge your development experience with SFCA. The future of static code analysis will be through this tool, and I think it’s time to embrace its transformative power.

Anuncio publicitario

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

A %d blogueros les gusta esto: