From e0685ad534039d714980a84dcba9acdfffd5d1a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 27 Sep 2022 16:11:46 +0200 Subject: [PATCH] Correctly update bindings of decorated class declarations (#8566) --- .../src/transformer-legacy.ts | 10 +++++++++- .../fixtures/legacy-regression/8559/input.mjs | 12 +++++++++++ .../legacy-regression/8559/options.json | 7 +++++++ .../fixtures/legacy-regression/8559/output.js | 20 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/input.mjs create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/options.json create mode 100644 packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/output.js diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-legacy.ts b/packages/babel-plugin-proposal-decorators/src/transformer-legacy.ts index 586793653290..16b59ecf6fd1 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-legacy.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-legacy.ts @@ -324,7 +324,15 @@ const visitor: Visitor = { ClassDeclaration(path) { const replacement = decoratedClassToExpression(path); if (replacement) { - path.replaceWith(replacement); + const [newPath] = path.replaceWith(replacement); + + const decl = newPath.get("declarations.0"); + const id = decl.node.id as t.Identifier; + + // TODO: Maybe add this logic to @babel/traverse + const binding = path.scope.getOwnBinding(id.name); + binding.identifier = id; + binding.path = decl; } }, ClassExpression(path, state) { diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/input.mjs new file mode 100644 index 000000000000..e0c390921046 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/input.mjs @@ -0,0 +1,12 @@ +import {autobind} from 'core-decorators'; + +export default function wrap() { + return function() { + class Foo { + @autobind + method() {} + } + + return Foo; + }; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/options.json new file mode 100644 index 000000000000..9e9a5144bebf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/options.json @@ -0,0 +1,7 @@ +{ + "targets": { "node": 8 }, + "plugins": [ + ["proposal-decorators", { "version": "legacy" }] + ], + "presets": ["env"] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/output.js new file mode 100644 index 000000000000..11140092ad3e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/8559/output.js @@ -0,0 +1,20 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = wrap; + +var _coreDecorators = require("core-decorators"); + +function wrap() { + return function () { + var _class; + + let Foo = (_class = class Foo { + method() {} + + }, (babelHelpers.applyDecoratedDescriptor(_class.prototype, "method", [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, "method"), _class.prototype)), _class); + return Foo; + }; +}