Skip to content

unnecessaryBlocks

Reports standalone block statements that don't create a meaningful scope.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Standalone block statements that aren’t part of control flow structures (if/else, loops, switch, etc.) or don’t create a meaningful lexical scope can make code harder to understand. In modern JavaScript and TypeScript, blocks primarily serve to create lexical scope for let and const variables, but using standalone blocks for this purpose is often confusing compared to other patterns like functions or modules.

5<3{d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEconsB0temporaryValAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0computeVal53(<);d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0processVal53(temporaryValue<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0processDa11<()sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3{d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEconsB0resuAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0calcula53(<);d ivclassNa="meec-li">53 < sp ansty="le--0:#D6DEEB;--1:#403F">53consoAB0l53(result<);d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEco61B0temporaryV44A61B0computeV3F5<3() ;divclass="Nameec-">61B0processV3F53(temporaryVal<;di vdivclass="Nameexpressive-">44AEfunc361B0process11<11() spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">de44AEco61B0re44A61B0calcu3F5<3() ;divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53con4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B<0log spans="tyle--0:#D6DEEB;--1:#40">3F53(resu3<);><}di vdivclass="Nameexpressive-">6376// Valid: block is part of control 443F53 (conditiode44AEco61B0v44A61B0getV3F5<3() ;divclass="Nameec-">de61B0processV3F53(val3<);><}di vdivclass="Nameexpressive-">6376// Valid: block creates scope in switch 44AEsw3F53 (typde44AE31<111" spans="tyle--0:#ECC48D;--1:#98">41<111" spans="tyle--0:#D6DEEB;--1:#40">3F5<3: {divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">44AEco61B0v44A61B03F5<3() ;divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">44AEb33F<53 spans="tyle--0:#D6DEEB;--1:#40">33< }>

This rule is not configurable.

If you frequently use standalone blocks for visual organization or temporary scoping in ways that your team finds helpful, you might choose to disable this rule. However, consider whether extracting to functions or using other scoping mechanisms would be clearer.