Avoid lexical declarations in case clauses

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: javascript-best-practices/no-case-declarations

Language: JavaScript

Severity: Warning

Category: Best Practices

Description

Lexical declaration in switch cases are leaked throughout all other cases, which is undesired behavior. Scope your lexical declarations using {}.

Non-Compliant Code Examples

switch (a) { 
    case 1: 
        {}
        function f() {} 
        break;
}
switch (a) { 
    case 1: 
    case 2: 
        let x; 
}
switch (a) { case 1: let x = 1; break; }
switch (a) { default: let x = 2; break; }
switch (a) { case 1: const x = 1; break; }
switch (a) { default: const x = 2; break; }
switch (a) { case 1: function f() {} break; }
switch (a) { default: function f() {} break; }
switch (a) { case 1: class C {} break; }
switch (a) { default: class C {} break; }

Compliant Code Examples

switch (a) { case 1: { let x = 1; break; } default: { let x = 2; break; } }
switch (a) { case 1: { const x = 1; break; } default: { const x = 2; break; } }
switch (a) { case 1: { function f() {} break; } default: { function f() {} break; } }
switch (a) { case 1: { class C {} break; } default: { class C {} break; } }
switch (a) { 
    case 1: 
    case 2: {} 
}
switch (a) {
    case 1: var x; 
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: mervebolat/span-id-preprocessing