All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S1439.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

Labels allow specifying a target statement to jump to using the break or continue statements. It’s possible to assign a label to any statement or block of statements. However, using it with any statement can create a complex control flow path, making the code harder to understand and maintain.

myLabel: if (i % 2 == 0) { // Noncompliant: Labeling an if statement
  if (i == 12) {
    console.log("12");
    break myLabel;
  }
  console.log("Even number, but not 12");
}

Instead of using a label with these nested if statements, this code block should be refactored.

if (i % 2 == 0) { // Compliant
  if (i == 12) {
    console.log("12");
  } else {
    console.log("Even number, but not 12");
  }
}

The rule considers that while, do-while, for, and switch statements don’t create complex control flow paths, thus these statements are not reported.

outerLoop: for (let i = 0; i < 10; i++) { // Compliant
  for (let j = 0; j < 10; j++) {
    if (condition(i, j)) {
      break outerLoop;
    }
  }
}

Resources

Documentation

Related rules

  • {rule:javascript:S1119} - Labels should not be used




© 2015 - 2024 Weber Informatics LLC | Privacy Policy