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

Optimisation breaks boolean statement #6896

Closed
fweinhold opened this issue Feb 2, 2023 · 1 comment · Fixed by #6899
Closed

Optimisation breaks boolean statement #6896

fweinhold opened this issue Feb 2, 2023 · 1 comment · Fixed by #6899
Assignees
Labels
Milestone

Comments

@fweinhold
Copy link

fweinhold commented Feb 2, 2023

Describe the bug

We have next.js project, which recently switched to SWC as the preferred optimiser lib. The components we use in that project originate from an internal component lib which still uses terser for the same purpose.

Now we discovered some very strange behaviour with one of our components, but only when used in the next.js project.

After digging into it a bit, we discovered that the problem seems to be SWC wrongly resolving a boolean construct, when it was beforehand optimised by terser.

In addition to the example I put in below, I made the example executable in a Fiddle: https://jsfiddle.net/nk1x74th/1/ I hope this helps...

Input code

// this the original code, just for comparison
function Limit(min, max) {
  var length = Math.abs(min - max);

  function reachedMin(n) {
    return n < min;
  }

  function reachedMax(n) {
    return n > max;
  }

  function reachedAny(n) {
    return reachedMin(n) || reachedMax(n);
  }

  function constrain(n) {
    if (!reachedAny(n)) return n;
    return reachedMin(n) ? min : max;
  }

  function removeOffset(n) {
    if (!length) return n;
    return n - length * Math.ceil((n - max) / length);
  }

  var self = {
    length: length,
    max: max,
    min: min,
    constrain: constrain,
    reachedAny: reachedAny,
    reachedMax: reachedMax,
    reachedMin: reachedMin,
    removeOffset: removeOffset
  };
  return self;
}
console.log("The result should be 0. And it is:", Limit(0,3).constrain(-1))

// this is the same code after being optimised by terser (with default settings)
console.log("The result should be 0. And it is:",function(n,e){var r=Math.abs(n-e);function t(e){return e<n}function u(n){return n>e}function c(n){return t(n)||u(n)}return{length:r,max:e,min:n,constrain:function(r){return c(r)?t(r)?n:e:r},reachedAny:c,reachedMax:u,reachedMin:t,removeOffset:function(n){return r?n-r*Math.ceil((n-e)/r):n}}}(0,3).constrain(-1));

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": true
    },
    "target": "es5",
    "loose": true,
    "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": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "isModule": "unknown",
  "env": {
    "targets": "",
    "bugfixes": true
  }
}

Playground link

https://play.swc.rs/?version=1.3.32&code=H4sIAAAAAAAAA5VTS1LjMBDd%2BxQ9rGxKiaHYyUCKPSk2cwHhtGNRdouSZD6V%2BO4jxbJlD8mCjdRS%2F9%2FrznOwtTTuQFBa7iWJBkq1QwZvnbFQKe2e7bvQ0ihKqo5KKxXBs2ylTVtJDFrxlcEhAfgQGhqkva3hAbbC1mvxarwNrE5GReKMpggaRVnjbisppcEf3J%2FtNAHBPTi3wv31Z33E1xmfR5%2Fkos8Tff%2FwWZZwPC7j%2FwxVKjJWi3nFsoL0zyJFNlVUXM618f0Bv1Rxqz7wpao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dvmwWm6Rs4TwuUpwQAAA%3D%3D

Expected behavior

The SWC optimisation should not break the logic of the contrain method, even if the code was beforehand optimised by another optimiser.

Actual behavior

constrain:function(r){return c(r)?t(r)?n:e:r} gets transformed to constrain:function(t){return t>e?t<n?n:e:t} which has not the same logic as before

Version

1.3.32

Additional context

No response

@fweinhold fweinhold added the C-bug label Feb 2, 2023
@kdy1 kdy1 self-assigned this Feb 2, 2023
@kdy1 kdy1 added this to the Planned milestone Feb 2, 2023
@kdy1 kdy1 closed this as completed in #6899 Feb 7, 2023
kdy1 added a commit that referenced this issue Feb 7, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.33, v1.3.34 Feb 8, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Mar 10, 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 Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants