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

Incorrect compression: compound assignment with inc/dec in template string #7697

Closed
onetw012 opened this issue Jul 23, 2023 · 1 comment · Fixed by #7699
Closed

Incorrect compression: compound assignment with inc/dec in template string #7697

onetw012 opened this issue Jul 23, 2023 · 1 comment · Fixed by #7699
Assignees
Labels
Milestone

Comments

@onetw012
Copy link

Describe the bug

SWC with "compress" option transpiles some expressions involving assignment, increment/decrement operator and template literal into invalid JS for ES2015+ targets. The occurrence of this issue appears to have started since v1.3.27.

Input code

let id = 0;

function getId() {
    id = id % 9999;
    return `${id++}`
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "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": true,
        "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,
        "passes": 2
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link

https://play.swc.rs/?version=1.3.70&code=H4sIAAAAAAAAA8tJLVHITFGwVTCw5uJKK81LLsnMz1NITy3xTNHQVKjmUgACsDyQUFWwBAJrsFhRaklpUZ5Cgkp1Zoq2dm0CVy0AFqXyMEoAAAA%3D&config=H4sIAAAAAAAAA32UwW7bMAyG732KwOcdtgLdYQ%2Bw255BUCTKUSaLhkilMYq8%2B2jZSbOGzs3mx5%2BUSIofL7tddyTX%2Fdp9yKf8jLYQlNu%2FWGjKbM9i6cANllyJI3ffrvRIMwo2ETTTZSEd29IDNxW9fv%2Fxtiq6hEhwVay2IeYYpvucDoexANGdTawSsg6Qmf7Xr6zg%2Bwy41Hv7HjGBzU%2BIsWRiZuihaIEdpmRHAnOyRYkyn9SWSKilmGFl8GYsOKo8%2B8gRs%2BR8pB6sNw49KCgWcBxPoMkkl8gyyfWU%2BzTsYV%2F7vvX5ixpONlXLSk44t5bIaR%2FZASOxCTVrFVzgRgkWuNb2qzIGU4BrUTIeMeaNlvwFkAIkS5TtAFrc5hFknLbU4aky5iATy5PCZby1W2bopaYmxqAUdq4MFI5aMwv46mCurNOOs%2BKN8lH0YCAEGRUlNL1HdgctKU8jYFCA9NcGbagWYG6PcIPP7%2BEJ%2Fi235GW%2BNjwGy4dtStOwx%2FQkwQB8QP%2FEQVrBuI2LLInzuM1r9iCjAV51qdTA4w6QB8BoUluXD7Mhz0Mimj7hXt0So8x5a%2BLrarvcdvNgc%2F%2B5A5b1%2FLI6dAP62uC6%2BOemL%2Bv6Z%2FfpdN3Mt9t0kf5clUvYyz%2FC6tcBRQYAAA%3D%3D

Expected behavior

Transpilation to ES5 returns syntactically correct Javascript

var getId=function(){return id%=9999,"".concat(id++)},id=0;

resembling, for instance, Terser with ecma2015 target

let id=0;function getId(){return id%=9999,""+id++}

Actual behavior

It tries to combine compound assignment with increment operator in one expression inside template string:

let id = 0;
function getId() {
    return `${id %= 9999++}`;
}

Version

1.3.70

Additional context

The example above can be reduced to something like:

function add11(a) {
    a += 10
    return `${a++}`
}

which is transpiled into:

function add11(a) {
    return `${a += 10++}`;
}

where 10++ leads to an error Uncaught SyntaxError: Invalid left-hand side expression in postfix operation

@onetw012 onetw012 added the C-bug label Jul 23, 2023
@kdy1 kdy1 self-assigned this Jul 24, 2023
@kdy1 kdy1 added this to the Planned milestone Jul 24, 2023
kdy1 added a commit that referenced this issue Jul 25, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.71 Jul 25, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Aug 24, 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 Aug 24, 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