StefanMaron / BusinessCentral.LinterCop

Community driven code linter for AL (MS Dynamics 365 Business Central)
https://stefanmaron.com
MIT License
72 stars 30 forks source link

Fix boolean conversion for Rule0032 #677

Closed dannoe closed 2 months ago

dannoe commented 2 months ago

This is a partial fix for issue #523. It is only partial, because it prevents the exception, but the rule (still) can not detect problems with codeunits, that are only available as symbol/app and not as source code.

The reason for the previous exception: singleInstanceProperty.Value returns "1" if the codeunit has SingleInstance = true set and is referenced via app/symbol.

The reason why it is not fully fixable: The codeunitTypeSymbol.GetMembers() call does not return global variables for codeunits that are only available as symbol/app and not in "real" source code. We are only getting procedures and triggers. Here's an example:

codeunit 54104 MyCodeunit
{
    var
        LogInManagement: Codeunit LogInManagement;

    procedure DoSomething()
    begin
        // This will not trigger the Rule0032 as GetMembers() does not work for
        // codeunits that are referenced by app/symbols and not by source code.
        // and singleInstanceProperty.Value will return "1" as a string instead of a boolean
        ClearAll(); 
    end;
}

I tried to mitigate this problem by accessing internal methods (like the GlobalVariables property of ReferenceCodeunitTypeSymbol) via reflection, but decided against it since I would have needed even more reflection to cast the return types correctly.

Fixes #523

Arthurvdv commented 2 months ago

This issue was driving me crazy and couldn't create a repo for this.

As this rule is fairly a specific use case which only occurs in an exceptional situation, preventing the exception seems enough and fair. Thank you for researching on this!