-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
Description
When a switch statement can be statically analyzed to have only one branch (because of a defined value, a resolved typeof, or any other static switch value), it should be unwrapped instead of remaining as a useless wrapper.
for example here is a polymorphic function that resolves an object differently based on the type of the first parameter : in esbuild playground
source for the minimal reproduction
function resolveSource(target) {
switch ('string' /* infered by TS from "typeof target" */) {
case 'string':
case 'symbol': {
console.log('service id syntax');
return {};
}
case 'function': {
console.log('target getter syntax');
return {};
}
case 'object': {
console.log('direct target syntax');
return {};
}
default: {
if (import.meta.env.DEV) {
warn(new TypeError(`incorrect target provided to onEvent, typeof target === ${typeof target}, expected string, symbol, function or object`));
}
return {};
}
}
}
resolveSource("service");When built with --minify-syntax, only the "string" branch remains :
function resolveSource(e) {
switch ("string") {
case "string":
case "symbol":
return console.log("service id syntax"), {};
}
}But it could be minified further, as only a single branch remains, and it has to be executed if the switch is evaluated, the body of the case should then be unwrapped to something like :
function resolveSource(e) {
return console.log("service id syntax"), {};
}Reactions are currently unavailable