fix(minifier): should not merge conditional function calls if referencing the function has a side-effect#8922
Merged
graphite-app[bot] merged 1 commit intomainfrom Feb 6, 2025
Conversation
Member
Author
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
5559619 to
e6446db
Compare
camc314
reviewed
Feb 6, 2025
CodSpeed Performance ReportMerging #8922 will not alter performanceComparing Summary
|
camc314
approved these changes
Feb 6, 2025
Member
Merge activity
|
…cing the function has a side-effect (#8922) `a ? b(c, d) : b(e, d)` should not be compressed to `b(a ? c : e, d)` if `b` has a side effect. Because `b` may update `a` variable. For example, compressing the following code changes the behavior. ```js var foo = () => console.log('foo') var bar = () => { foo = () => console.log('foo2') } bar() ? foo(1, 2) : foo(3, 2) // outputs 'foo' // was compressed into var foo = () => console.log('foo') var bar = () => { foo = () => console.log('foo2') } foo(bar() ? 1 : 3, 2); // outputs 'foo2' ``` We can improve this if we know that `foo` is not updated after it was declared (i.e. `foo` is a const). But I didn't implement it for now. <details> <summary>evaluation steps in detail</summary> Before the compression, the code is evaluated in the following steps: 1. Evaluate `a` 1. Evaluate `b` 1. Evaluate `c` / `e` 1. Evaluate `d` 1. Evaluate `b(c,d)` / `b(e,d)` After the compression, the code is evaluated in the following steps: 1. Evaluate `b` 1. Evaluate `a` 1. Evaluate `c` / `e` 1. Evaluate `d` 1. Evaluate `b(c,d)` / `b(e,d)` The steps of 1 and 2 are reversed. </details> **References** - [Spec of conditional expression](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-conditional-operator-runtime-semantics-evaluation) - [Spec of call expression](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-function-calls-runtime-semantics-evaluation)
e6446db to
4a723f1
Compare
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.

a ? b(c, d) : b(e, d)should not be compressed tob(a ? c : e, d)ifbhas a side effect.Because
bmay updateavariable.For example, compressing the following code changes the behavior.
We can improve this if we know that
foois not updated after it was declared (i.e.foois a const). But I didn't implement it for now.evaluation steps in detail
Before the compression, the code is evaluated in the following steps:
abc/edb(c,d)/b(e,d)After the compression, the code is evaluated in the following steps:
bac/edb(c,d)/b(e,d)The steps of 1 and 2 are reversed.
References