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

fix: delete this in static class properties initialization #15312

Merged
merged 3 commits into from Dec 30, 2022
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
Expand Up @@ -24,6 +24,7 @@
"@babel/helper-member-expression-to-functions": "workspace:^",
"@babel/helper-optimise-call-expression": "workspace:^",
"@babel/helper-replace-supers": "workspace:^",
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^",
"@babel/helper-split-export-declaration": "workspace:^"
},
"peerDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -10,6 +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 * as ts from "./typescript";

Expand Down Expand Up @@ -890,6 +891,15 @@ type ReplaceThisState = {
const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
{
ThisExpression(path, state) {
// 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;
}

state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.classRef));
},
Expand Down
@@ -0,0 +1,7 @@
class Foo {
x = delete this;
static x = delete this;
}

expect(new Foo().x).toBe(true);
expect(Foo.x).toBe(true);
@@ -0,0 +1,4 @@
class Foo {
x = delete this;
static x = delete this;
}
@@ -0,0 +1,7 @@
var Foo = /*#__PURE__*/babelHelpers.createClass(function Foo() {
"use strict";

babelHelpers.classCallCheck(this, Foo);
babelHelpers.defineProperty(this, "x", delete this);
});
babelHelpers.defineProperty(Foo, "x", true);
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -591,6 +591,7 @@ __metadata:
"@babel/helper-optimise-call-expression": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-replace-supers": "workspace:^"
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^"
"@babel/helper-split-export-declaration": "workspace:^"
"@babel/plugin-syntax-class-static-block": ^7.14.5
"@babel/preset-env": "workspace:^"
Expand Down