Inappropriate code example for S2301

  • What language is this for?
    • TypeScript
  • Which rule?
    • S2301
  • Why do you believe it’s a false-positive/false-negative?
    • No, bad code example
  • I am using
    • SonarQube Cloud
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
    • Validate some code that violates S2301 and look at the code example

The code example given for S2301 is a bit inappropriate in my opinion. See code here: SonarJS/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S2301.html at master · SonarSource/SonarJS · GitHub

Hi,

Welcome to the community and thanks for this report!

Could you provide a code sample that you feel would be appropriate?

 
Thx,
Ann

I suggest something that doesn’t doesn’t use a combination of the words “tempt” “child” and “offerCandy”.

An alternative could be:

function feedHuman(name: string) {
  offerSushi(name);
}

function feedAnimal(name: string) {
  offerCarrot(name);
}

// ...
function feed() {
  isHuman ? feedHuman("Joe") : feedAnimal("Joe");
}
2 Likes

Hi,

Thanks for that. I’ve queued this for the language folks.

 
Ann

1 Like

Hello Edwin,

Thank you for your feedback. I have opened a ticket to track this: JS-516