Repository navigation
Keep hoisted var declarations when folding away a branch #2279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burrows99
wants to merge
1
commit into
react:main
Choose a base branch
from
burrows99:fix/constant-folding-keep-hoisted-vars
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should fix · A var declared in two dead branches is still dropped
The check
!writes.some(p => p.isVariableDeclarator() && outside(p))skips hoisting when the binding has another declarator outside the current dead branch. Scope bindings aren't recomputed until the whole pass ends (clearScope()/crawl()inProgram.exit). So a declarator that sat in an earlier dead branch, already removed in this pass, still shows up inbinding.path/constantViolationsand counts as "declared elsewhere".Take two dead branches that redeclare the same name:
The output is
use(a);with no declaration, which is the sameReferenceErrorthe PR sets out to fix. On the first fold, the second declarator counts as outside, so nothing is hoisted. On the second fold, the first declarator (already removed) counts as outside. Theif (true) {} else { var a = 1; }form, repeated twice, losesathe same way. A file with severalif (__DEV__) { var x = ... }blocks hits this.Suggested fix: track the dead branches removed so far in the current pass. Then count a declarator as live only if it isn't inside any of them, e.g.
droppedSoFar.every(d => !p.isDescendant(d)), with the list reset per traversal. Please add the two-dead-branches case to the new test.Dropping the "declared elsewhere" clause altogether isn't a good alternative. It emits redundant
var a;declarations next to live ones. It also breaks the existingcan optimize if-else-branches with constant conditionstest, because the extra declaration adds a constant violation that stopsvar b = a + 4from folding.Reproduced: I added a probe test at
packages/metro-transform-plugins/src/__tests__/review-probe-test.jsand ran it withyarn jest --no-watchmanon the head. The two-dead-ifcase printeduse(a);and the two-dead-elsecase printed{}\n{}\nuse(a);, both missingvar a. The base gives the same output. Then I patched in a closure-level list of dropped branch paths and checked declarators against it. Both cases then printedvar a; ..., the live-redeclaration cases were unchanged, and all 30 tests in the constant-folding and probe suites passed. Afterwards I reverted the patch. · idbe5026ce