Skip to content

Commit

Permalink
Correctly update bindings of decorated class declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 27, 2022
1 parent 40a6d42 commit e41a386
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Expand Up @@ -325,6 +325,14 @@ const visitor: Visitor<PluginPass> = {
const replacement = decoratedClassToExpression(path);
if (replacement) {
path.replaceWith(replacement);

const decl = path.get("declarations.0");
const { id } = decl.node;

// 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
}
}]
]
}
@@ -0,0 +1,22 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = wrap;

var _coreDecorators = require("core-decorators");

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; }

function wrap() {
return function () {
var _class;

let Foo = (_class = class Foo {
method() {}

}, (_applyDecoratedDescriptor(_class.prototype, "method", [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, "method"), _class.prototype)), _class);
return Foo;
};
}

0 comments on commit e41a386

Please sign in to comment.