From 2bd50847fb223052d6faaf88ae189c89cefd0810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 31 Jan 2021 22:57:53 +0100 Subject: [PATCH 1/2] Fix class fields when `super()` is in a default param --- .../input.js | 6 ++++++ .../options.json | 6 ++++++ .../output.js | 8 ++++++++ .../input.js | 6 ++++++ .../options.json | 6 ++++++ .../output.js | 8 ++++++++ .../derived-super-in-default-params/input.js | 6 ++++++ .../options.json | 6 ++++++ .../derived-super-in-default-params/output.js | 8 ++++++++ .../babel-traverse/src/path/modification.ts | 17 ++++++++++++++++- 10 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/input.js new file mode 100644 index 000000000000..b22f258c391d --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + bar = "foo"; + + constructor(x = test ? super() : 0) { + } +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/options.json new file mode 100644 index 000000000000..0f6348c0ad45 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + ["external-helpers", { "helperVersion": "7.100.0" }], + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/output.js new file mode 100644 index 000000000000..1d4f378afdd5 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-complex/output.js @@ -0,0 +1,8 @@ +class Foo extends Bar { + constructor(x = test ? (() => { + var _temp; + + return _temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp; + })() : 0) {} + +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/input.js new file mode 100644 index 000000000000..8d12a91f22c5 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + bar = "foo"; + + constructor(x = () => { check(super()) }) { + } +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/options.json new file mode 100644 index 000000000000..0f6348c0ad45 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + ["external-helpers", { "helperVersion": "7.100.0" }], + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/output.js new file mode 100644 index 000000000000..db628b9500cc --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params-in-arrow/output.js @@ -0,0 +1,8 @@ +class Foo extends Bar { + constructor(x = () => { + var _temp; + + check((_temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp)); + }) {} + +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/input.js new file mode 100644 index 000000000000..dcec264d35d5 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/input.js @@ -0,0 +1,6 @@ +class Foo extends Bar { + bar = "foo"; + + constructor(x = super()) { + } +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/options.json new file mode 100644 index 000000000000..0f6348c0ad45 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + ["external-helpers", { "helperVersion": "7.100.0" }], + "proposal-class-properties" + ] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/output.js new file mode 100644 index 000000000000..bcc2f94334ef --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived-super-in-default-params/output.js @@ -0,0 +1,8 @@ +class Foo extends Bar { + constructor(x = (() => { + var _temp; + + return _temp = super(), babelHelpers.defineProperty(this, "bar", "foo"), _temp; + })()) {} + +} diff --git a/packages/babel-traverse/src/path/modification.ts b/packages/babel-traverse/src/path/modification.ts index fc4ac0dd4b93..3b0ffff60213 100644 --- a/packages/babel-traverse/src/path/modification.ts +++ b/packages/babel-traverse/src/path/modification.ts @@ -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_); @@ -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), + [], + ), + ); + (this.get("callee.body") as NodePath).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 })) { From bea63789a05c96f4e1b34e5e992a7c3e0d60eb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 1 Feb 2021 20:12:12 +0100 Subject: [PATCH 2/2] Use assertion instead of type cast --- packages/babel-traverse/src/path/modification.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/babel-traverse/src/path/modification.ts b/packages/babel-traverse/src/path/modification.ts index 3b0ffff60213..88ffe614f610 100644 --- a/packages/babel-traverse/src/path/modification.ts +++ b/packages/babel-traverse/src/path/modification.ts @@ -132,13 +132,12 @@ export function insertAfter( let { scope } = this; if (scope.path.isPattern()) { + t.assertExpression(node); + this.replaceWith( - t.callExpression( - t.arrowFunctionExpression([], this.node as t.Expression), - [], - ), + t.callExpression(t.arrowFunctionExpression([], node), []), ); - (this.get("callee.body") as NodePath).insertAfter(nodes); + (this.get("callee.body") as NodePath).insertAfter(nodes); return [this]; }