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

Correctly update bindings of decorated class declarations #8566

Merged
merged 4 commits into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
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,12 @@
{
"plugins": [
["proposal-decorators", { "legacy": true }]
],
"presets": [
["env", {
"targets": {
"node": 8
}
}]
]
}
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
@@ -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;
};
}