From bab5c62f907b6f90d094531ab22aa0a86d7c35a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 2 Feb 2021 15:38:07 +0100 Subject: [PATCH] Fix class fields when `super()` is in a default param (#12729) * Fix class fields when `super()` is in a default param * Use assertion instead of type cast --- .../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 ++++++ .../derived-super-in-default-params/options.json | 6 ++++++ .../derived-super-in-default-params/output.js | 8 ++++++++ packages/babel-traverse/src/path/modification.ts | 16 +++++++++++++++- 10 files changed, 75 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..88ffe614f610 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,17 @@ 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()) { + t.assertExpression(node); + + this.replaceWith( + t.callExpression(t.arrowFunctionExpression([], node), []), + ); + (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 })) {