From 8bb397e575a3df0d6600c4aa4fbc16dc285958aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 8 Nov 2022 12:13:16 -0500 Subject: [PATCH] print better message for null --- packages/babel-helpers/src/helpers-generated.ts | 2 +- packages/babel-helpers/src/helpers/checkInRHS.js | 4 +++- .../rhs-not-object/exec.js | 4 ++++ .../test/fixtures/private-loose/rhs-not-object/exec.js | 4 ++++ .../test/fixtures/private/rhs-not-object/exec.js | 4 ++++ .../test/fixtures/to-native-fields/rhs-not-object/exec.js | 4 ++++ 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index a6091490f15f..8332e714672c 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -43,7 +43,7 @@ export default Object.freeze({ ), checkInRHS: helper( "7.20.1", - "export default function _checkInRHS(value){if(Object(value)!==value)throw TypeError(\"right-hand side of 'in' should be an object, got \"+typeof value);return value}", + 'export default function _checkInRHS(value){if(Object(value)!==value)throw TypeError("right-hand side of \'in\' should be an object, got "+value!==null?typeof value:"null");return value}', ), jsx: helper( "7.0.0-beta.0", diff --git a/packages/babel-helpers/src/helpers/checkInRHS.js b/packages/babel-helpers/src/helpers/checkInRHS.js index e07a3af8352e..4e85d79f74e7 100644 --- a/packages/babel-helpers/src/helpers/checkInRHS.js +++ b/packages/babel-helpers/src/helpers/checkInRHS.js @@ -3,7 +3,9 @@ export default function _checkInRHS(value) { if (Object(value) !== value) { throw TypeError( - "right-hand side of 'in' should be an object, got " + typeof value + "right-hand side of 'in' should be an object, got " + value !== null + ? typeof value + : "null" ); } return value; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js index 412b37f1946d..43c9e8ae5d6e 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js @@ -2,6 +2,7 @@ expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in expect(() => class { static #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -9,6 +10,7 @@ expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-h expect(() => class { static #p() {}; static q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p() {}; static q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p() {}; static q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p() {}; static q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p() {}; static q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -16,6 +18,7 @@ expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' s expect(() => new class { #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -23,5 +26,6 @@ expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side o expect(() => new class { #p() {}; q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p() {}; q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p() {}; q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p() {}; q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p() {}; q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js index 412b37f1946d..43c9e8ae5d6e 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js @@ -2,6 +2,7 @@ expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in expect(() => class { static #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -9,6 +10,7 @@ expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-h expect(() => class { static #p() {}; static q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p() {}; static q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p() {}; static q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p() {}; static q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p() {}; static q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -16,6 +18,7 @@ expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' s expect(() => new class { #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -23,5 +26,6 @@ expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side o expect(() => new class { #p() {}; q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p() {}; q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p() {}; q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p() {}; q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p() {}; q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js index 412b37f1946d..43c9e8ae5d6e 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js @@ -2,6 +2,7 @@ expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in expect(() => class { static #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -9,6 +10,7 @@ expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-h expect(() => class { static #p() {}; static q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p() {}; static q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p() {}; static q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p() {}; static q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p() {}; static q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -16,6 +18,7 @@ expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' s expect(() => new class { #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -23,5 +26,6 @@ expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side o expect(() => new class { #p() {}; q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p() {}; q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p() {}; q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p() {}; q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p() {}; q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js index 412b37f1946d..43c9e8ae5d6e 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js @@ -2,6 +2,7 @@ expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in expect(() => class { static #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -9,6 +10,7 @@ expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-h expect(() => class { static #p() {}; static q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => class { static #p() {}; static q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => class { static #p() {}; static q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => class { static #p() {}; static q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => class { static #p() {}; static q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -16,6 +18,7 @@ expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' s expect(() => new class { #p = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`); @@ -23,5 +26,6 @@ expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side o expect(() => new class { #p() {}; q = #p in "" }).toThrowError(`right-hand side of 'in' should be an object, got string`); expect(() => new class { #p() {}; q = #p in true }).toThrowError(`right-hand side of 'in' should be an object, got boolean`); expect(() => new class { #p() {}; q = #p in void 0 }).toThrowError(`right-hand side of 'in' should be an object, got undefined`); +expect(() => class { static #p = #p in null }).toThrowError(`right-hand side of 'in' should be an object, got null`); expect(() => new class { #p() {}; q = #p in Symbol.iterator }).toThrowError(`right-hand side of 'in' should be an object, got symbol`); expect(() => new class { #p() {}; q = #p in 0n }).toThrowError(`right-hand side of 'in' should be an object, got bigint`);