fix: late constant folding for cross-module if-statement DCE#4413
Open
Felipeness wants to merge 2 commits intoevanw:mainfrom
Open
fix: late constant folding for cross-module if-statement DCE#4413Felipeness wants to merge 2 commits intoevanw:mainfrom
Felipeness wants to merge 2 commits intoevanw:mainfrom
Conversation
When bundling with --minify-syntax, imported constants like `export const DEV = false` are inlined at print time via ConstValues. However, `if` statements with these inlined constants were not folded because mangleIf() runs at parse time (before cross-module resolution) and the printer's late constant folding only handled expressions (EIf), not statements (SIf). This adds late constant folding for SIf in printStmt(): when the test expression resolves to a known boolean after ConstValues substitution, the dead branch is eliminated entirely. Also drops residual side-effect-free expression statements (like `!1;`) that result from cross-module constant inlining in logical expressions. Fixes evanw#2589, fixes evanw#3964.
Author
|
Hey @evanw, just a gentle ping on this and the related #4412. Both PRs have passing tests (\FAIL ./... [setup failed] |
care44cubs-max
approved these changes
Apr 3, 2026
care44cubs-max
approved these changes
Apr 3, 2026
care44cubs-max
approved these changes
Apr 3, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When bundling with
--minify-syntax, imported constants likeexport const DEV = falseare inlined at print time viaConstValues. However,ifstatements (SIf) with these inlined constants were not folded — onlyifexpressions (EIf/ternary) were. This left dead branches in the output:Before:
After:
Approach
Added late constant folding for
SIfinprintStmt()(mirroring howEIfis already handled inlateConstantFoldUnaryOrBinaryOrIfExpr()):MinifySyntaxis enabled, fold the test expression throughlateConstantFoldUnaryOrBinaryOrIfExpr()(which resolvesEImportIdentifierrefs viaConstValues)ToBooleanWithSideEffects()to determine if the condition is statically knownAlso added
isLateConstantFoldedSideEffectFree()to drop residual expression statements like!1;that result from logical expressions with inlined constants (e.g.,DEV && console.log("debug")→!1;).Test
Three new snapshot tests added:
TestCrossModuleConstantFoldingIfStmt— basicif (DEV)eliminationTestCrossModuleConstantFoldingIfStmtThrow— if/else withthrowin both branchesTestCrossModuleConstantFoldingIfStmtMultiple— multiple independentifstatementsFixes #2589, fixes #3964.