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

es(minify): if_return plus sequence optimize make code run in wrong order #9485

Closed
stormslowly opened this issue Aug 22, 2024 · 10 comments
Closed
Labels

Comments

@stormslowly
Copy link
Contributor

Describe the bug

using swc_core: 0.101.4

just parse, resolve, optimize to the input code, then final generated code run in the wrong order.

in the input code, the hook will not be called, is async is false

after optimize

function bug(r, e) {
  var i;
  return e && isPromiseLike((i = r())) ? i : 0;
}

Input code

export function bug(hook, async) {
  var ret;
  ret = hook();
  if (!(async && isPromiseLike(ret))) {
    return 0;
  }
  return ret;
}

Config

No response

Playground link (or link to the minimal reproduction)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b61031e4d72c3952b521891a3b3f7955

SWC Info output

No response

Expected behavior

no matter what value async is, function hook should always be called.

Actual behavior

after optimize

function bug(r, e) {
  var i;
  return e && isPromiseLike((i = r())) ? i : 0;
}

if e( async) is false, the r(hook) will never be called

Version

0.101.4

Additional context

config CompressOption with below, code works as expect

    sequences: 0,
    if_return: false,
    ..Default::default()
@stormslowly
Copy link
Contributor Author

can not be reproduced in https://play.swc.rs with swc 1.7.4 nor nightly

@kdy1 kdy1 added this to the Planned milestone Aug 22, 2024
@kdy1 kdy1 self-assigned this Aug 22, 2024
@kdy1 kdy1 removed this from the Planned milestone Aug 22, 2024
@kdy1 kdy1 removed their assignment Aug 22, 2024
@stormslowly
Copy link
Contributor Author

@kdy1 any hint for me? maybe i can help to fix it

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

It's not a bug of SWC minifier; it's wrong usage I guess

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2024
@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

You may need paren_remover and fixer

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

Can you try the example minifier with your input?

@stormslowly
Copy link
Contributor Author

Can you try the example minifier with your input?

yes, it works as expected .

i am a little confused to when/where to use fixer? after calling optimize ?

@kdy1
Copy link
Member

kdy1 commented Aug 22, 2024

After applying the name mangler or the hygiene pass. optimize applies the name mangler (if mangle option is specified), so it's after optimize in this case.

@stormslowly
Copy link
Contributor Author

thx a lot

@swc-bot
Copy link
Collaborator

swc-bot commented Sep 21, 2024

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants