Skip to content

Commit

Permalink
fix(es/transforms/optimization): Preserve x instanceof Object (#1630)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored May 3, 2021
1 parent d10671b commit b6ff4d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ecmascript/transforms/optimization/src/simplify/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,22 @@ impl SimplifyExpr {
}
}

fn is_obj(e: &Expr) -> bool {
match *e {
Expr::Array { .. }
| Expr::Object { .. }
| Expr::Fn { .. }
| Expr::New { .. } => true,
_ => false,
}
}

// Non-object types are never instances.
if is_non_obj(&left) {
return make_bool_expr(span, false, iter::once(right));
}

if right.is_ident_ref_to(js_word!("Object")) {
if is_obj(&left) && right.is_ident_ref_to(js_word!("Object")) {
return make_bool_expr(span, true, iter::once(left));
}

Expand Down
2 changes: 2 additions & 0 deletions ecmascript/transforms/optimization/src/simplify/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ fn test_fold_instance_of() {

// These cases is foldable, but no handled currently.
fold("new Foo() instanceof Object", "new Foo(), true;");

// These would require type information to fold.
fold_same("[] instanceof Foo");
fold_same("({}) instanceof Foo");
Expand All @@ -1148,6 +1149,7 @@ fn test_fold_instance_of() {

// An unknown value should never be folded.
fold_same("x instanceof Foo");
fold_same("x instanceof Object");
}

#[test]
Expand Down

0 comments on commit b6ff4d6

Please sign in to comment.