Skip to content

@studnicky/v8/switch-statements

Disallows switch cases with a BlockStatement body (case X: { ... }). V8 switch dispatch expects simple calls or returns; block statement bodies inhibit the fast dispatch table and prevent V8 from generating optimal jump-table code.

Fixable: No · Options: No · Suggested severity: error

✗ Incorrect

ts
switch (action) {
  case 'start': {
    const result = init();
    return result;
  }
  case 'stop': {
    cleanup();
    break;
  }
}

✓ Correct

ts
// Simple calls or returns without block wrappers
switch (action) {
  case 'start': return init();
  case 'stop': return cleanup();
  default: return noop();
}
ts
// Or use a dispatch map instead of switch
const handlers = new Map<string, () => void>([
  ['start', () => init()],
  ['stop', () => cleanup()]
]);
handlers.get(action)?.();