Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 743 Bytes

no-assign-in-case-without-braces.md

File metadata and controls

38 lines (29 loc) · 743 Bytes

Disallow assigning a variable in a case statement, unless the case body is wrapped in braces creating new scope

switch statements can be tricky, especially when their case clauses modify variables outside of the switch.

This rule discourages the use of switches to assign variables.

Rule Details

The following patterns are considered warnings:

/* eslint no-assign-in-case-without-braces */
var foo;
switch (qux) {
  case baz:
    foo = bar;
}

The following patterns are not considered warnings:

/* eslint no-assign-in-case-without-braces */
switch (qux) {
  case baz: {
    var foo = bar;
  }
}

function determine(q) {
  switch (q) {
    case baz:
      return bar;
  }
}
var foo = determine(qux)