mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 06:36:08 +03:00
fix(es/transforms/optimization): Preserve x instanceof Object
(#1630)
This commit is contained in:
parent
d10671bbda
commit
b6ff4d6f71
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user