You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
which sets desc.writable true in the following condition: if ('value' in desc || desc.initializer) { desc.writable = true; }
I don't understand this part. Should this be changed somehow?
Context
This behavior occurs only when both of the following conditions are met:
A decorator is present on the class property.
No initializer is defined in the class property declaration.
For example, if there's no decorator:
classTest{a;}
a is writable.
Transpiled code:
functionDec(target,key,descriptor){// do nothingreturndescriptor;}classTest{constructor(){this.a=void0;}}lett=newTest();console.log(t.a);// prints 'undefined'console.log(Object.getOwnPropertyDescriptor(t,'a'));// writable: false (should be true)t.a=2;console.log(t.a);// prints 'undefined' (should be 2)
Also, if I set an initializer:
classTest{
@Deca=1;}
a is writable.
Transpiled code:
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) : void0});}function_applyDecoratedDescriptor(target,property,decorators,descriptor,context){vardesc={};Object['ke'+'ys'](descriptor).forEach(function(key){desc[key]=descriptor[key];});desc.enumerable=!!desc.enumerable;desc.configurable=!!desc.configurable;if('value'indesc||desc.initializer){desc.writable=true;}desc=decorators.slice().reverse().reduce(function(desc,decorator){returndecorator(target,property,desc)||desc;},desc);if(context&&desc.initializer!==void0){desc.value=desc.initializer ? desc.initializer.call(context) : void0;desc.initializer=undefined;}if(desc.initializer===void0){Object['define'+'Property'](target,property,desc);desc=null;}returndesc;}function_initializerWarningHelper(descriptor,context){thrownewError('Decorating class property failed. Please ensure that '+'proposal-class-properties is enabled and set to use loose mode. '+'To use proposal-class-properties in spec mode with decorators, wait for '+'the next major version of decorators in stage 2.');}functionDec(target,key,descriptor){// do nothingreturndescriptor;}letTest=(_class=classTest{constructor(){_initializerDefineProperty(this,"a",_descriptor,this);}},(_descriptor=_applyDecoratedDescriptor(_class.prototype,"a",[Dec],{enumerable: true,initializer: function(){return1;}})),_class);lett=newTest();console.log(t.a);// prints 'undefined'console.log(Object.getOwnPropertyDescriptor(t,'a'));// writable: false (should be true)t.a=2;console.log(t.a);// prints 'undefined' (should be 2)
Hey @yhpark! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.
If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.
yhpark
changed the title
Class property is non-writable in a specific setting
Class property is non-writable when used with decorator without initializer
Feb 18, 2018
Choose one: is this a bug report or feature request? bug report
Input Code
Babel/Babylon Configuration (.babelrc, package.json, cli command)
Expected Behavior
The property
a
should bewritable
.Current Behavior
writable
ina
's descriptor is set to false.Possible Solution
Currently Babel transpiles the code with
_applyDecoratedDescriptor
function declaration as following:which sets
desc.writable
true in the following condition:if ('value' in desc || desc.initializer) { desc.writable = true; }
I don't understand this part. Should this be changed somehow?
Context
This behavior occurs only when both of the following conditions are met:
For example, if there's no decorator:
a
is writable.Transpiled code:
Also, if I set an initializer:
a
is writable.Transpiled code:
Your Environment
The text was updated successfully, but these errors were encountered: