Skip to content
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

Fix inlining nested block statements in branch simplifier #1536

Merged
merged 2 commits into from
Apr 4, 2021

Conversation

devongovett
Copy link
Contributor

This fixes an issue where the branch simplifier would inline single statement blocks too aggressively, resulting in broken code. For example:

if (Date.now() < 0) {
    for(let i = 0; i < 10; i++){
        if (Date.now() < 0) {
            console.log(1);
        }
    }
} else {
    console.log(2);
}

gets turned into

if (Date.now() < 0) for(let i = 0; i < 10; i++) if (Date.now() < 0) console.log(1);
else console.log(2);

This results in the else being applied to the second if rather than the first.

I've attempted to solve this by computing the depth of statements in the block's first statement until hitting another block statement. If the depth is greater than 1, then it's not safe to inline the block.

Perhaps there's a better way to solve this? i.e. should the printer be handling this based on precedence?

@kdy1 kdy1 added this to the v1.2.52 milestone Apr 4, 2021
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable approach to me. Thanks!

@kdy1 kdy1 merged commit 0d79ca6 into swc-project:master Apr 4, 2021
@devongovett devongovett deleted the fix-branch-simplifier branch April 4, 2021 12:42
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants