test: failing tests for constant folding and TDZ issues #3150
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
RE: #3125
Spent a bit of time trying to fix these in
js_parser.go
but couldn't solve it. This PR has a few failing tests which illustrate the problem.There are two issues:
(1) Constant propagation not working properly when the constant is declared after it is used
Consider this program:
The definition of
x
should be hoisted to the top of the enclosing block. Since it is constant, it is eligible for constant folding/propagation. The propagation should respect the Temporal Dead Zone; accessingx
before it is initialized should raise aReferenceError: Cannot access 'x' before initialization
.esbuild
minifies this program to(() => x)()
. I believe this is becausex
is correctly identified as a constant, but then isn't able to do the substitution into the function body because it is defined after the function that uses it.(2) Dead statement removal doesn't respect the Temporal Dead Zone
Consider this program:
This should crash with an error
ReferenceError: Cannot access 'number' before initialization
.However,
esbuild
minifies it to something like{const n=10}
, omitting thenumber;
statement.