From 2bdadfd942b441d315aea8860cef230828a0406e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 4 Nov 2022 16:40:03 -0400 Subject: [PATCH] fix: validate in-rhs C when transpiling `#p in C` --- Makefile | 4 +-- .../src/fields.ts | 18 ++++++++++--- .../babel-helpers/src/helpers-generated.ts | 4 +++ .../babel-helpers/src/helpers/checkInRHS.js | 9 +++++++ .../src/index.ts | 14 +++++++--- .../accessor/output.js | 2 +- .../compiled-classes/output.js | 4 +-- .../field/output.js | 2 +- .../method/output.js | 2 +- .../nested-class-other-redeclared/output.js | 8 +++--- .../nested-class-redeclared/output.js | 4 +-- .../nested-class/output.js | 4 +-- .../rhs-not-object/exec.js | 27 +++++++++++++++++++ .../rhs-not-object/options.json | 3 +++ .../static-accessor/output.js | 2 +- .../static-field/output.js | 2 +- .../static-method/output.js | 2 +- .../fixtures/private-loose/accessor/output.js | 2 +- .../fixtures/private-loose/field/output.js | 2 +- .../fixtures/private-loose/method/output.js | 2 +- .../private-loose/native-classes/output.js | 4 +-- .../nested-class-other-redeclared/output.js | 8 +++--- .../nested-class-redeclared/output.js | 4 +-- .../private-loose/nested-class/output.js | 4 +-- .../private-loose/rhs-not-object/exec.js | 27 +++++++++++++++++++ .../private-loose/rhs-not-object/options.json | 3 +++ .../private-loose/static-accessor/output.js | 2 +- .../private-loose/static-field/output.js | 2 +- .../private-loose/static-method/output.js | 2 +- .../private-loose/static-shadow/output.js | 2 +- .../test/fixtures/private/accessor/output.js | 2 +- .../test/fixtures/private/field/output.js | 2 +- .../test/fixtures/private/method/output.js | 2 +- .../fixtures/private/native-classes/output.js | 4 +-- .../nested-class-other-redeclared/output.js | 8 +++--- .../private/nested-class-redeclared/output.js | 4 +-- .../fixtures/private/nested-class/output.js | 4 +-- .../fixtures/private/rhs-not-object/exec.js | 27 +++++++++++++++++++ .../private/rhs-not-object/options.json | 3 +++ .../private/static-accessor/output.js | 2 +- .../fixtures/private/static-field/output.js | 2 +- .../fixtures/private/static-method/output.js | 2 +- .../fixtures/private/static-shadow/output.js | 2 +- .../to-native-fields/accessor/output.js | 2 +- .../output.js | 2 +- .../class-expression-instance/output.js | 2 +- .../class-expression-static/output.js | 2 +- .../fixtures/to-native-fields/field/output.js | 2 +- .../half-constructed-instance/output.js | 8 +++--- .../half-constructed-static/output.js | 6 ++--- .../to-native-fields/method/output.js | 2 +- .../multiple-checks/output.js | 8 +++--- .../nested-class-other-redeclared/output.js | 8 +++--- .../nested-class-redeclared/output.js | 4 +-- .../to-native-fields/nested-class/output.js | 4 +-- .../to-native-fields/rhs-not-object/exec.js | 27 +++++++++++++++++++ .../rhs-not-object/options.json | 3 +++ .../static-accessor/output.js | 2 +- .../to-native-fields/static-field/output.js | 2 +- .../to-native-fields/static-method/output.js | 2 +- .../to-native-fields/static-shadow/output.js | 2 +- .../static-shadowed-binding/output.js | 2 +- packages/babel-runtime-corejs2/package.json | 9 +++++++ packages/babel-runtime-corejs3/package.json | 9 +++++++ packages/babel-runtime/package.json | 9 +++++++ 65 files changed, 266 insertions(+), 90 deletions(-) create mode 100644 packages/babel-helpers/src/helpers/checkInRHS.js create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/options.json create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/options.json create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/options.json create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js create mode 100644 packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/options.json diff --git a/Makefile b/Makefile index ee06da1a82af..50390d60c967 100644 --- a/Makefile +++ b/Makefile @@ -192,8 +192,8 @@ new-version-checklist: # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # @echo "!!!!!! !!!!!!" - # @echo "!!!!!! Write any message that should !!!!!!" - # @echo "!!!!!! block the release here !!!!!!" + # @echo "!!!!!! Update the minVersion of packages/babel-helpers/src/helpers/checkInRHS.js" + # @echo "!!!!!! !!!!!!" # @echo "!!!!!! !!!!!!" # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" # @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" diff --git a/packages/babel-helper-create-class-features-plugin/src/fields.ts b/packages/babel-helper-create-class-features-plugin/src/fields.ts index 6f814e949efb..18715c6b1e8d 100644 --- a/packages/babel-helper-create-class-features-plugin/src/fields.ts +++ b/packages/babel-helper-create-class-features-plugin/src/fields.ts @@ -211,7 +211,7 @@ const privateInVisitor = privateNameVisitorFactory<{ file: File; innerBinding?: t.Identifier; }>({ - BinaryExpression(path) { + BinaryExpression(path, { file }) { const { operator, left, right } = path.node; if (operator !== "in") return; if (!t.isPrivateName(left)) return; @@ -230,7 +230,9 @@ const privateInVisitor = privateNameVisitorFactory<{ if (privateFieldsAsProperties) { const { id } = privateNamesMap.get(name); path.replaceWith(template.expression.ast` - Object.prototype.hasOwnProperty.call(${right}, ${t.cloneNode(id)}) + Object.prototype.hasOwnProperty.call(${file.addHelper( + "checkInRHS", + )}(${right}), ${t.cloneNode(id)}) `); return; } @@ -238,11 +240,19 @@ const privateInVisitor = privateNameVisitorFactory<{ const { id, static: isStatic } = privateNamesMap.get(name); if (isStatic) { - path.replaceWith(template.expression.ast`${right} === ${this.classRef}`); + path.replaceWith( + template.expression.ast`${file.addHelper( + "checkInRHS", + )}(${right}) === ${t.cloneNode(this.classRef)}`, + ); return; } - path.replaceWith(template.expression.ast`${t.cloneNode(id)}.has(${right})`); + path.replaceWith( + template.expression.ast`${t.cloneNode(id)}.has(${file.addHelper( + "checkInRHS", + )}(${right}))`, + ); }, }); diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index 17c1db2e87cb..35fdc8b03ac3 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -41,6 +41,10 @@ export default Object.freeze({ "7.0.0-beta.0", 'import OverloadYield from"OverloadYield";export default function _awaitAsyncGenerator(value){return new OverloadYield(value,0)}', ), + checkInRHS: helper( + "7.20.1", + 'export default function _checkInRHS(value){var type=typeof value;if("object"!==type&&"function"!==type)throw TypeError("right-hand side of \'in\' should be an object, got "+type);return value}', + ), jsx: helper( "7.0.0-beta.0", 'var REACT_ELEMENT_TYPE;export default function _createRawReactElement(type,props,key,children){REACT_ELEMENT_TYPE||(REACT_ELEMENT_TYPE="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103);var defaultProps=type&&type.defaultProps,childrenLength=arguments.length-3;if(props||0===childrenLength||(props={children:void 0}),1===childrenLength)props.children=children;else if(childrenLength>1){for(var childArray=new Array(childrenLength),i=0;i { enableFeature(this.file, FEATURES.privateIn, loose); }, visitor: { - BinaryExpression(path) { + BinaryExpression(path, state) { const { node } = path; if (node.operator !== "in") return; if (!t.isPrivateName(node.left)) return; @@ -158,7 +158,9 @@ export default declare((api, opt: Options) => { } path.replaceWith( template.expression.ast` - ${t.cloneNode(outerClass.node.id)} === ${path.node.right} + ${t.cloneNode(outerClass.node.id)} === ${state.addHelper( + "checkInRHS", + )}(${node.right}) `, ); } else { @@ -171,7 +173,9 @@ export default declare((api, opt: Options) => { ); path.replaceWith( - template.expression.ast`${id}.has(${path.node.right})`, + template.expression.ast`${id}.has(${state.addHelper( + "checkInRHS", + )}(${node.right}))`, ); } } else { @@ -187,7 +191,9 @@ export default declare((api, opt: Options) => { ); path.replaceWith( - template.expression.ast`${id}.has(${path.node.right})`, + template.expression.ast`${id}.has(${state.addHelper( + "checkInRHS", + )}(${node.right}))`, ); } }, diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/accessor/output.js index b784b4ea02e5..99af0664387d 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/accessor/output.js @@ -7,7 +7,7 @@ class Foo { }); } test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } function _get_foo() {} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/compiled-classes/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/compiled-classes/output.js index d0f50c534427..56723c59b197 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/compiled-classes/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/compiled-classes/output.js @@ -13,12 +13,12 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test() { - return Object.prototype.hasOwnProperty.call(this, _bar); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _bar); } }], [{ key: "test", value: function test() { - return Object.prototype.hasOwnProperty.call(Foo, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(Foo), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/field/output.js index 352d2b534f23..d16e99fa240c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/field/output.js @@ -7,6 +7,6 @@ class Foo { }); } test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/method/output.js index 2e000bf1a9b0..90eb3d1a859f 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/method/output.js @@ -6,7 +6,7 @@ class Foo { }); } test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } function _foo2() {} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-other-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-other-redeclared/output.js index 6ded895fec68..4c9f46dd0ef5 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-other-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-other-redeclared/output.js @@ -21,11 +21,11 @@ class Foo { }); } test() { - Object.prototype.hasOwnProperty.call(this, _foo); - Object.prototype.hasOwnProperty.call(this, _bar2); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _bar2); } } - Object.prototype.hasOwnProperty.call(this, _foo); - Object.prototype.hasOwnProperty.call(this, _bar); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _bar); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-redeclared/output.js index d3073f234bf7..e066ef767fc0 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class-redeclared/output.js @@ -16,9 +16,9 @@ class Foo { }); } test() { - Object.prototype.hasOwnProperty.call(this, _foo2); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo2); } } - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class/output.js index dcf02e546ead..406473c5fb9c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/nested-class/output.js @@ -9,9 +9,9 @@ class Foo { test() { class Nested { test() { - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } } - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } } 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 new file mode 100644 index 000000000000..412b37f1946d --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/exec.js @@ -0,0 +1,27 @@ +expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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 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`); + +expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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() {}; 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`); + +expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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`); + +expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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/assumption-privateFieldsAsProperties/rhs-not-object/options.json b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/options.json new file mode 100644 index 000000000000..22b476c4bb57 --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/rhs-not-object/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "12.0.0" +} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-accessor/output.js index 11ec1676f116..de80926eea95 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-accessor/output.js @@ -1,7 +1,7 @@ var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo"); class Foo { test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } function _get_foo() {} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-field/output.js index 86970d674978..78c397d3f887 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-field/output.js @@ -1,7 +1,7 @@ var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo"); class Foo { test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } Object.defineProperty(Foo, _foo, { diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-method/output.js index 71829bd804c2..a94d7e2df94c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/assumption-privateFieldsAsProperties/static-method/output.js @@ -1,7 +1,7 @@ var _foo = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("foo"); class Foo { test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } } function _foo2() {} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/accessor/output.js index 853e6bd7062a..e77b8058492b 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/accessor/output.js @@ -12,7 +12,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/field/output.js index c2591821c193..f34fd44e153c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/field/output.js @@ -12,7 +12,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/method/output.js index 8f150b89393c..d8ff2d463c4a 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/method/output.js @@ -11,7 +11,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/native-classes/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/native-classes/output.js index bc0b5d930fbd..596332f31431 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/native-classes/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/native-classes/output.js @@ -7,10 +7,10 @@ class Foo { }); } static test() { - return Foo === Foo; + return babelHelpers.checkInRHS(Foo) === Foo; } test() { - return _bar.has(this); + return _bar.has(babelHelpers.checkInRHS(this)); } } var _foo = { diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-other-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-other-redeclared/output.js index 63c8a088a85b..7992e14ef759 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-other-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-other-redeclared/output.js @@ -29,14 +29,14 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - Object.prototype.hasOwnProperty.call(this, _foo); - Object.prototype.hasOwnProperty.call(this, _bar2); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _bar2); } }]); return Nested; }(); - Object.prototype.hasOwnProperty.call(this, _foo); - Object.prototype.hasOwnProperty.call(this, _bar); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _bar); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-redeclared/output.js index 17b292e242a9..da5cf04f3c0d 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class-redeclared/output.js @@ -24,12 +24,12 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - Object.prototype.hasOwnProperty.call(this, _foo2); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo2); } }]); return Nested; }(); - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class/output.js index af8cf8073cd4..ea0b82693e83 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/nested-class/output.js @@ -19,12 +19,12 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } }]); return Nested; }(); - Object.prototype.hasOwnProperty.call(this, _foo); + Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(this), _foo); } }]); return Foo; 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 new file mode 100644 index 000000000000..412b37f1946d --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/exec.js @@ -0,0 +1,27 @@ +expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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 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`); + +expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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() {}; 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`); + +expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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`); + +expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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/options.json b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/options.json new file mode 100644 index 000000000000..22b476c4bb57 --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/rhs-not-object/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "12.0.0" +} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-accessor/output.js index 4b3e093c8c94..c44e1f808ba8 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-accessor/output.js @@ -8,7 +8,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-field/output.js index be9098e07384..420281e8fc4f 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-field/output.js @@ -8,7 +8,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-method/output.js index 5ec524d065be..ca661fe7cdb3 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-method/output.js @@ -8,7 +8,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return Object.prototype.hasOwnProperty.call(other, _foo); + return Object.prototype.hasOwnProperty.call(babelHelpers.checkInRHS(other), _foo); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/output.js index 1ca900812373..ae4aa620ac05 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/output.js @@ -3,7 +3,7 @@ class Test { const _Test2 = 2; const func = () => { const _Test = 3; - return other === Test && _Test; + return babelHelpers.checkInRHS(other) === Test && _Test; }; return func() + _Test2; } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/accessor/output.js index 1a5a171e62ea..2ddcb76181b9 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/accessor/output.js @@ -12,7 +12,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return _foo.has(other); + return _foo.has(babelHelpers.checkInRHS(other)); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/field/output.js index 5c19f387b89f..a632933ac604 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/field/output.js @@ -12,7 +12,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return _foo.has(other); + return _foo.has(babelHelpers.checkInRHS(other)); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/method/output.js index 7d7f7a60aa0e..ff73b1d6e42c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/method/output.js @@ -9,7 +9,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return _foo.has(other); + return _foo.has(babelHelpers.checkInRHS(other)); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/native-classes/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/native-classes/output.js index bc0b5d930fbd..596332f31431 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/native-classes/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/native-classes/output.js @@ -7,10 +7,10 @@ class Foo { }); } static test() { - return Foo === Foo; + return babelHelpers.checkInRHS(Foo) === Foo; } test() { - return _bar.has(this); + return _bar.has(babelHelpers.checkInRHS(this)); } } var _foo = { diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-other-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-other-redeclared/output.js index a93260ef0c89..e67fc1769a46 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-other-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-other-redeclared/output.js @@ -29,14 +29,14 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - _foo.has(this); - _bar2.has(this); + _foo.has(babelHelpers.checkInRHS(this)); + _bar2.has(babelHelpers.checkInRHS(this)); } }]); return Nested; }(); - _foo.has(this); - _bar.has(this); + _foo.has(babelHelpers.checkInRHS(this)); + _bar.has(babelHelpers.checkInRHS(this)); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-redeclared/output.js index ff76f6033e13..faacad85de43 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class-redeclared/output.js @@ -24,12 +24,12 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - _foo2.has(this); + _foo2.has(babelHelpers.checkInRHS(this)); } }]); return Nested; }(); - _foo.has(this); + _foo.has(babelHelpers.checkInRHS(this)); } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class/output.js index e1eecdc3989f..d0bb08ff47f3 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/nested-class/output.js @@ -19,12 +19,12 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Nested, [{ key: "test", value: function test() { - _foo.has(this); + _foo.has(babelHelpers.checkInRHS(this)); } }]); return Nested; }(); - _foo.has(this); + _foo.has(babelHelpers.checkInRHS(this)); } }]); return Foo; 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 new file mode 100644 index 000000000000..412b37f1946d --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/exec.js @@ -0,0 +1,27 @@ +expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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 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`); + +expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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() {}; 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`); + +expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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`); + +expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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/options.json b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/options.json new file mode 100644 index 000000000000..22b476c4bb57 --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/rhs-not-object/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "12.0.0" +} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-accessor/output.js index 4cf51b3fe313..e1dddc3e9c71 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-accessor/output.js @@ -7,7 +7,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return other === Foo; + return babelHelpers.checkInRHS(other) === Foo; } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-field/output.js index d6c3295514d4..9c65a222b8b2 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-field/output.js @@ -7,7 +7,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return other === Foo; + return babelHelpers.checkInRHS(other) === Foo; } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-method/output.js index dfa898f00021..a373036cb199 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-method/output.js @@ -7,7 +7,7 @@ let Foo = /*#__PURE__*/function () { babelHelpers.createClass(Foo, [{ key: "test", value: function test(other) { - return other === Foo; + return babelHelpers.checkInRHS(other) === Foo; } }]); return Foo; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/output.js index 1ca900812373..ae4aa620ac05 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/output.js @@ -3,7 +3,7 @@ class Test { const _Test2 = 2; const func = () => { const _Test = 3; - return other === Test && _Test; + return babelHelpers.checkInRHS(other) === Test && _Test; }; return func() + _Test2; } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/accessor/output.js index 0ebe7e230972..1513b126cc8a 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/accessor/output.js @@ -5,6 +5,6 @@ class Foo { } get #foo() {} test(other) { - return _FooBrandCheck.has(other); + return _FooBrandCheck.has(babelHelpers.checkInRHS(other)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js index 47b5bf829a39..4512aecc3a6c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-in-default-param/output.js @@ -3,7 +3,7 @@ return _fooBrandCheck = /*#__PURE__*/new WeakSet(), class { #foo = void _fooBrandCheck.add(this); test(other) { - return _fooBrandCheck.has(other); + return _fooBrandCheck.has(babelHelpers.checkInRHS(other)); } }; })()) => {}; diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-instance/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-instance/output.js index 211a179a6097..dc8de3baec41 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-instance/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-instance/output.js @@ -3,7 +3,7 @@ function fn() { return new (_privBrandCheck = /*#__PURE__*/new WeakSet(), class { #priv = void _privBrandCheck.add(this); method(obj) { - return _privBrandCheck.has(obj); + return _privBrandCheck.has(babelHelpers.checkInRHS(obj)); } })(); } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-static/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-static/output.js index ab557072c40d..e14a04f0b1c6 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-static/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/class-expression-static/output.js @@ -3,7 +3,7 @@ function fn() { return new (_privBrandCheck = /*#__PURE__*/new WeakSet(), class { static #priv = void _privBrandCheck.add(this); method(obj) { - return _privBrandCheck.has(obj); + return _privBrandCheck.has(babelHelpers.checkInRHS(obj)); } })(); } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/field/output.js index 282aa9358f9b..449eea44e4b6 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/field/output.js @@ -2,6 +2,6 @@ var _fooBrandCheck = /*#__PURE__*/new WeakSet(); class Foo { #foo = (_fooBrandCheck.add(this), 1); test(other) { - return _fooBrandCheck.has(other); + return _fooBrandCheck.has(babelHelpers.checkInRHS(other)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-instance/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-instance/output.js index 1a8571bbc811..6f7596336600 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-instance/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-instance/output.js @@ -4,10 +4,10 @@ var _xBrandCheck = /*#__PURE__*/new WeakSet(); var _yBrandCheck = /*#__PURE__*/new WeakSet(); class F { m() { - _FBrandCheck.has(this); - _xBrandCheck.has(this); - _yBrandCheck.has(this); - _FBrandCheck.has(this); + _FBrandCheck.has(babelHelpers.checkInRHS(this)); + _xBrandCheck.has(babelHelpers.checkInRHS(this)); + _yBrandCheck.has(babelHelpers.checkInRHS(this)); + _FBrandCheck.has(babelHelpers.checkInRHS(this)); } get #w() {} #x = (_FBrandCheck.add(this), (_xBrandCheck.add(this), 0)); diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-static/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-static/output.js index c7d088022683..b754eb6f1441 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-static/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/half-constructed-static/output.js @@ -3,9 +3,9 @@ var _xBrandCheck = /*#__PURE__*/new WeakSet(); var _yBrandCheck = /*#__PURE__*/new WeakSet(); class F { static m() { - _xBrandCheck.has(this); - _yBrandCheck.has(this); - F === this; + _xBrandCheck.has(babelHelpers.checkInRHS(this)); + _yBrandCheck.has(babelHelpers.checkInRHS(this)); + F === babelHelpers.checkInRHS(this); } static #x = (_xBrandCheck.add(this), 0); static #y = (_temp = (() => { diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/method/output.js index 691a2c72d844..81c8571f0748 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/method/output.js @@ -5,6 +5,6 @@ class Foo { } #foo() {} test(other) { - return _FooBrandCheck.has(other); + return _FooBrandCheck.has(babelHelpers.checkInRHS(other)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/multiple-checks/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/multiple-checks/output.js index 902bef31fb0c..8c7f1a2a7577 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/multiple-checks/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/multiple-checks/output.js @@ -4,9 +4,9 @@ class A { #x = (_ABrandCheck.add(this), void _xBrandCheck.add(this)); #m() {} test() { - _xBrandCheck.has(this); - _ABrandCheck.has(this); - _xBrandCheck.has(this); - _ABrandCheck.has(this); + _xBrandCheck.has(babelHelpers.checkInRHS(this)); + _ABrandCheck.has(babelHelpers.checkInRHS(this)); + _xBrandCheck.has(babelHelpers.checkInRHS(this)); + _ABrandCheck.has(babelHelpers.checkInRHS(this)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-other-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-other-redeclared/output.js index 0ca6c142536e..fa17e8b056ad 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-other-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-other-redeclared/output.js @@ -8,11 +8,11 @@ class Foo { class Nested { #bar = (_barBrandCheck.add(this), 2); test() { - _fooBrandCheck.has(this); - _barBrandCheck.has(this); + _fooBrandCheck.has(babelHelpers.checkInRHS(this)); + _barBrandCheck.has(babelHelpers.checkInRHS(this)); } } - _fooBrandCheck.has(this); - _barBrandCheck2.has(this); + _fooBrandCheck.has(babelHelpers.checkInRHS(this)); + _barBrandCheck2.has(babelHelpers.checkInRHS(this)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-redeclared/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-redeclared/output.js index 13789de0d07c..ca46a4626a2c 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-redeclared/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class-redeclared/output.js @@ -6,9 +6,9 @@ class Foo { class Nested { #foo = (_fooBrandCheck.add(this), 2); test() { - _fooBrandCheck.has(this); + _fooBrandCheck.has(babelHelpers.checkInRHS(this)); } } - _fooBrandCheck2.has(this); + _fooBrandCheck2.has(babelHelpers.checkInRHS(this)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class/output.js index 276ad2b3dc91..7b10bc175396 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/nested-class/output.js @@ -4,9 +4,9 @@ class Foo { test() { class Nested { test() { - _fooBrandCheck.has(this); + _fooBrandCheck.has(babelHelpers.checkInRHS(this)); } } - _fooBrandCheck.has(this); + _fooBrandCheck.has(babelHelpers.checkInRHS(this)); } } 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 new file mode 100644 index 000000000000..412b37f1946d --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/exec.js @@ -0,0 +1,27 @@ +expect(() => class { static #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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 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`); + +expect(() => class { static #p() {}; static q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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() {}; 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`); + +expect(() => new class { #p = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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`); + +expect(() => new class { #p() {}; q = #p in 0 }).toThrowError(`right-hand side of 'in' should be an object, got number`); +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(() => 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/options.json b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/options.json new file mode 100644 index 000000000000..1655c55f42cc --- /dev/null +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/rhs-not-object/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "14.0.0" +} diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-accessor/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-accessor/output.js index 4523533658f3..3dda06a5c5b1 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-accessor/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-accessor/output.js @@ -1,6 +1,6 @@ class Foo { static get #foo() {} test(other) { - return Foo === other; + return Foo === babelHelpers.checkInRHS(other); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-field/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-field/output.js index 3951619cf7f4..2a8c5cbd5242 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-field/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-field/output.js @@ -2,6 +2,6 @@ var _fooBrandCheck = /*#__PURE__*/new WeakSet(); class Foo { static #foo = (_fooBrandCheck.add(this), 1); test(other) { - return _fooBrandCheck.has(other); + return _fooBrandCheck.has(babelHelpers.checkInRHS(other)); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-method/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-method/output.js index b2036b980999..97ce37327d2b 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-method/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-method/output.js @@ -1,6 +1,6 @@ class Foo { static #foo() {} test(other) { - return Foo === other; + return Foo === babelHelpers.checkInRHS(other); } } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/output.js index 1ca900812373..ae4aa620ac05 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/output.js @@ -3,7 +3,7 @@ class Test { const _Test2 = 2; const func = () => { const _Test = 3; - return other === Test && _Test; + return babelHelpers.checkInRHS(other) === Test && _Test; }; return func() + _Test2; } diff --git a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadowed-binding/output.js b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadowed-binding/output.js index 8ef264ad2df3..6aef183b2b97 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadowed-binding/output.js +++ b/packages/babel-plugin-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadowed-binding/output.js @@ -3,7 +3,7 @@ class A { static #foo = void _fooBrandCheck.add(this); test() { let A = function fn(A) { - return _fooBrandCheck.has(A); + return _fooBrandCheck.has(babelHelpers.checkInRHS(A)); }; } } diff --git a/packages/babel-runtime-corejs2/package.json b/packages/babel-runtime-corejs2/package.json index 1e9b28e41640..1125f427adcb 100644 --- a/packages/babel-runtime-corejs2/package.json +++ b/packages/babel-runtime-corejs2/package.json @@ -81,6 +81,15 @@ "./helpers/awaitAsyncGenerator.js" ], "./helpers/esm/awaitAsyncGenerator": "./helpers/esm/awaitAsyncGenerator.js", + "./helpers/checkInRHS": [ + { + "node": "./helpers/checkInRHS.js", + "import": "./helpers/esm/checkInRHS.js", + "default": "./helpers/checkInRHS.js" + }, + "./helpers/checkInRHS.js" + ], + "./helpers/esm/checkInRHS": "./helpers/esm/checkInRHS.js", "./helpers/jsx": [ { "node": "./helpers/jsx.js", diff --git a/packages/babel-runtime-corejs3/package.json b/packages/babel-runtime-corejs3/package.json index 1f396fb8f796..53dc26f87b45 100644 --- a/packages/babel-runtime-corejs3/package.json +++ b/packages/babel-runtime-corejs3/package.json @@ -80,6 +80,15 @@ "./helpers/awaitAsyncGenerator.js" ], "./helpers/esm/awaitAsyncGenerator": "./helpers/esm/awaitAsyncGenerator.js", + "./helpers/checkInRHS": [ + { + "node": "./helpers/checkInRHS.js", + "import": "./helpers/esm/checkInRHS.js", + "default": "./helpers/checkInRHS.js" + }, + "./helpers/checkInRHS.js" + ], + "./helpers/esm/checkInRHS": "./helpers/esm/checkInRHS.js", "./helpers/jsx": [ { "node": "./helpers/jsx.js", diff --git a/packages/babel-runtime/package.json b/packages/babel-runtime/package.json index 0b2226d1d004..c98104256edb 100644 --- a/packages/babel-runtime/package.json +++ b/packages/babel-runtime/package.json @@ -80,6 +80,15 @@ "./helpers/awaitAsyncGenerator.js" ], "./helpers/esm/awaitAsyncGenerator": "./helpers/esm/awaitAsyncGenerator.js", + "./helpers/checkInRHS": [ + { + "node": "./helpers/checkInRHS.js", + "import": "./helpers/esm/checkInRHS.js", + "default": "./helpers/checkInRHS.js" + }, + "./helpers/checkInRHS.js" + ], + "./helpers/esm/checkInRHS": "./helpers/esm/checkInRHS.js", "./helpers/jsx": [ { "node": "./helpers/jsx.js",