@@ -187,7 +187,10 @@ impl<'a> PeepholeOptimizations {
187187 }
188188
189189 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.
191194
192195 let (
193196 AssignmentTarget :: AssignmentTargetIdentifier ( write_id_ref) ,
@@ -202,6 +205,7 @@ impl<'a> PeepholeOptimizations {
202205 return false ;
203206 }
204207
208+ let new_op = logical_expr. operator . to_assignment_operator ( ) ;
205209 expr. operator = new_op;
206210 expr. right = ctx. ast . move_expression ( & mut logical_expr. right ) ;
207211 true
@@ -1317,11 +1321,8 @@ mod test {
13171321
13181322 // a.b might have a side effect
13191323 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'" ) ;
13251326
13261327 // for non `=` operators, `GetValue(lref)` is called before `Evaluation of AssignmentExpression`
13271328 // so cannot be fold to `a += x ? 0 : 1`
@@ -1376,7 +1377,7 @@ mod test {
13761377 test ( "x && (x = g())" , "x &&= g()" ) ;
13771378 test ( "x ?? (x = g())" , "x ??= g()" ) ;
13781379
1379- // `||=`, `&&=`, `??=` sets the name property of the function
1380+ // `||=`, `&&=`, `??=` sets the name property of the function, but we ignore that difference
13801381 // Example case: `let f = false; f || (f = () => {}); console.log(f.name)`
13811382 test ( "x || (x = () => 'a')" , "x ||= () => 'a'" ) ;
13821383
@@ -1424,6 +1425,11 @@ mod test {
14241425 // This case is not supported, since the minifier does not support with statements
14251426 // test_same("var x; with (z) { x = x || 1 }");
14261427
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+
14271433 let target = ESTarget :: ES2019 ;
14281434 let code = "var x; x = x || 1" ;
14291435 assert_eq ! (
0 commit comments