For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/eslint/no-case-declarations.md.
close

no-case-declarations

Added in v0.2.2

Configuration

PresetConfigured Value
✅ js.configs.recommended"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-case-declarations': 'error',
    },
  },
]);

Rule Details

Disallow lexical declarations in case clauses. Lexical declarations (let, const, function, class) in case clauses are visible in the entire switch block but only get initialized when assigned, which can lead to unexpected behavior.

Examples of incorrect code for this rule:

switch (foo) {
  case 1:
    let x = 1;
    break;
  case 2:
    const y = 2;
    break;
  case 3:
    function f() {}
    break;
  case 4:
    class C {}
    break;
}

Examples of correct code for this rule:

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

Original Documentation