Skip to content

Prune empty case blocks before a default #4359

@dgp1130

Description

@dgp1130

I think there's an opportunity to further optimize switch statements by removing empty case blocks before a default.

Input:

switch (globalThis.foo) {
  case 'first': {
    console.log('first');
  }
  case 'second':
  default: {
    console.log('default');
  }
}

Expected output:

switch (globalThis.foo) {
  case 'first': console.log('first');
  default: console.log('default');
}

Actual output (0.27.1):

switch (globalThis.foo) {
  case 'first': console.log('first');
  case 'second': // Unnecessary!
  default: console.log('default');
}

Playground link

I think you do need to keep case statements which contain expressions (case getSecond(): ...) as that's probably important for execution order and potential side effects. But I think any literal values should be safe to remove, and that's likely the most common case with switch statements.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions