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 class fields when super() is in a default param #12729

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,6 @@
class Foo extends Bar {
bar = "foo";

constructor(x = test ? super() : 0) {
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"proposal-class-properties"
]
}
@@ -0,0 +1,8 @@
class Foo extends Bar {
constructor(x = test ? (() => {
var _temp;

return _temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp;
})() : 0) {}

}
@@ -0,0 +1,6 @@
class Foo extends Bar {
bar = "foo";

constructor(x = () => { check(super()) }) {
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"proposal-class-properties"
]
}
@@ -0,0 +1,8 @@
class Foo extends Bar {
constructor(x = () => {
var _temp;

check((_temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp));
}) {}

}
@@ -0,0 +1,6 @@
class Foo extends Bar {
bar = "foo";

constructor(x = super()) {
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"proposal-class-properties"
]
}
@@ -0,0 +1,8 @@
class Foo extends Bar {
constructor(x = (() => {
var _temp;

return _temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp;
})()) {}

}
17 changes: 16 additions & 1 deletion packages/babel-traverse/src/path/modification.ts
Expand Up @@ -95,7 +95,10 @@ export function _containerInsertAfter(this: NodePath, nodes) {
* expression, ensure that the completion record is correct by pushing the current node.
*/

export function insertAfter(this: NodePath, nodes_: t.Node | t.Node[]) {
export function insertAfter(
this: NodePath,
nodes_: t.Node | t.Node[],
): NodePath[] {
this._assertUnremoved();

const nodes = this._verifyNodeList(nodes_);
Expand Down Expand Up @@ -127,6 +130,18 @@ export function insertAfter(this: NodePath, nodes_: t.Node | t.Node[]) {
if (this.node) {
const node = this.node as t.Expression | t.VariableDeclaration;
let { scope } = this;

if (scope.path.isPattern()) {
this.replaceWith(
t.callExpression(
t.arrowFunctionExpression([], this.node as t.Expression),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use invariant instead if node will never be t.VariableDeclaration when scope.path is pattern.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I just realize that it is TypeScript! so we can use microsoft/TypeScript#32695 instead.

[],
),
);
(this.get("callee.body") as NodePath<t.Expression>).insertAfter(nodes);
return [this];
}

// Inserting after the computed key of a method should insert the
// temporary binding in the method's parent's scope.
if (parentPath.isMethod({ computed: true, key: node })) {
Expand Down