Lone Block Statement

ID

javascript.no_lone_blocks

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

JavaScript

Tags

code-style, dead-code

Description

Reports { …​ } block statements that are not the body of a function, control-flow construct, or labelled statement, and that do not declare any block-scoped binding (let, const, function, class). Such a block adds an extra level of nesting without changing scope or behaviour.

Rationale

  • Most lone blocks are leftover scaffolding — the surrounding if / for was deleted but the body stayed.

  • A block that scopes let / const is meaningful (the bindings disappear after the block); the rule explicitly preserves that case.

  • Labelled blocks (outer: { …​ break outer; …​ }) are recognised by the parser as labelled statements rather than lone blocks and are not flagged.

// Bad
function f() {
  doFirst();
  {
    doSecond();
  }
}

Remediation

Either remove the braces, or add the surrounding control-flow construct that the block was supposed to belong to.

// Good - braces dropped
function f() {
  doFirst();
  doSecond();
}

// Good - block scopes a binding intentionally
function g() {
  {
    const cached = compute();
    use(cached);
  }
}