Skip to content

swc fixer removes required parentheses around iffe #11322

@lukesandberg

Description

@lukesandberg

Describe the bug

In turbopack we are using the swc fixer transform to normalize the AST after mutating it here

This appears to be removing some necessary parentheses and leading to a parse error about a missing identifier.

This unit test fails in the fixer module

    test_fixer!(
        regression_14,
        "((function () { })() && a, b)",
        "(function () { })() && a, b"
    );

with


----- Actual -----
>>>>> Orig <<<<<
((function () { })() && a, b)
>>>>> Code <<<<<
function() {}() && a, b;


thread 'fixer::tests::regression_14' panicked at crates/swc_ecma_transforms_base/src/tests.rs:201:13:
assertion failed: `(left == right)`
            function() {}() && a, b;

(function() {})() && a, b;


basically the parens around the iffe are removed which leads to a parsing error since it is subsequently parsed as a function declaration but is missing a name.

This is a reduced test case from the original code. afaict both the && and the comma expression are required.

The original code was from a minified node module bundle for the react-google-reviews package and it looked like

fa = function (e) {
    var t = '',
      r = Object.keys(e)
    return (
      r.forEach(function (n, i) {
        var a = e[n]
        ;((function (e) {
          return /[height|width]$/.test(e)
        })((n = da(n))) &&
          'number' == typeof a &&
          (a += 'px'),
          (t +=
            !0 === a ? n : !1 === a ? 'not ' + n : '(' + n + ': ' + a + ')'),
          i < r.length - 1 && (t += ' and '))
      }),
      t
    )
  },

Input code

it('should preserve required parens in logical expressions', () => {
  let x = 2

  ;((function (a) {
    return a
  })(x) && x,
    x)

  expect(x).toBe(4)
})

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.15.3&code=H4sIAAAAAAAAAx2NsQ7CMBBD93yFp%2FZOVAyIDZWBP4nKAZGiBJILioT671w72n62g9JYX7nFO95FqpSvoMinhSLm%2BCKpIiTE%2FAyLj5C%2BUTXkVMcJxJiv%2BDkgiqJjxsmZuBA9WlrUKJDnHYCtaisJ3sTK1BnDgD7tEXUcrMu8te1CFjXgqPkmdGa3svsDAIFoRqcAAAA%3D&config=H4sIAAAAAAAAA1WPSw7DIAxE9zkF8rqLKqq66B16CIs6ERE%2FYSIVRbl7gUDb7Ow3Mx7YBiFgYQkPseUxLx4DU%2FjumXCyEd%2BZAEmDLIPyES5dXbhIE2qmivZDgYhhplhTPF7HW0uAdo6pJxozyqop%2FXdKZ3wg5rOxWNHOms6NQ2sF415rFdtfYvJ0vOAOP1Mvi2Gtd0HxswcLG%2FYPX8DACBYBAAA%3D

SWC Info output

No response

Expected behavior

i would not expect the fixer to introduce a parse error

Actual behavior

The above test is rewritten as

it('should preserve required parens in logical expressions', ()=>{
    let x = 2;
    function(a) {
        return a;
    }(x) && x, x;
    expect(x).toBe(4);
});

which fails to parse

Version

swc_core 45.0.1

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions