Skip to content

Commit

Permalink
checkInRHS helper compat
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 8, 2022
1 parent 5d9dccc commit 5fef810
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
39 changes: 30 additions & 9 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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,
)})`,
);
},
});
Expand Down
Expand Up @@ -8,14 +8,15 @@ import {
buildPrivateNamesMap,
transformPrivateNamesUsage,
buildFieldsInitNodes,
buildCheckInRHS,
} from "./fields";
import type { PropPath } from "./fields";
import { buildDecoratedClass, hasDecorators } from "./decorators";
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 };

Expand Down
Expand Up @@ -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";
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
)})`,
);
}
},
Expand Down

0 comments on commit 5fef810

Please sign in to comment.