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
Fix default descriptor setting for class properties with decorators #7429
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8268/ |
@@ -172,10 +172,18 @@ export default function() { | |||
decorators.map(dec => t.cloneNode(dec.expression)), | |||
), | |||
t.objectExpression([ | |||
t.objectProperty( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd suspect this change touch some output.js
test file, but this PR has no such changes, do you know why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply. There are only 4 fixture-based tests in this package and one of them is affected by this change. I already included the output.js
change in this commit (https://github.com/babel/babel/pull/7429/files#diff-798ac9fdf8c8cc407676867ecb8cf1b3). Since there's no plugin depending on this package, no tests outside this package would be affected by this change.
Can someone please review this PR? This issue makes class property decorators practically unusable. |
I've also run in to this bug and second @yhpark in that it makes class decorators unusable. I'd be willing to help resolve the existing conflicts if someone is interested in reviewing. |
fe625bd
to
7b54f1a
Compare
@Andarist (cc. @nicolo-ribaudo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Probably low priority since we are close to release the new decorators implementation)
Hi guys, does anyone know whether there is a way to use property decorators today? As far as I can see, the change proposed in this issue hasn't been integrated into the master. Is a new implementation of decorators available? |
@eduard-malakhov You need to assign an initial value to the property like this:
|
@marano I've tried assigning a default value, but this still results in a property that is |
Merged. Sorry if it took so long; the fix will be released in Babel 7. |
@nicolo-ribaudo Thank you! I am afraid this patch alone is not enough to make decorators work properly in all cases. I've been playing with this sample project which makes use of inversify-inject-decorators and to make it function properly besides the changes in property descriptor I needed to explicitly When the object is created, it already contains an own property and therefore the property created by the decorator on the prototype is not accessible unless one deletes the object's property. I am not sure, where this property comes from and whether this is a new behavior because if it has always been this way then it is not clear how inversify-inject-decorators and similar decorators could work in principle. |
The problem is that Babel doesn't allow changing the placement of a field (the decorators you use changes it from being defined on the instance to being defined on the prototype). If anyone wants to open a PR (legacy spec: https://github.com/wycats/javascript-decorators) I will be happy to review it. |
As described in #7391, class properties with decorators are given a descriptor whose
writable
value is false by default, which created an issue that the value of a class property with decorators is fixed as undefined value when it doesn't have an initializer. This patch changes the behavior to be consistent with the class properties without decorators as handled byproposal-class-properties
plugin, where a class property gets a base descriptor withconfigurable: true, enumerable: true, writable: true
(https://github.com/babel/babel/blob/master/packages/babel-plugin-proposal-class-properties/src/index.js#L51-L53).I also made a small change in
applyDecoratedDescriptor
helper that determinesdesc.writable
value, sincedesc.writable
is always set to either true or false after the above change is made.