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

minify-simplify + minify-guarded-expressions incorrectly transform a switch statement #171

Closed
asapach opened this issue Oct 1, 2016 · 2 comments · Fixed by #179
Closed
Labels
bug Confirmed bug

Comments

@asapach
Copy link

asapach commented Oct 1, 2016

I've distilled my repro case to this:

function foo(bar) {
    bar = bar || 'baz';

    switch (bar) {
        case 'spam':
            return true;
        case 'ham':
        case 'eggs':
            return false;
    }
    return false;
}

Notice that the function only returns true if bar == 'spam'.

When I run it through minify-simplify plugin (babel --plugins=minify-simplify) it produces a correct result:

function foo(bar) {
    return bar = bar || 'baz', bar === 'spam' || (bar === 'ham' || bar === 'eggs') && false;
}

When I run the source through minify-guarded-expressions separately (babel --plugins=minify-guarded-expressions) it has no effect.

However when I run minify-simplify and minify-guarded-expressions at the same time (babel --plugins=minify-guarded-expressions,minify-simplify) I get this:

function foo(bar) {
    return bar = bar || 'baz', bar === 'spam' || bar === 'ham' || bar === 'eggs';
}

Notice that the function now returns true for all the cases, not just 'spam'.

Here's a REPL

@asapach
Copy link
Author

asapach commented Oct 1, 2016

I guess this can be further simplified to:

foo === 'bar' || foo === 'baz' && false

Running babel --plugins=minify-guarded-expressions produces:

foo === 'bar' || foo === 'baz';

Which is clearly incorrect, since the right branch is effectively dead code. Here's a REPL

@kangax kangax added the bug Confirmed bug label Oct 3, 2016
@kangax
Copy link
Member

kangax commented Oct 3, 2016

Yeah, we should be smarter here. If the value is utilized (passed to function, assigned, returned, etc.) probably need to ignore this optimization entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants