Skip to content

Commit

Permalink
refactor(minifier): remove Tri, use Option<bool> instead (#6912)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 26, 2024
1 parent d3c5c78 commit 2c7ac29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 74 deletions.
60 changes: 30 additions & 30 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_syntax::{
};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::{node_util::Ctx, tri::Tri, CompressorPass};
use crate::{node_util::Ctx, CompressorPass};

/// Constant Folding
///
Expand Down Expand Up @@ -270,20 +270,20 @@ impl<'a, 'b> PeepholeFoldConstants {
}
BinaryOperator::Equality => Self::try_abstract_equality_comparison(left, right, ctx),
BinaryOperator::Inequality => {
Self::try_abstract_equality_comparison(left, right, ctx).not()
Self::try_abstract_equality_comparison(left, right, ctx).map(|b| !b)
}
BinaryOperator::StrictEquality => {
Self::try_strict_equality_comparison(left, right, ctx)
}
BinaryOperator::StrictInequality => {
Self::try_strict_equality_comparison(left, right, ctx).not()
Self::try_strict_equality_comparison(left, right, ctx).map(|b| !b)
}
_ => Tri::Unknown,
_ => None,
};
let value = match value {
Tri::True => true,
Tri::False => false,
Tri::Unknown => return None,
Some(true) => true,
Some(false) => false,
None => return None,
};
Some(ctx.ast.expression_boolean_literal(e.span, value))
}
Expand All @@ -293,7 +293,7 @@ impl<'a, 'b> PeepholeFoldConstants {
left_expr: &'b Expression<'a>,
right_expr: &'b Expression<'a>,
ctx: Ctx<'a, 'b>,
) -> Tri {
) -> Option<bool> {
let left = ValueType::from(left_expr);
let right = ValueType::from(right_expr);
if left != ValueType::Undetermined && right != ValueType::Undetermined {
Expand All @@ -304,7 +304,7 @@ impl<'a, 'b> PeepholeFoldConstants {
(left, right),
(ValueType::Null, ValueType::Undefined) | (ValueType::Undefined, ValueType::Null)
) {
return Tri::True;
return Some(true);
}

if matches!((left, right), (ValueType::Number, ValueType::String))
Expand All @@ -327,7 +327,7 @@ impl<'a, 'b> PeepholeFoldConstants {
);
}

return Tri::Unknown;
return None;
}

if matches!((left, right), (ValueType::String, ValueType::Number))
Expand All @@ -350,33 +350,33 @@ impl<'a, 'b> PeepholeFoldConstants {
);
}

return Tri::Unknown;
return None;
}

if matches!(left, ValueType::BigInt) || matches!(right, ValueType::BigInt) {
let left_bigint = ctx.get_side_free_bigint_value(left_expr);
let right_bigint = ctx.get_side_free_bigint_value(right_expr);

if let (Some(l_big), Some(r_big)) = (left_bigint, right_bigint) {
return Tri::from(l_big.eq(&r_big));
return Some(l_big.eq(&r_big));
}
}

if matches!(left, ValueType::String | ValueType::Number)
&& matches!(right, ValueType::Object)
{
return Tri::Unknown;
return None;
}

if matches!(left, ValueType::Object)
&& matches!(right, ValueType::String | ValueType::Number)
{
return Tri::Unknown;
return None;
}

return Tri::False;
return Some(false);
}
Tri::Unknown
None
}

/// <https://tc39.es/ecma262/#sec-strict-equality-comparison>
Expand All @@ -385,13 +385,13 @@ impl<'a, 'b> PeepholeFoldConstants {
left_expr: &'b Expression<'a>,
right_expr: &'b Expression<'a>,
ctx: Ctx<'a, 'b>,
) -> Tri {
) -> Option<bool> {
let left = ValueType::from(left_expr);
let right = ValueType::from(right_expr);
if left != ValueType::Undetermined && right != ValueType::Undetermined {
// Strict equality can only be true for values of the same type.
if left != right {
return Tri::False;
return Some(false);
}
return match left {
ValueType::Number => {
Expand All @@ -400,52 +400,52 @@ impl<'a, 'b> PeepholeFoldConstants {

if let (Some(l_num), Some(r_num)) = (left_number, right_number) {
if l_num.is_nan() || r_num.is_nan() {
return Tri::False;
return Some(false);
}

return Tri::from(l_num == r_num);
return Some(l_num == r_num);
}

Tri::Unknown
None
}
ValueType::String => {
let left_string = ctx.get_side_free_string_value(left_expr);
let right_string = ctx.get_side_free_string_value(right_expr);
if let (Some(left_string), Some(right_string)) = (left_string, right_string) {
// In JS, browsers parse \v differently. So do not compare strings if one contains \v.
if left_string.contains('\u{000B}') || right_string.contains('\u{000B}') {
return Tri::Unknown;
return None;
}

return Tri::from(left_string == right_string);
return Some(left_string == right_string);
}

Tri::Unknown
None
}
ValueType::Undefined | ValueType::Null => Tri::True,
ValueType::Undefined | ValueType::Null => Some(true),
ValueType::Boolean if right.is_boolean() => {
let left = ctx.get_boolean_value(left_expr);
let right = ctx.get_boolean_value(right_expr);
if let (Some(left_bool), Some(right_bool)) = (left, right) {
return Tri::from(left_bool == right_bool);
return Some(left_bool == right_bool);
}
Tri::Unknown
None
}
// TODO
ValueType::BigInt
| ValueType::Object
| ValueType::Boolean
| ValueType::Undetermined => Tri::Unknown,
| ValueType::Undetermined => None,
};
}

// Then, try to evaluate based on the value of the expression.
// There's only one special case:
// Any strict equality comparison against NaN returns false.
if left_expr.is_nan() || right_expr.is_nan() {
return Tri::False;
return Some(false);
}
Tri::Unknown
None
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod compressor;
mod keep_var;
mod node_util;
mod options;
mod tri;

#[cfg(test)]
mod tester;
Expand Down
43 changes: 0 additions & 43 deletions crates/oxc_minifier/src/tri.rs

This file was deleted.

0 comments on commit 2c7ac29

Please sign in to comment.