diff --git a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs index 0ce6209565ad5..15a8587ed3521 100644 --- a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs @@ -187,7 +187,7 @@ impl<'a> PeepholeOptimizations { fn swap_binary_expressions(e: &mut BinaryExpression<'a>) { if e.operator.is_equality() - && (e.left.is_literal() || e.left.is_no_substitution_template()) + && (e.left.is_literal() || e.left.is_no_substitution_template() || e.left.is_void_0()) && !e.right.is_literal() { std::mem::swap(&mut e.left, &mut e.right); @@ -1638,6 +1638,25 @@ mod test { test_same("var foo; v = typeof foo == 'string' && foo !== null"); } + #[test] + fn test_swap_binary_expressions() { + test_same("v = a === 0"); + test("v = 0 === a", "v = a === 0"); + test_same("v = a === '0'"); + test("v = '0' === a", "v = a === '0'"); + test("v = a === `0`", "v = a === '0'"); + test("v = `0` === a", "v = a === '0'"); + test_same("v = a === void 0"); + test("v = void 0 === a", "v = a === void 0"); + + test_same("v = a !== 0"); + test("v = 0 !== a", "v = a !== 0"); + test_same("v = a == 0"); + test("v = 0 == a", "v = a == 0"); + test_same("v = a != 0"); + test("v = 0 != a", "v = a != 0"); + } + #[test] fn test_remove_unary_plus() { test("v = 1 - +foo", "v = 1 - foo"); diff --git a/crates/oxc_minifier/tests/peephole/esbuild.rs b/crates/oxc_minifier/tests/peephole/esbuild.rs index 365e4ea56e91b..3ba3e0213b4ae 100644 --- a/crates/oxc_minifier/tests/peephole/esbuild.rs +++ b/crates/oxc_minifier/tests/peephole/esbuild.rs @@ -1388,8 +1388,8 @@ fn test_flatten_values() { test("return a || (b && c)", "return a || b && c;"); test("return a === void 0", "return a === void 0;"); test("return a !== void 0", "return a !== void 0;"); - // test("return void 0 === a", "return a === void 0;"); - // test("return void 0 !== a", "return a !== void 0;"); + test("return void 0 === a", "return a === void 0;"); + test("return void 0 !== a", "return a !== void 0;"); test("return a == void 0", "return a == null;"); test("return a != void 0", "return a != null;"); test("return void 0 == a", "return a == null;");