mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): merge try_compress_type_of_equal_string into try_minimize_binary (#8561)
This compression can be handled by `try_minimize_binary`.
This commit is contained in:
parent
007e8c017c
commit
8f57929419
3 changed files with 16 additions and 35 deletions
|
|
@ -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(_)
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue