Avoid lexical declarations in case clauses
This product is not supported for your selected
Datadog site. (
).
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우
언제든지 연락주시기 바랍니다.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;
}
원활한 통합. Datadog Code Security를 경험해 보세요