Skip to content

caseDeclarations

Reports lexical declarations in case clauses without wrapping them in blocks.

✅ This rule is included in the ts javascript presets.

Lexical declarations (let, const, function, class) in case and default clauses are scoped to the entire switch statement block, not just to the individual clause where they appear. This can lead to unexpected behavior when multiple clauses attempt to declare variables with the same name, resulting in syntax errors or unintended variable shadowing. Wrapping the contents of case clauses in curly braces creates a proper block scope, preventing these issues.

AEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0processVal1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0numb1<1)sp ansty="le--0:#7FDBCA;--1:#096E">7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0stri53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEswit53 (value)< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEca5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">5<3:d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEle53resuAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Do1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu53 resulnt < sp ansty="le--0:#C792EA;--1:#8844">AEca5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">5<3:d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEle53resuAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dt1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu53 resulnt < sp ansty="le--0:#C792EA;--1:#8844">AEdefau5<3:d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEle53resuAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Doth1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu53 resul53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">dAEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0handleTy1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53ty7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0stri1<1)sp ansty="le--0:#7FDBCA;--1:#096E">7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0vo53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEswit53 (type)< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEca5<3 sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dus1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3:d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEconsB0daAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0fetchUserDa53(<);d ivclassNa="meec-li">nt < sp ansty="le--0:#82AAFF;--1:#3B61">B0processDa53(data<);d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEbre5<3;d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEca5<3 sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dadm1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3:d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEconsB0daAE <= sp ansty="le--0:#82AAFF;--1:#3B61">B0fetchAdminDa53(<);d ivclassNa="meec-li">nt < sp ansty="le--0:#82AAFF;--1:#3B61">B0processDa53(data<);d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEbre5<3;d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEfunc361B0processV1<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v6361B0nu1<111) spans="tyle--0:#7FDBCA;--1:#09">6361B0st3F<53 {divclass="Nameec-">de44AEsw3F53 (valudent< spans="tyle--0:#C792EA;--1:#88">44AE30<9821 spans="tyle--0:#D6DEEB;--1:#40">3F5<3: {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AE3F53re44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E41<111" spans="tyle--0:#D6DEEB;--1:#40">3dent < spans="tyle--0:#C792EA;--1:#88">44AEre3F53 res3F53< spans="tyle--0:#D6DEEB;--1:#40">3dent< spans="tyle--0:#C792EA;--1:#88">44AE30<9822 spans="tyle--0:#D6DEEB;--1:#40">3F5<3: {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AE3F53re44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E41<111" spans="tyle--0:#D6DEEB;--1:#40">3dent < spans="tyle--0:#C792EA;--1:#88">44AEre3F53 res3F53< spans="tyle--0:#D6DEEB;--1:#40">3dent< spans="tyle--0:#C792EA;--1:#88">44AEdef3F5<3: {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AE3F53re44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Do1<111" spans="tyle--0:#D6DEEB;--1:#40">3dent < spans="tyle--0:#C792EA;--1:#88">44AEre3F53 res3F53< spans="tyle--0:#D6DEEB;--1:#40">33F<53 spans="tyle--0:#D6DEEB;--1:#40">33c44AEfunc361B0handle1<111( spans="tyle--0:#D7DBE0;--1:#40">3F536361B0st1<111) spans="tyle--0:#7FDBCA;--1:#09">6361B03F<53 {divclass="Nameec-">de44AEsw3F53 (typdent< spans="tyle--0:#C792EA;--1:#88">44AE31<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#D6DEEB;--1:#40">3F5<3: {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AEco61B044A61B0fetchUser3F5<3() ;divclass="Nameec-">dent < spans="tyle--0:#82AAFF;--1:#3B">61B0process3F53(dadent < spans="tyle--0:#C792EA;--1:#88">44AEb33F53< spans="tyle--0:#D6DEEB;--1:#40">3dent< spans="tyle--0:#C792EA;--1:#88">44AE31<111" spans="tyle--0:#ECC48D;--1:#98">4E4Da1<111" spans="tyle--0:#D6DEEB;--1:#40">3F5<3: {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AEco61B044A61B0fetchAdmin3F5<3() ;divclass="Nameec-">dent < spans="tyle--0:#82AAFF;--1:#3B">61B0process3F53(dadent < spans="tyle--0:#C792EA;--1:#88">44AEb33F53< spans="tyle--0:#D6DEEB;--1:#40">33F<53 spans="tyle--0:#D6DEEB;--1:#40">33< }>

This rule is not configurable.

If you are certain that you will never have variable name conflicts between case clauses and you understand the scoping implications, you might choose to disable this rule. However, using block scopes in case clauses is a best practice that prevents potential bugs and makes the code’s intent clearer.