Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if eventsubscribers don't use var for parameters defined as var. #676

Merged

Conversation

dannoe
Copy link
Contributor

@dannoe dannoe commented Jul 6, 2024

This implements the idea from #654 and adds tests for the rule.

I also did some additional things:

  • Fixed a NullReferenceException from Rule0054 that I was getting even with the fix from Resolve NullReferenceException on LC0054 #673.
  • I tried a new way to add the DiagnosticDescriptors. For this rule the Descriptors is now in a nested class inside the rule itself.
    The reason for this is, that I find the LinterCopAnalyzers.Generated.cs file very unclear and unreadable with these extreme long lines.
  • Fixed some Nullable warnings

I am open to suggestions, vetoes, or requests to split these changes into multiple PRs.

@dannoe dannoe force-pushed the add-eventsubscriber-var-check branch from 46eb77a to 7c296e9 Compare July 7, 2024 08:15
Copy link
Collaborator

@Arthurvdv Arthurvdv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a new way to add the DiagnosticDescriptors. For this rule the Descriptors is now in a nested class inside the rule itself.
The reason for this is, that I find the LinterCopAnalyzers.Generated.cs file very unclear and unreadable with these extreme long lines.

I see the difference now and absolutely agree that the new way is more clear and readable. I do have a question about the nested class inside the rule itself. There are a few exceptions where the rule and the descriptor aren't coupled, like the descriptors Rule0005VariableCasingShouldNotDifferFromDeclaration is used in the Rule0003 and Rule0005 class. Can this then still work?

@dannoe
Copy link
Contributor Author

dannoe commented Jul 7, 2024

I tried a new way to add the DiagnosticDescriptors. For this rule the Descriptors is now in a nested class inside the rule itself.
The reason for this is, that I find the LinterCopAnalyzers.Generated.cs file very unclear and unreadable with these extreme long lines.

I do have a question about the nested class inside the rule itself. There are a few exceptions where the rule and the descriptor aren't coupled, like the descriptors Rule0005VariableCasingShouldNotDifferFromDeclaration is used in the Rule0003 and Rule0005 class. Can this then still work?

As long as the nested class is accessible from other places in the code (like public or internal for the same assembly) this should be no problem.

@Arthurvdv
Copy link
Collaborator

Arthurvdv commented Jul 7, 2024

First I wanted to thank you contributing, really appreciatie you're taking the time to help further improve the LinterCop :-)

When I was creating a example for the wiki, I've run into ArgumentNullException (due tough a typo I've created)

image

codeunit 50100 MyBusinessLogic
{
    [IntegrationEvent(false, false)]
    local procedure OnBeforeMyBusinessLogic(var Customer: Record Customer; var IsHandled: Boolean)
    begin
    end;
}

codeunit 50501 MyEventSubscriber
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::MyBusinessLogic, NonExistingEventName, '', false, false)]
    local procedure OnBeforeMyBusinessLogicOnMyBusinessLogic(Customer: Record Customer)
    begin
    end;
}

While the AL code is wrong due to the NonExistingEventName, it would be great if the LinterCop doesn't throw an exception during development. Do you want me to have a look into this of is this something you can easily resolve?

@dannoe
Copy link
Contributor Author

dannoe commented Jul 7, 2024

While the AL code is wrong due to the NonExistingEventName, it would be great if the LinterCop doesn't throw an exception during development. Do you want me to have a look into this of is this something you can easily resolve?

That's interesting. The exceptions occurred because there was a null given as a parameter to the GetFirstMethod method. But according to nullable annotations of microsoft code, the ValueText of the IAttributeArgumentSymbol is never null (it is string not string?). But somehow it still gives null in case of non-compilable code.

At first I couldn't reproduce this on my site because I was testing it with a large project (700+ files). But I was able to reproduce it with a smaller test project and added an additional null check.

Edit: I removed the NRE fix from this PR and opened a seperate PR (#678)

@dannoe dannoe force-pushed the add-eventsubscriber-var-check branch from 63ce7bf to d28eb0f Compare July 7, 2024 21:55
@Arthurvdv
Copy link
Collaborator

Again, thanks for the contribution, much appreciated :-)

@Arthurvdv Arthurvdv merged commit c49a8cd into StefanMaron:prerelease Jul 8, 2024
41 checks passed
@dannoe dannoe deleted the add-eventsubscriber-var-check branch July 8, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants