Skip to content

Commit

Permalink
Correctly update bindings of decorated class declarations (#8566)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 27, 2022
1 parent bf3b20a commit e0685ad
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Expand Up @@ -324,7 +324,15 @@ const visitor: Visitor<PluginPass> = {
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) {
Expand Down
@@ -0,0 +1,12 @@
import {autobind} from 'core-decorators';

export default function wrap() {
return function() {
class Foo {
@autobind
method() {}
}

return Foo;
};
}
@@ -0,0 +1,7 @@
{
"targets": { "node": 8 },
"plugins": [
["proposal-decorators", { "version": "legacy" }]
],
"presets": ["env"]
}
@@ -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;
};
}

0 comments on commit e0685ad

Please sign in to comment.