Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor class transform cleanups #15700

Merged
merged 2 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 12 additions & 16 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -10,7 +10,7 @@ import type {
} from "@babel/helper-member-expression-to-functions";
import optimiseCall from "@babel/helper-optimise-call-expression";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import { isTransparentExprWrapper } from "@babel/helper-skip-transparent-expression-wrappers";
import { skipTransparentExprWrapperNodes } from "@babel/helper-skip-transparent-expression-wrappers";

import * as ts from "./typescript";

Expand Down Expand Up @@ -956,29 +956,25 @@ type ReplaceThisState = {

const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
{
ThisExpression(path, state) {
UnaryExpression(path) {
// Replace `delete this` with `true`
const parent = path.findParent(
path => !isTransparentExprWrapper(path.node),
);
if (t.isUnaryExpression(parent.node, { operator: "delete" })) {
path.parentPath.replaceWith(t.booleanLiteral(true));
return;
const { node } = path;
if (node.operator === "delete") {
const argument = skipTransparentExprWrapperNodes(node.argument);
if (t.isThisExpression(argument)) {
path.replaceWith(t.booleanLiteral(true));
}
}

},
ThisExpression(path, state) {
state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.classRef));
},
MetaProperty(path) {
const meta = path.get("meta");
const property = path.get("property");
const { scope } = path;
const { node, scope } = path;
// if there are `new.target` in static field
// we should replace it with `undefined`
if (
meta.isIdentifier({ name: "new" }) &&
property.isIdentifier({ name: "target" })
) {
if (node.meta.name === "new" && node.property.name === "target") {
path.replaceWith(scope.buildUndefinedNode());
}
},
Expand Down
22 changes: 8 additions & 14 deletions packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -163,17 +163,13 @@ export default function transformClass(
}

/**
* Creates a class constructor or bail out if there is none
* Creates a class constructor or bail out if there is one
*/
function maybeCreateConstructor() {
let hasConstructor = false;
const paths = classState.path.get("body.body");
for (const path of paths) {
// @ts-expect-error: StaticBlock does not have `kind` property
hasConstructor = path.equals("kind", "constructor");
if (hasConstructor) break;
const classBodyPath = classState.path.get("body");
for (const path of classBodyPath.get("body")) {
if (path.isClassMethod({ kind: "constructor" })) return;
}
if (hasConstructor) return;

let params: t.FunctionExpression["params"], body;

Expand All @@ -190,12 +186,10 @@ export default function transformClass(
body = t.blockStatement([]);
}

classState.path
.get("body")
.unshiftContainer(
"body",
t.classMethod("constructor", t.identifier("constructor"), params, body),
);
classBodyPath.unshiftContainer(
"body",
t.classMethod("constructor", t.identifier("constructor"), params, body),
);
}

function buildBody() {
Expand Down