diff --git a/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs b/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs index 9243180470571..24f60d0038808 100644 --- a/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs +++ b/crates/oxc_ecmascript/src/constant_evaluation/value_type.rs @@ -65,7 +65,7 @@ impl<'a> From<&Expression<'a>> for ValueType { Expression::BooleanLiteral(_) => Self::Boolean, Expression::NullLiteral(_) => Self::Null, Expression::NumericLiteral(_) => Self::Number, - Expression::StringLiteral(_) => Self::String, + Expression::StringLiteral(_) | Expression::TemplateLiteral(_) => Self::String, Expression::ObjectExpression(_) | Expression::ArrayExpression(_) | Expression::RegExpLiteral(_) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs index b90fcb01c7306..ced900bb3bd84 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -769,6 +769,8 @@ impl<'a> PeepholeMinimizeConditions { false } + // `typeof foo === 'number'` -> `typeof foo == 'number'` + // ^^^^^^^^^^ `ValueType::from(&e.left).is_string()` is `true`. // `a instanceof b === true` -> `a instanceof b` // `a instanceof b === false` -> `!(a instanceof b)` // ^^^^^^^^^^^^^^ `ValueType::from(&e.left).is_boolean()` is `true`. @@ -2033,4 +2035,16 @@ mod test { test("if (anything1 ? anything2 : (0, false));", "if (anything1 && anything2);"); test("if(!![]);", "if([]);"); } + + #[test] + fn test_try_compress_type_of_equal_string() { + test("typeof foo === 'number'", "typeof foo == 'number'"); + test("'number' === typeof foo", "'number' == typeof foo"); + test("typeof foo === `number`", "typeof foo == `number`"); + test("`number` === typeof foo", "`number` == typeof foo"); + test("typeof foo !== 'number'", "typeof foo != 'number'"); + test("'number' !== typeof foo", "'number' != typeof foo"); + test("typeof foo !== `number`", "typeof foo != `number`"); + test("`number` !== typeof foo", "`number` != typeof foo"); + } } diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index de73732b0a357..3d6bf479ea39f 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -133,10 +133,7 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { match expr { Expression::ArrowFunctionExpression(e) => self.try_compress_arrow_expression(e, ctx), Expression::ChainExpression(e) => self.try_compress_chain_call_expression(e, ctx), - Expression::BinaryExpression(e) => { - Self::swap_binary_expressions(e); - self.try_compress_type_of_equal_string(e); - } + Expression::BinaryExpression(e) => Self::swap_binary_expressions(e), Expression::AssignmentExpression(e) => { self.try_compress_normal_assignment_to_combined_assignment(e, ctx); self.try_compress_normal_assignment_to_combined_logical_assignment(e, ctx); @@ -1097,24 +1094,6 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { ) } - /// `typeof foo === 'number'` -> `typeof foo == 'number'` - fn try_compress_type_of_equal_string(&mut self, e: &mut BinaryExpression<'a>) { - let op = match e.operator { - BinaryOperator::StrictEquality => BinaryOperator::Equality, - BinaryOperator::StrictInequality => BinaryOperator::Inequality, - _ => return, - }; - if !matches!(&e.left, Expression::UnaryExpression(unary_expr) if unary_expr.operator.is_typeof()) - { - return; - } - if !e.right.is_string_literal() { - return; - } - e.operator = op; - self.changed = true; - } - fn try_compress_chain_call_expression( &mut self, chain_expr: &mut ChainExpression<'a>, @@ -1906,18 +1885,6 @@ mod test { test("void 0 != foo", "foo != null"); } - #[test] - fn test_try_compress_type_of_equal_string() { - test("typeof foo === 'number'", "typeof foo == 'number'"); - test("'number' === typeof foo", "typeof foo == 'number'"); - test("typeof foo === `number`", "typeof foo == 'number'"); - test("`number` === typeof foo", "typeof foo == 'number'"); - test("typeof foo !== 'number'", "typeof foo != 'number'"); - test("'number' !== typeof foo", "typeof foo != 'number'"); - test("typeof foo !== `number`", "typeof foo != 'number'"); - test("`number` !== typeof foo", "typeof foo != 'number'"); - } - #[test] fn test_property_key() { // Object Property