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 2 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 @@ -890,6 +890,10 @@ type ReplaceThisState = {
const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
{
ThisExpression(path, state) {
if (t.isUnaryExpression(path.parent, { operator: "delete" })) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path.parent could be a transparent expression wrapper.

For example,

// typescript
class Foo {
  x = delete this as Foo
}

We can use findParent until we see the first ancestry that is not such wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)

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);