swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.16k stars 1.23k forks source link

swc_ecma_minifier bad case #8651

Closed xc2 closed 8 months ago

xc2 commented 8 months ago

Describe the bug

swc_ecma_minifier produces bad code when optimizing lottie-web with default config.

i've tested with the example "compress" and found that seems https://github.com/swc-project/swc/commit/a69f172aacb29caafef2c2af0659faf1b9154f2c introduced the issue.

Input code

https://github.com/airbnb/lottie-web/blob/63a39aeb27b8c13726ce9fcf850d9351b14be3e6/build/player/lottie.js#L10380-L10392

// input.js
// cargo run -p swc_ecma_minifier --example compress input.js

function renderShape () {
    var i;
    var len = this.animatedContents.length;
    var animatedContent;

    for (i = 0; i < len; i += 1) {
        animatedContent = this.animatedContents[i];

        if ((this._isFirstFrame || animatedContent.element._isAnimated) && animatedContent.data !== true) {
          animatedContent.fn(animatedContent.data, animatedContent.element, this._isFirstFrame);
        }
    }
}

renderShape();

Config

{}

Playground link (or link to the minimal reproduction)

https://github.com/swc-project/swc/blob/main/crates/swc_ecma_minifier/examples/compress.rs

SWC Info output

No response

Expected behavior

https://github.com/swc-project/swc/commit/3b845c33b34aaa5de3d304efc2c8bf8f276c9390

function renderShape() {
    var i, animatedContent, len = this.animatedContents.length;
    for(i = 0; i < len; i += 1)animatedContent = this.animatedContents[i], (this._isFirstFrame || animatedContent.element._isAnimated) && !0 !== animatedContent.data && animatedContent.fn(animatedContent.data, animatedContent.element, this._isFirstFrame);
}
renderShape();

Actual behavior

https://github.com/swc-project/swc/commit/a69f172aacb29caafef2c2af0659faf1b9154f2c

function renderShape() {
    var i, animatedContent, len = this.animatedContents.length;
    for(i = 0; i < len; i += 1)(this._isFirstFrame || (animatedContent = this.animatedContents[i]).element._isAnimated) && !0 !== animatedContent.data && animatedContent.fn(animatedContent.data, animatedContent.element, this._isFirstFrame);
}
renderShape();

Version

0.189.84

Additional context

related: https://github.com/web-infra-dev/rspack/issues/5656

swc-bot commented 7 months ago

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.