SonarLint gives warning for contradicting rules

  • versions used (SonarQube, Scanner, Plugin, and any relevant extension)

    SonarLint 5.3.0.36775 plugin for IntelliJ

  • error observed (wrap logs/code around triple quote ``` for proper formatting)

SonarLint complains about trailing comma saying Trailing commas should not be used
at line #184 and #185 citing rule javascript:S1537.

But when I remove the trailing commas, it complains again but this time says Trailing commas should be used citing rule javascript:S3723

Here the linting rules that SonarLint displays seem to contradict themselves.

      const axiosConfig = {
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          "Access-Control-Allow-Origin": "*",
        },
      };

Below is the screenshot that I took after removing the trailing comma from line #185 :

image

  • steps to reproduce
    Not quite sure how to reproduce it in any other scenario, but perhaps having a similar code structure like above might reproduce this issue.

  • potential workaround
    Since here the two rules seem to contradict each other, I don’t think this has any workaround other than disabling those rules from within the settings.

Hello, thank you for your feedback!

These 2 rules are not enabled by default, so you should probably only enable the one that makes sense according to your project’s or team’s conventions.

Got it. Thank you so much. That makes sense.

I was just thinking we can have a single rule for it instead of having two separate rules. We can have a single rule enabled by default(preferably the convention that is widely accepted/recommended) and still keep that customizable(like having a single checkbox that says trailing comma ).

Having a single rule will simplify the user experience and not cause any confusion. This will abstract away similar rules.

We can get some inspiration from already existing rule javascript:S1441 : Quotes for string literals should be used consistently.

The rule description box for S1441 provides a neat and clear instruction for overriding the preference:

Clicking on the Rule Settings link takes us to the setting window where can change the setting

1 Like

Thanks for these ideas, we will consider it.

Thank you so much for considering this. Just curious : why has this already been marked Solved ?

Another common rules that pop up from time to time are the ones regarding the code formatting conventions like java:S1106 An open curly brace should be located at the beginning of a line. This too can be looked into.

Thanks again.

‘Solved’ means that these is nothing more to discuss :slight_smile:

I don’t understand your another point about java rule, is it also contradicting something?

1 Like

Ticket Merge 2 rules about trailing comma into a single one · Issue #2823 · SonarSource/SonarJS · GitHub

1 Like

Ticket Merge 2 rules about trailing comma into a single one · Issue #2823 · SonarSource/SonarJS · GitHub

Thank you so much for opening the GitHub issue.

I don’t understand your another point about java rule, is it also contradicting something?

I was referring to the brace positioning convention. Some people like to do this :

methodName(){ //<---brace just starts from where method declaration ends
//implementation goes here
}

while others prefer this :

methodName()
{ //<---brace starts from newline
//implementation goes here
}

Just like the trailing comma issue, this also can be merged into one with either of those two conventions being made default and providing with option to configure.

I feel there might be other similar rules too which can be merged. I’ll do bring them to your notice if I encounter any.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.