@@ -187,7 +187,10 @@ impl<'a> PeepholeOptimizations {
187
187
}
188
188
189
189
let Expression :: LogicalExpression ( logical_expr) = & mut expr. right else { return false } ;
190
- let new_op = logical_expr. operator . to_assignment_operator ( ) ;
190
+ // NOTE: if the right hand side is an anonymous function, applying this compression will
191
+ // set the `name` property of that function.
192
+ // Since codes relying on the fact that function's name is undefined should be rare,
193
+ // we do this compression even if `keep_names` is enabled.
191
194
192
195
let (
193
196
AssignmentTarget :: AssignmentTargetIdentifier ( write_id_ref) ,
@@ -202,6 +205,7 @@ impl<'a> PeepholeOptimizations {
202
205
return false ;
203
206
}
204
207
208
+ let new_op = logical_expr. operator . to_assignment_operator ( ) ;
205
209
expr. operator = new_op;
206
210
expr. right = ctx. ast . move_expression ( & mut logical_expr. right ) ;
207
211
true
@@ -1317,11 +1321,8 @@ mod test {
1317
1321
1318
1322
// a.b might have a side effect
1319
1323
test_same ( "x ? a.b = 0 : a.b = 1" ) ;
1320
- // `a = x ? () => 'a' : () => 'b'` does not set the name property of the function
1321
- // TODO: need to pass these tests when `keep_fnames` are introduced
1322
- // test_same("x ? a = () => 'a' : a = () => 'b'");
1323
- // test_same("x ? a = function () { return 'a' } : a = function () { return 'b' }");
1324
- // test_same("x ? a = class { foo = 'a' } : a = class { foo = 'b' }");
1324
+ // `a = x ? () => 'a' : () => 'b'` does not set the name property of the function, but we ignore that difference
1325
+ test ( "x ? a = () => 'a' : a = () => 'b'" , "a = x ? () => 'a' : () => 'b'" ) ;
1325
1326
1326
1327
// for non `=` operators, `GetValue(lref)` is called before `Evaluation of AssignmentExpression`
1327
1328
// so cannot be fold to `a += x ? 0 : 1`
@@ -1376,7 +1377,7 @@ mod test {
1376
1377
test ( "x && (x = g())" , "x &&= g()" ) ;
1377
1378
test ( "x ?? (x = g())" , "x ??= g()" ) ;
1378
1379
1379
- // `||=`, `&&=`, `??=` sets the name property of the function
1380
+ // `||=`, `&&=`, `??=` sets the name property of the function, but we ignore that difference
1380
1381
// Example case: `let f = false; f || (f = () => {}); console.log(f.name)`
1381
1382
test ( "x || (x = () => 'a')" , "x ||= () => 'a'" ) ;
1382
1383
@@ -1424,6 +1425,11 @@ mod test {
1424
1425
// This case is not supported, since the minifier does not support with statements
1425
1426
// test_same("var x; with (z) { x = x || 1 }");
1426
1427
1428
+ // `||=`, `&&=`, `??=` sets the name property of the function, while `= x || y` does not
1429
+ // but we ignore that difference
1430
+ // Example case: `let f = false; f = f || (() => {}); console.log(f.name)`
1431
+ test ( "var x; x = x || (() => 'a')" , "var x; x ||= (() => 'a')" ) ;
1432
+
1427
1433
let target = ESTarget :: ES2019 ;
1428
1434
let code = "var x; x = x || 1" ;
1429
1435
assert_eq ! (
0 commit comments