Pragma and Rulesets let you make or break the rules.

The AL Language extension (the language for developing applications for Microsoft Dynamics 365 Business Central) contains fourcode analyzers for analysis of your code at build time. “Code violations” are displayed in the Problem window when the code is analyzed. The information is valuable for identifying and jumping to Code Analysis Tool rule violations.

Now that you potentially have a list of “violations,” what if you want to treat them differently or even ignore them? Within the AL language, a few ways to control code analysis exist.

The al.codeAnalyzers setting allows you to specify which code analyzers are active for a particular environment, workspace or project. Code Analysis enforcement is only for the analyzers listed, leaving the option to only include the analyzers you want to enforce.

The al.codeAnalyzers settings identifies the code analyzers used in code analysis

Rulesets are another option for specifying how code analysis reports the issues it encounters while analyzing AL code. The name and path for the ruleset file is set with the al.ruleSetPath setting. A Ruleset file is a JSON file that contains the Rule objects that define how to report a specific rule. In a rule, you specify the id of the diagnostic, the action (None, Hidden, Warning, Error) to take when the diagnostic is encountered, and a justification comment to note the rule. A Ruleset is applied globally within the scope of the setting.

A Ruleset file is a JSON file that contains the Rule objects that define how to report a specific rule.

Another option to control Code Analysis problems is the Pragma Warning Directive. The pragma warning instruction allows you to enable or disable specific warnings in a location of code. When disabling warnings with the pragma directive, the warnings are not reset back to the original state until you restore them.

An example of using the pragma directive to disable the rule id AA0001 for one section of code and restoring it.

There are several ways to enhance Code Analysis in AL, leaving you the flexibility to manage your code. Remember that if you submit an extension to AppSource, that Code Analysis is part of the technical validation, and your Rulesets may hide issues that cause the extension to fail validation.

Note: The code listed in this article is for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2022 Wave 1

Permanent link to this article: https://www.dvlprlife.com/2022/07/rulesets-pragma-i-do-not-want-to-follow-rules/

4 pings

  1. […] is possible to override the behavior of the code analyzer for specific Ids via the use of Pragma and Rulesets; that is for another […]

  2. […] is possible to override the behavior of the code analyzer for specific Ids via the use of Pragma and Rulesets; that is for another […]

  3. […] could suppress the warning by using the suppressWarnings key in the app.json file, the pragma compiler directive, or a Ruleset rule. Beginning with Business Central 2022 Wave 2, the implicit and explicit use of […]

  4. […] could suppress the warning by using the suppressWarnings property in the app.json file, the pragma compiler directive, or a Ruleset rule. The implicit and explicit use of ‘with’ can be switched on or off with the […]

Leave a Reply

Your email address will not be published.