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

Invalid Compression #6648

Closed
uaeio opened this issue Dec 14, 2022 · 2 comments
Closed

Invalid Compression #6648

uaeio opened this issue Dec 14, 2022 · 2 comments
Labels
Milestone

Comments

@uaeio
Copy link

uaeio commented Dec 14, 2022

Describe the bug

Steps to reproduce:

  1. Go to https://swc.rs/playground
  2. Enter the next code into the code area:
function memo(getDeps, fn, opts) {
    let x;
    return function() {
        if (getDeps()) {
            
            opts.onChange?.(x = fn())
        }
        return x
    } 
  }

  console.log(
      memo(() =>true, () => Math.random(), { key: '123'})().toString()
  )

This code should when executed return a string with some random number in it

  1. Select minify and compress options
  2. Select ES2020 Target

Input code

function memo(getDeps, fn, opts) {
    let x;
    return function() {
        if (getDeps()) {
            
            opts.onChange?.(x = fn())
        }
        return x
    } 
  }

  console.log(
      memo(() =>true, () => Math.random(), { key: '123'})().toString()
  )

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2020",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.22&code=H4sIAAAAAAAAA1VPSw6CMBDde4rZ0SZNE3WnQRe6deUJCA6FCDOkDAmGcHdbAkTfonnTvs%2B06CmXigkabFg5lDu2nYGCDHArnYZxBwE1CgznmXqU3hMUi1GtkoiqgDVD6d%2BHiL8hhlumW5mRw6tVA6ShNJg21bSxpXGYL6aYM%2B3CkTN1XKOt2alFO38ibJRexPdoYKbwyKS0PqMXN0obGOGNnxMk%2B8MxmbTSVvgpviKnYrn%2BAjw5H7YSAQAA&config=H4sIAAAAAAAAA22TQZLjIAxF7%2BJ1Fl29mEUfYHZ9BkoB4ZDByIVEOq5U7t7CdnCczM56%2FhLiS9y6M9vu69aNkBlz%2FeIpCVy7rw7tAGxzGKU7qEyRh8h4P3QCuUepEv78%2BPzQ35GIcRUcuiGk4KdazNIwZmSu35pUBkzCTQc504%2BGkotGR6KIkN5iA2xCEuwxb6mWYoSR0Vwgt4x6GuTAlHaoCDozZhqfaHJBAiUt9mAOwRlLDhsIGa2EC24SraGSxNpY62SGDo%2Bl76uBixIvEAtIq4XX2QY9seWdKLAYXxK%2FoF2nC1ouuaqCNxml5PTQnCmknQ%2F%2FELXPCMwJBtwyZ%2B51Dnul%2F48qJK9DlKlRnfDWVcJeL2dC8O2GtWvMEja3Mrpisd7QbsVXuLsQB4cGvVe3WzL%2FBLGnrZhMI5JvoboGfpvBEprHPu1pXYE3%2BFe7kudxrHwAOb0ynoYjxbcSA8qJ3BtWI4ReYdbtvY6vtCSHajO6px%2BFa9jWVIcvZGJ9bQ%2BfdSE0x%2FSRjm199VEOkPq2lvcKyJUKbrN582v9092317nUC%2Fy96uY6v457rE8RBAAA

Expected behavior

The produced code has the same behavior as input code.

Explanation:

  • In the original code we first calculating x and only after try to optionally call onChange
  • In the result code we do not calculate x, because it is places inside optional call to onChange

Actual behavior

It returns code which fails to return a string with random number in it.

console.log((function(getDeps,fn,opts){let x;return function(){return getDeps()&&opts.onChange?.(x=fn()),x}})(()=>!0,()=>Math.random(),{key:"123"})().toString());

Version

1.3.22

Additional context

No response

@uaeio uaeio added the C-bug label Dec 14, 2022
@kdy1
Copy link
Member

kdy1 commented Dec 14, 2022

Fixed by #6637

@kdy1 kdy1 closed this as completed Dec 14, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.3.23 Dec 14, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Jan 13, 2023

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 Jan 13, 2023
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