fix(es/transforms/optimization): Preserve x instanceof Object (#1630)

This commit is contained in:
Niklas Mischkulnig 2021-05-03 11:12:15 +02:00 committed by GitHub
parent d10671bbda
commit b6ff4d6f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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));
}

View File

@ -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");
@ -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]