Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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
Expand Down
Loading