From e7c458ddf821000898122d9474bf2e0da5f9250d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hua=CC=81ng=20Ju=CC=80nlia=CC=80ng?= Date: Tue, 9 Jul 2019 22:10:01 -0400 Subject: [PATCH] fix: custom instOfHandler result should be cast to boolean --- packages/babel-helpers/src/helpers.js | 2 +- .../test/fixtures/instanceof/instanceof/exec.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/babel-helpers/src/helpers.js b/packages/babel-helpers/src/helpers.js index b25b747c9720..b521cb06b9db 100644 --- a/packages/babel-helpers/src/helpers.js +++ b/packages/babel-helpers/src/helpers.js @@ -587,7 +587,7 @@ helpers.wrapNativeSuper = helper("7.0.0-beta.0")` helpers.instanceof = helper("7.0.0-beta.0")` export default function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { - return right[Symbol.hasInstance](left); + return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } diff --git a/packages/babel-plugin-transform-instanceof/test/fixtures/instanceof/instanceof/exec.js b/packages/babel-plugin-transform-instanceof/test/fixtures/instanceof/instanceof/exec.js index 187f18b8f7a2..eb681c286f3f 100644 --- a/packages/babel-plugin-transform-instanceof/test/fixtures/instanceof/instanceof/exec.js +++ b/packages/babel-plugin-transform-instanceof/test/fixtures/instanceof/instanceof/exec.js @@ -4,6 +4,11 @@ foo[Symbol.hasInstance]= function () { return true; }; var bar = {}; expect(bar instanceof foo).toBe(true); + +var qux = {}; +qux[Symbol.hasInstance]= function () { return NaN }; +expect(bar instanceof qux).toBe(false); + expect(new String).toBeInstanceOf(String); //