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 790a941ca913..39885e15b2c6 100644 --- a/packages/babel-helper-create-class-features-plugin/src/fields.ts +++ b/packages/babel-helper-create-class-features-plugin/src/fields.ts @@ -206,6 +206,24 @@ function unshadow( } } +export function buildCheckInRHS( + rhs: t.Expression, + file: File, + inRHSIsObject?: boolean, +) { + let helperRef; + try { + helperRef = file.addHelper("checkInRHS"); + } catch (err) { + if (err.code !== "BABEL_HELPER_UNKNOWN") { + throw err; + } + } + return helperRef !== undefined && inRHSIsObject !== true + ? t.callExpression(helperRef, [rhs]) + : rhs; +} + const privateInVisitor = privateNameVisitorFactory<{ classRef: t.Identifier; file: File; @@ -230,9 +248,10 @@ const privateInVisitor = privateNameVisitorFactory<{ if (privateFieldsAsProperties) { const { id } = privateNamesMap.get(name); path.replaceWith(template.expression.ast` - Object.prototype.hasOwnProperty.call(${file.addHelper( - "checkInRHS", - )}(${right}), ${t.cloneNode(id)}) + Object.prototype.hasOwnProperty.call(${buildCheckInRHS( + right, + file, + )}, ${t.cloneNode(id)}) `); return; } @@ -241,17 +260,19 @@ const privateInVisitor = privateNameVisitorFactory<{ if (isStatic) { path.replaceWith( - template.expression.ast`${file.addHelper( - "checkInRHS", - )}(${right}) === ${t.cloneNode(this.classRef)}`, + template.expression.ast`${buildCheckInRHS( + right, + file, + )} === ${t.cloneNode(this.classRef)}`, ); return; } path.replaceWith( - template.expression.ast`${t.cloneNode(id)}.has(${file.addHelper( - "checkInRHS", - )}(${right}))`, + template.expression.ast`${t.cloneNode(id)}.has(${buildCheckInRHS( + right, + file, + )})`, ); }, }); diff --git a/packages/babel-helper-create-class-features-plugin/src/index.ts b/packages/babel-helper-create-class-features-plugin/src/index.ts index 6705416de160..4a655f30ff1a 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.ts +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -8,6 +8,7 @@ import { buildPrivateNamesMap, transformPrivateNamesUsage, buildFieldsInitNodes, + buildCheckInRHS, } from "./fields"; import type { PropPath } from "./fields"; import { buildDecoratedClass, hasDecorators } from "./decorators"; @@ -15,7 +16,7 @@ import { injectInitialization, extractComputedKeys } from "./misc"; import { enableFeature, FEATURES, isLoose, shouldTransform } from "./features"; import { assertFieldTransformed } from "./typescript"; -export { FEATURES, enableFeature, injectInitialization }; +export { FEATURES, enableFeature, injectInitialization, buildCheckInRHS }; declare const PACKAGE_JSON: { name: string; version: string }; diff --git a/packages/babel-plugin-proposal-private-property-in-object/src/index.ts b/packages/babel-plugin-proposal-private-property-in-object/src/index.ts index 0266b990e4e8..91ff58cd94a7 100644 --- a/packages/babel-plugin-proposal-private-property-in-object/src/index.ts +++ b/packages/babel-plugin-proposal-private-property-in-object/src/index.ts @@ -4,6 +4,7 @@ import { enableFeature, FEATURES, injectInitialization as injectConstructorInit, + buildCheckInRHS, } from "@babel/helper-create-class-features-plugin"; import annotateAsPure from "@babel/helper-annotate-as-pure"; import type * as t from "@babel/types"; @@ -121,6 +122,7 @@ export default declare((api, opt: Options) => { visitor: { BinaryExpression(path, state) { const { node } = path; + const { file } = state; if (node.operator !== "in") return; if (!t.isPrivateName(node.left)) return; @@ -158,9 +160,10 @@ export default declare((api, opt: Options) => { } path.replaceWith( template.expression.ast` - ${t.cloneNode(outerClass.node.id)} === ${state.addHelper( - "checkInRHS", - )}(${node.right}) + ${t.cloneNode(outerClass.node.id)} === ${buildCheckInRHS( + node.right, + file, + )} `, ); } else { @@ -173,9 +176,10 @@ export default declare((api, opt: Options) => { ); path.replaceWith( - template.expression.ast`${id}.has(${state.addHelper( - "checkInRHS", - )}(${node.right}))`, + template.expression.ast`${id}.has(${buildCheckInRHS( + node.right, + file, + )})`, ); } } else { @@ -191,9 +195,10 @@ export default declare((api, opt: Options) => { ); path.replaceWith( - template.expression.ast`${id}.has(${state.addHelper( - "checkInRHS", - )}(${node.right}))`, + template.expression.ast`${id}.has(${buildCheckInRHS( + node.right, + file, + )})`, ); } },