evanw / evanw/esbuild

Unused variable inside a block is not treeshaken if not at the start of the body

Open
#3,404 0 comments 0 reactions 0 assignees View on GitHub
suboptimal-output
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

If an unused variable is declared somewhere in the middle of the function body, it will not be removed. Seems to apply to block statements in general, any non treeshaken declaration will prevent any further declarations within the block from being removed.

https://esbuild.github.io/try/#YgAwLjE5LjMAe21pbmlmeTogdHJ1ZSwgZXh0ZXJuYWw6IFsid2hlcmV2ZXIiXSwgYnVuZGxlOnRydWUsIGZvcm1hdDogImVzbSIsIHRyZWVTaGFraW5nOiB0cnVlfQBlAGVudHJ5LmpzAGltcG9ydCAqIGFzICQgZnJvbSAid2hlcmV2ZXIiOwoKZXhwb3J0IGZ1bmN0aW9uIE1haW4oKSB7CiAgICAgICBjb25zdCBhID0gMjsKICAgICAgIGNvbnN0IGZvbyA9ICQKICAgICAgIGNvbnN0IGIgPSAzCgogICAgICAgaWYgKGZvbykgewogICAgICAgIGNvbnN0IGMgPSA0OwogICAgICAgIGZvby50b1N0cmluZygpCiAgICAgICAgY29uc3QgZCA9IDUKICAgICAgIH0KICAgICAgIHJldHVybiBmb28KfQ

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked esbuild playground reproduction and inspect the minified output for the function and block declarations shown there. Trace the tree-shaking logic for declarations after a retained declaration, then add a regression test covering this case. Done means the unused declarations are removed without changing the remaining behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.