You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
functionresolveSource(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(newTypeError(`incorrect target provided to onEvent, typeof target === ${typeoftarget}, expected string, symbol, function or object`));}return{};}}}resolveSource("service");
When built with --minify-syntax, only the "string" branch remains :
functionresolveSource(e){switch("string"){case"string":
case"symbol":
returnconsole.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 :
functionresolveSource(e){returnconsole.log("service id syntax"),{};}
The text was updated successfully, but these errors were encountered:
terser however returns only console.log("service id syntax"); from the whole snippet (can't find a way to share from the REPL ). I don't think expecting esbuild to be that thorough would be any help as it's not esbuild's main concern tho.
Uh oh!
There was an error while loading. Please reload this page.
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
When built with
--minify-syntax
, only the"string"
branch remains :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 :
The text was updated successfully, but these errors were encountered: