-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
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
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