Skip to content

Commit

Permalink
add ==, !=, ===, !== operator support for evaluation (#3693)
Browse files Browse the repository at this point in the history
extracted from #3670

depends on #3685 

adds the equal operators to the evaluator
  • Loading branch information
sokra authored Feb 22, 2023
1 parent a147845 commit 0b844b1
Show file tree
Hide file tree
Showing 56 changed files with 347,979 additions and 139,967 deletions.
16 changes: 15 additions & 1 deletion crates/turbopack-ecmascript/src/analyzer/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn replace_builtin(value: &mut JsValue) -> bool {
match &mut **prop {
// matching constant string property access on an object like `{a: 1, b:
// 2}["a"]`
JsValue::Constant(ConstantValue::StrAtom(_) | ConstantValue::StrWord(_)) => {
JsValue::Constant(ConstantValue::Str(_)) => {
let prop_str = prop.as_str().unwrap();
let mut potential_values = Vec::new();
for (i, part) in parts.iter_mut().enumerate().rev() {
Expand Down Expand Up @@ -463,6 +463,20 @@ pub fn replace_builtin(value: &mut JsValue) -> bool {
true
}
}
// match a binary operator like `a == b`
JsValue::Binary(..) => {
if let Some(v) = value.is_truthy() {
let v = if v {
ConstantValue::True
} else {
ConstantValue::False
};
*value = JsValue::Constant(v);
true
} else {
false
}
}
// match the not operator like `!a`
// Evaluate not when the inner value is truthy or falsy
JsValue::Not(_, ref inner) => match inner.is_truthy() {
Expand Down
28 changes: 28 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,34 @@ impl EvalContext {
..
}) => JsValue::nullish_coalescing(vec![self.eval(left), self.eval(right)]),

Expr::Bin(BinExpr {
op: op!("=="),
left,
right,
..
}) => JsValue::equal(self.eval(left), self.eval(right)),

Expr::Bin(BinExpr {
op: op!("!="),
left,
right,
..
}) => JsValue::not_equal(self.eval(left), self.eval(right)),

Expr::Bin(BinExpr {
op: op!("==="),
left,
right,
..
}) => JsValue::strict_equal(self.eval(left), self.eval(right)),

Expr::Bin(BinExpr {
op: op!("!=="),
left,
right,
..
}) => JsValue::strict_not_equal(self.eval(left), self.eval(right)),

&Expr::Cond(CondExpr {
box ref cons,
box ref alt,
Expand Down
Loading

1 comment on commit 0b844b1

@vercel
Copy link

@vercel vercel bot commented on 0b844b1 Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.