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

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

There is a newer version: 5.0.0.6962
Show newest version

Why is this an issue?

Nested code blocks can be used to create a new scope: variables declared within that block cannot be accessed from the outside, and their lifetime end at the end of the block. However, this only happens when you use ES6 let or const keywords, a class declaration or a function declaration (in strict mode). Otherwise, the nested block is redundant and should be removed.

Exceptions

The rule does not apply to the following cases:

  • Block statements containing variable declarations using let or const keywords or class declarations are not redundant as they create a new scope.
{
    let x = 1;
}
  • The same applies to function declarations in strict mode
"use strict";
{
    function foo() {}
}
  • The rule also does not apply to the blocks that are part of the control flow.
if (condition) {
    doSomething();
}

How to fix it

The nested code blocks should be extracted into separate methods.

Code examples

Noncompliant code example

{   // Noncompliant: redundant code block
    var foo = bar();
}

if (condition) {
    doSomething();
    {   // Noncompliant: redundant code block
        doOtherStuff();
    }
}

Compliant solution

var foo = bar();

if (condition) {
    doSomething();
    doOtherStuff();
}

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy