-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Setting a getter with Object.defineProperty not working within decorator #2127
Comments
I tried fixing this, and it seems like babel emits the same result. input.js const returnValue = "asdasd"
class TestClass2 {
@deco testProperty;
}
function deco(target, key) {
Object.defineProperty(target.constructor.prototype, key, {
get: function () { return returnValue },
enumerable: true,
configurable: true,
});
}
const instance = new TestClass2()
console.log(instance.testProperty)
{
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-class-properties"
]
} Command:
var _class, _descriptor;
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(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.defineProperty(target, property, desc); desc = null; } return desc; }
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and runs after the decorators transform.'); }
const returnValue = "asdasd";
let TestClass2 = (_class = class TestClass2 {
constructor() {
_initializerDefineProperty(this, "testProperty", _descriptor, this);
}
}, (_descriptor = _applyDecoratedDescriptor(_class.prototype, "testProperty", [deco], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
})), _class);
function deco(target, key) {
Object.defineProperty(target.constructor.prototype, key, {
get: function () {
return returnValue;
},
enumerable: true,
configurable: true
});
}
const instance = new TestClass2();
console.log(instance.testProperty); Hmm... |
I investigated further. const a = {
get foo() {
return 'foo'
},
bar: 'bar'
};
Object.defineProperty(a, 'foo', {
configurable: true,
enumerable: true,
writable: true
})
Object.defineProperty(a, 'bar', {
configurable: true,
enumerable: true,
writable: true
})
console.log(a) This prints |
swc_ecma_transforms_base: - Fix `_applyDecoratedDescriptor`. (Closes #2127)
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
If I set a getter for a property with
Object.defineProperty
within a decorator it returnsundefined
instead of the variableInput code
Config
Expected behavior
All tests should not fail
expect(instance.testProperty).toBe(returnValue)
should returntrue
it works, when changing the
transformer
tots-jest
oresbuild-runner/jest
oresbuild-jest
Version
Additional context
The same problem applies to
@swc-node/jest
.This issue most likely prevents users from using the known library
typescript-ioc
: https://github.com/thiagobustamante/typescript-iocthiagobustamante/typescript-ioc#68
thiagobustamante/typescript-ioc#77
The text was updated successfully, but these errors were encountered: