Skip to content

Commit ec5a19b

Browse files
committed
fix(minifier): do not remove binary expressions (oxc-project#6829)
1 parent 2aa763c commit ec5a19b

File tree

2 files changed

+2
-207
lines changed

2 files changed

+2
-207
lines changed

crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
312312
),
313313
));
314314
}
315-
Expression::BinaryExpression(bin_expr) => {
316-
let left = bin_expr.left.may_have_side_effects();
317-
let right = bin_expr.left.may_have_side_effects();
318-
319-
if left && right {
320-
return None;
321-
}
322-
if !left && !right {
323-
return Some(ctx.ast.statement_empty(SPAN));
324-
}
325-
return Some(ctx.ast.statement_expression(
326-
bin_expr.span,
327-
ctx.ast.move_expression(if left {
328-
&mut bin_expr.right
329-
} else {
330-
&mut bin_expr.left
331-
}),
332-
));
333-
}
334315
Expression::FunctionExpression(function_expr) if function_expr.id.is_none() => {
335316
Some(ctx.ast.statement_empty(SPAN))
336317
}
@@ -555,7 +536,7 @@ mod test {
555536
fold("{if(false)if(false)if(false)foo(); {bar()}}", "bar()");
556537

557538
fold("{'hi'}", "");
558-
fold("{x==3}", "");
539+
fold("{x==3}", "x == 3");
559540
fold("{`hello ${foo}`}", "");
560541
fold("{ (function(){x++}) }", "");
561542
fold_same("function f(){return;}");

tasks/coverage/snapshots/runtime.snap

Lines changed: 1 addition & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 06454619
22

33
runtime Summary:
44
AST Parsed : 18444/18444 (100.00%)
5-
Positive Passed: 18198/18444 (98.67%)
5+
Positive Passed: 18260/18444 (99.00%)
66
tasks/coverage/test262/test/annexB/language/function-code/if-decl-else-decl-a-func-existing-block-fn-no-init.js
77
minify error: Test262Error: Expected SameValue(«function f(){}», «undefined») to be true
88

@@ -321,66 +321,12 @@ minify error: SyntaxError: Identifier 'f' has already been declared
321321
tasks/coverage/test262/test/language/expressions/addition/S11.6.1_A4_T5.js
322322
minify error: Test262Error: #1.1: -0 + -0 === - 0. Actual: +0
323323

324-
tasks/coverage/test262/test/language/expressions/addition/bigint-and-number.js
325-
minify error: Test262Error: 1n + 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
326-
327-
tasks/coverage/test262/test/language/expressions/addition/bigint-errors.js
328-
minify error: Test262Error: 0n + Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
329-
330-
tasks/coverage/test262/test/language/expressions/addition/bigint-toprimitive.js
331-
minify error: Test262Error: 0n + {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
332-
333-
tasks/coverage/test262/test/language/expressions/addition/coerce-symbol-to-prim-err.js
334-
minify error: Test262Error: error thrown by left-hand side Expected a Test262Error to be thrown but no exception was thrown at all
335-
336-
tasks/coverage/test262/test/language/expressions/addition/coerce-symbol-to-prim-invocation.js
337-
minify error: Test262Error: methods invoked in correct sequence Expected SameValue""», «"leftright"») to be true
338-
339-
tasks/coverage/test262/test/language/expressions/addition/coerce-symbol-to-prim-return-obj.js
340-
minify error: Test262Error: ordinary object value, right-hand side Expected a TypeError to be thrown but no exception was thrown at all
341-
342-
tasks/coverage/test262/test/language/expressions/addition/coerce-symbol-to-prim-return-prim.js
343-
minify error: Test262Error: ToNumber(Symbol): right-hand side Expected a TypeError to be thrown but no exception was thrown at all
344-
345-
tasks/coverage/test262/test/language/expressions/addition/get-symbol-to-prim-err.js
346-
minify error: Test262Error: error from property access of left-hand side Expected a Test262Error to be thrown but no exception was thrown at all
347-
348-
tasks/coverage/test262/test/language/expressions/addition/symbol-to-string.js
349-
minify error: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
350-
351324
tasks/coverage/test262/test/language/expressions/array/spread-obj-getter-init.js
352325
transform error: Test262Error: Expected SameValuetrue», «false») to be true
353326

354327
tasks/coverage/test262/test/language/expressions/assignment/fn-name-lhs-cover.js
355328
codegen error: Test262Error: descriptor value should be ; object value should be
356329

357-
tasks/coverage/test262/test/language/expressions/bitwise-and/bigint-and-number.js
358-
minify error: Test262Error: 1n & 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
359-
360-
tasks/coverage/test262/test/language/expressions/bitwise-and/bigint-errors.js
361-
minify error: Test262Error: 0n & Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
362-
363-
tasks/coverage/test262/test/language/expressions/bitwise-and/bigint-toprimitive.js
364-
minify error: Test262Error: 0n & {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
365-
366-
tasks/coverage/test262/test/language/expressions/bitwise-or/bigint-and-number.js
367-
minify error: Test262Error: 1n | 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
368-
369-
tasks/coverage/test262/test/language/expressions/bitwise-or/bigint-errors.js
370-
minify error: Test262Error: 0n | Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
371-
372-
tasks/coverage/test262/test/language/expressions/bitwise-or/bigint-toprimitive.js
373-
minify error: Test262Error: 0n | {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
374-
375-
tasks/coverage/test262/test/language/expressions/bitwise-xor/bigint-and-number.js
376-
minify error: Test262Error: 1n ^ 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
377-
378-
tasks/coverage/test262/test/language/expressions/bitwise-xor/bigint-errors.js
379-
minify error: Test262Error: 0n ^ Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
380-
381-
tasks/coverage/test262/test/language/expressions/bitwise-xor/bigint-toprimitive.js
382-
minify error: Test262Error: 0n ^ {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
383-
384330
tasks/coverage/test262/test/language/expressions/call/spread-obj-getter-init.js
385331
transform error: Test262Error: Expected SameValuetrue», «false») to be true
386332

@@ -390,18 +336,6 @@ minify error: Test262Error: Expected SameValue(«0», «1») to be true
390336
tasks/coverage/test262/test/language/expressions/division/S11.5.2_A4_T8.js
391337
minify error: Test262Error: #1.2: -0 / 1 === - 0. Actual: +0
392338

393-
tasks/coverage/test262/test/language/expressions/division/bigint-and-number.js
394-
minify error: Test262Error: 1n / 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
395-
396-
tasks/coverage/test262/test/language/expressions/division/bigint-complex-infinity.js
397-
minify error: Test262Error: 1n / 0n throws RangeError Expected a RangeError to be thrown but no exception was thrown at all
398-
399-
tasks/coverage/test262/test/language/expressions/division/bigint-errors.js
400-
minify error: Test262Error: 0n / Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
401-
402-
tasks/coverage/test262/test/language/expressions/division/bigint-toprimitive.js
403-
minify error: Test262Error: 0n / {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
404-
405339
tasks/coverage/test262/test/language/expressions/does-not-equals/bigint-and-object.js
406340
minify error: Test262Error: The result of (0n != {valueOf: function() {return 0n;}}) is false Expected SameValuetrue», «false») to be true
407341

@@ -444,69 +378,18 @@ codegen error: Test262Error: constructor is %Promise% Expected SameValue(«[obje
444378
tasks/coverage/test262/test/language/expressions/equals/bigint-and-object.js
445379
minify error: Test262Error: The result of (0n == {valueOf: function() {return 0n;}}) is true Expected SameValuefalse», «true») to be true
446380

447-
tasks/coverage/test262/test/language/expressions/equals/coerce-symbol-to-prim-err.js
448-
minify error: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
449-
450-
tasks/coverage/test262/test/language/expressions/equals/coerce-symbol-to-prim-invocation.js
451-
minify error: Test262Error: method invoked exactly once Expected SameValue0», «1») to be true
452-
453-
tasks/coverage/test262/test/language/expressions/equals/coerce-symbol-to-prim-return-obj.js
454-
minify error: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
455-
456-
tasks/coverage/test262/test/language/expressions/equals/get-symbol-to-prim-err.js
457-
minify error: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
458-
459-
tasks/coverage/test262/test/language/expressions/exponentiation/bigint-and-number.js
460-
minify error: Test262Error: 1n ** 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
461-
462-
tasks/coverage/test262/test/language/expressions/exponentiation/bigint-errors.js
463-
minify error: Test262Error: 0n ** Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
464-
465381
tasks/coverage/test262/test/language/expressions/exponentiation/bigint-negative-exponent-throws.js
466382
transform error: Test262Error: (-1n) ** -1n throws RangeError Expected a RangeError but got a TypeError
467383

468-
tasks/coverage/test262/test/language/expressions/exponentiation/bigint-toprimitive.js
469-
minify error: Test262Error: 0n ** {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
470-
471384
tasks/coverage/test262/test/language/expressions/greater-than/bigint-and-incomparable-string.js
472385
minify error: Test262Error: The result of (1n > "0.") is false Expected SameValuetrue», «false») to be true
473386

474-
tasks/coverage/test262/test/language/expressions/greater-than/bigint-and-symbol.js
475-
minify error: Test262Error: 3n > Symbol("2") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
476-
477387
tasks/coverage/test262/test/language/expressions/greater-than-or-equal/bigint-and-incomparable-string.js
478388
minify error: Test262Error: The result of (1n >= "0.") is false Expected SameValuetrue», «false») to be true
479389

480-
tasks/coverage/test262/test/language/expressions/instanceof/primitive-prototype-with-object.js
481-
minify error: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
482-
483-
tasks/coverage/test262/test/language/expressions/instanceof/prototype-getter-with-object-throws.js
484-
minify error: Test262Error: Expected a DummyError to be thrown but no exception was thrown at all
485-
486-
tasks/coverage/test262/test/language/expressions/instanceof/symbol-hasinstance-get-err.js
487-
minify error: Test262Error: Expected a Test262Error to be thrown but no exception was thrown at all
488-
489-
tasks/coverage/test262/test/language/expressions/instanceof/symbol-hasinstance-invocation.js
490-
minify error: Test262Error: Expected SameValue0», «1») to be true
491-
492-
tasks/coverage/test262/test/language/expressions/instanceof/symbol-hasinstance-not-callable.js
493-
minify error: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all
494-
495-
tasks/coverage/test262/test/language/expressions/left-shift/bigint-and-number.js
496-
minify error: Test262Error: 1n << 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
497-
498-
tasks/coverage/test262/test/language/expressions/left-shift/bigint-errors.js
499-
minify error: Test262Error: 0n << Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
500-
501-
tasks/coverage/test262/test/language/expressions/left-shift/bigint-toprimitive.js
502-
minify error: Test262Error: 0n << {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
503-
504390
tasks/coverage/test262/test/language/expressions/less-than/bigint-and-incomparable-string.js
505391
minify error: Test262Error: The result of ("0." < 1n) is false Expected SameValuetrue», «false») to be true
506392

507-
tasks/coverage/test262/test/language/expressions/less-than/bigint-and-symbol.js
508-
minify error: Test262Error: 3n < Symbol("2") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
509-
510393
tasks/coverage/test262/test/language/expressions/less-than-or-equal/bigint-and-incomparable-string.js
511394
minify error: Test262Error: The result of ("0." <= 1n) is false Expected SameValuetrue», «false») to be true
512395

@@ -525,30 +408,9 @@ minify error: Test262Error: #2.2: -1 % -1 === - 0. Actual: +0
525408
tasks/coverage/test262/test/language/expressions/modulus/S11.5.3_A4_T6.js
526409
minify error: Test262Error: #3.2: -0 % 1 === - 0. Actual: +0
527410

528-
tasks/coverage/test262/test/language/expressions/modulus/bigint-and-number.js
529-
minify error: Test262Error: 1n % 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
530-
531-
tasks/coverage/test262/test/language/expressions/modulus/bigint-errors.js
532-
minify error: Test262Error: 0n % Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
533-
534-
tasks/coverage/test262/test/language/expressions/modulus/bigint-modulo-zero.js
535-
minify error: Test262Error: 1n % 0n throws RangeError Expected a RangeError to be thrown but no exception was thrown at all
536-
537-
tasks/coverage/test262/test/language/expressions/modulus/bigint-toprimitive.js
538-
minify error: Test262Error: 0n % {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
539-
540411
tasks/coverage/test262/test/language/expressions/multiplication/S11.5.1_A4_T2.js
541412
minify error: Test262Error: #6.2: 0 * -0 === - 0. Actual: +0
542413

543-
tasks/coverage/test262/test/language/expressions/multiplication/bigint-and-number.js
544-
minify error: Test262Error: 1n * 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
545-
546-
tasks/coverage/test262/test/language/expressions/multiplication/bigint-errors.js
547-
minify error: Test262Error: 0n * Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
548-
549-
tasks/coverage/test262/test/language/expressions/multiplication/bigint-toprimitive.js
550-
minify error: Test262Error: 0n * {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
551-
552414
tasks/coverage/test262/test/language/expressions/new/spread-obj-getter-init.js
553415
transform error: Test262Error: Expected SameValuetrue», «false») to be true
554416

@@ -576,27 +438,9 @@ minify error: Test262Error: Expected a Test262Error to be thrown but no exceptio
576438
tasks/coverage/test262/test/language/expressions/object/prop-def-id-get-error.js
577439
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
578440

579-
tasks/coverage/test262/test/language/expressions/right-shift/bigint-and-number.js
580-
minify error: Test262Error: 1n >> 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
581-
582-
tasks/coverage/test262/test/language/expressions/right-shift/bigint-errors.js
583-
minify error: Test262Error: 0n >> Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
584-
585-
tasks/coverage/test262/test/language/expressions/right-shift/bigint-toprimitive.js
586-
minify error: Test262Error: 0n >> {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
587-
588441
tasks/coverage/test262/test/language/expressions/subtraction/S11.6.2_A4_T5.js
589442
minify error: Test262Error: #3.2: -0 - 0 === - 0. Actual: +0
590443

591-
tasks/coverage/test262/test/language/expressions/subtraction/bigint-and-number.js
592-
minify error: Test262Error: 1n - 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
593-
594-
tasks/coverage/test262/test/language/expressions/subtraction/bigint-errors.js
595-
minify error: Test262Error: 0n - Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
596-
597-
tasks/coverage/test262/test/language/expressions/subtraction/bigint-toprimitive.js
598-
minify error: Test262Error: 0n - {[Symbol.toPrimitive]: 1} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
599-
600444
tasks/coverage/test262/test/language/expressions/super/call-spread-obj-getter-init.js
601445
transform error: Test262Error: Expected SameValuetrue», «false») to be true
602446

@@ -618,42 +462,12 @@ minify error: Test262Error: #3.2: +(-0) === -0. Actual: +0
618462
tasks/coverage/test262/test/language/expressions/unary-plus/bigint-throws.js
619463
minify error: Test262Error: +0n throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
620464

621-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint-and-number.js
622-
minify error: Test262Error: 1n >>> 1 throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
623-
624-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint-errors.js
625-
minify error: Test262Error: 0n >>> Symbol("1") throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
626-
627-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint-non-primitive.js
628-
minify error: Test262Error: ({valueOf: function() {return 0b101n;}, toString: err}) >>> 1n throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
629-
630-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint-toprimitive.js
631-
minify error: Test262Error: 0n >>> {[Symbol.toPrimitive]: function() {return 2n;}, valueOf: err, toString: err} throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
632-
633-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint-wrapped-values.js
634-
minify error: Test262Error: 0n >>> Object(2n) throws TypeError Expected a TypeError to be thrown but no exception was thrown at all
635-
636-
tasks/coverage/test262/test/language/expressions/unsigned-right-shift/bigint.js
637-
minify error: Test262Error: bigint >>> bigint throws a TypeError Expected a TypeError to be thrown but no exception was thrown at all
638-
639465
tasks/coverage/test262/test/language/expressions/void/S11.4.2_A2_T2.js
640466
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
641467

642468
tasks/coverage/test262/test/language/global-code/decl-func.js
643469
codegen error: Test262Error: descriptor should not be configurable
644470

645-
tasks/coverage/test262/test/language/literals/numeric/S7.8.3_A4.1_T3.js
646-
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
647-
648-
tasks/coverage/test262/test/language/literals/numeric/S7.8.3_A4.1_T4.js
649-
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
650-
651-
tasks/coverage/test262/test/language/literals/numeric/S7.8.3_A4.1_T5.js
652-
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
653-
654-
tasks/coverage/test262/test/language/literals/numeric/S7.8.3_A4.1_T6.js
655-
minify error: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all
656-
657471
tasks/coverage/test262/test/language/literals/regexp/u-surrogate-pairs-atom-escape-decimal.js
658472
codegen error: Test262Error: Expected SameValuetrue», «false») to be true
659473

0 commit comments

Comments
 (0)