Upravit

Sdílet prostřednictvím


The code analysis tool

This article shows you how to use static code analysis tool on an AL project from within Visual Studio Code. The code analysis tool is a feature of the AL Language extension for Microsoft Dynamics 365 Business Central for Visual Studio Code that helps you to write better code by analyzing your code for potential issues. The tool runs in the background and provides warnings and errors in the code editor as you write code.

Enable the code analysis

Follow these steps to create a basic project in AL.

  1. Select Alt+ A, Alt+L to create a new project.
  2. Open the Command Palette Ctrl+Shift+P and choose either User Settings or Workspace Settings.
  3. Under Extensions, go to Al Language extension configuration. When you scroll down, you find Code Analyzers section, choose Edit in settings.json.
  4. Set the al.enableCodeAnalysis in the settings file to true.
    "al.enableCodeAnalysis": true
  5. In the al.codeanalyzers setting, 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.

Note

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"
{
    trigger OnOpenPage();
    var
        result: Integer;
    begin        
        // 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));
    end;
}

View the results of the code analysis

The code analysis tools run in the background. You 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. Learn more about AL keyboard shortcuts in Keyboard shortcuts.

Code analyzers

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.

Using the code analysis tools with the ruleset
Ruleset for the code analysis tool
Development in AL
Directives in AL
Debugging in AL
AL Language Extension configuration