compress: unreachable switch case removal drops a CaseBlock function initializer
Author: ko-successCreated Sep 1, 2026Updated Sep 1, 2026
Version and config
terser 5.51.2{
module: false,
compress: true,
mangle: {},
output: {},
parse: {},
rename: {}
}Input program
switch (0) {
case 0: console.log(g()); break; // fn
case 1: function g() { return "fn"; }
}terser output
var g; console.log(g()); // TypeError: g is not a functionExpected result
The output should preserve the CaseBlock function binding, or perform an equivalent optimization such as:
console.log("fn"); // fnA FunctionDeclaration in any case clause is part of the lexical declarations of the whole CaseBlock.
When the switch statement is evaluated, BlockDeclarationInstantiation initializes the function binding before the selected case is evaluated. Therefore, although the statements of case 1 are unreachable, removing its function declaration also removes observable declaration initialization.
Specification references:
Source: terser/terser