The code analysis tool
This article shows you how to use static code analysis tool on an AL project from within Visual Studio Code.
Enable the code analysis
Follow these steps to create a basic project in AL.
- Select Alt+ A, Alt+L to create a new project.
- Open the Command Palette Ctrl+Shift+P and choose either User Settings or Workspace Settings.
- Under Extensions, go to Al Language extension configuration. When you scroll down, you'll find Code Analyzers section, choose Edit in settings.json.
- Set the
al.enableCodeAnalysisin the settings file to
- In the
al.codeanalyzerssetting, use Ctrl+Space to pick from the available code analyzers. Separate the list of code analyzers with commas. For more information about the available analyzers, see AppSourceCop, CodeCop, PerTenantExtensionCop, and UICop.
At this point, the selected analyzers run on your project. Next, add some code to the project that will, in the following example, be used to demonstrate a violation of the AA0001 "There must be exactly one space character on each side of a binary operator such as := + - AND OR =." code analysis rule.
By default, code analysis runs in the background.
Add your own code to the project
In the Visual Studio Code Explorer, open the
HelloWorld.al file and replace the existing code with the following code:
pageextension 50100 CustomerListExt extends "Customer List"
// The following line will trigger the warning
// AA0001 "There must be exactly one space character on each side
// of a binary operator such as := + - AND OR =."
result := 2+2;
Message('2 + 2 = ' + Format(result));
View the results of the code analysis
The code analysis tools run in the background. You'll see the faulty expression underlined and the warning "There must be exactly one space character on each side of '+'." display if you move cursor over the underlined code. You can also view the list of issues by selecting the View tab of Visual Studio Code and choosing the Problems option.
Using the Ctrl+Shift+B shortcut to build your project runs the code analysis tools on the entire project and the detected issues display in the Output window of Visual Studio Code. For more information about AL keyboard shortcuts, see Keyboard shortcuts.
A code analyzer is a library that builds on the compiler's functionality to offer enhanced analysis of the syntax and semantics of your code at build time. The AL Language extension for Microsoft Dynamics 365 Business Central for Visual Studio Code contains four analyzers:
- CodeCop is an analyzer that enforces the official AL Coding Guidelines. For more information about the CodeCop rules, see CodeCop Analyzer Rules.
- PerTenantExtensionCop is an analyzer that enforces rules that must be respected by extensions meant to be installed for individual tenants. For more information about the PerTenantExtensionCop rules, see PerTenantExtensionCop Analyzer Rules.
- AppSourceCop is an analyzer that enforces rules that must be respected by extensions meant to be published to Microsoft AppSource. For more information about the AppSourceCop rules, see AppSourceCop Analyzer Rules.
- UICop is an analyzer that enforces rules that must be respected by extensions that are meant to customize the web client. For more information about the UserInterfaceCop rules, see UICop Analyzer Rules.
Enable code analysis on large projects
To improve performance while running code analysis on large projects, you can follow the performance tips in Code Analysis Performance Configuration.