Closed osi-oswald closed 6 years ago
Just figured, buildInitializerDefineProperty
could be changed to this:
const buildInitializerDefineProperty = template(`
function NAME(target, property, descriptor, context){
if (!descriptor || !descriptor.initializer) return;
context[property] = descriptor.initializer.call(context);
}
`);
That way a setter defined on the prototype has a chance to react to the initialization assignment, whereas using Object.defineProperty
will just skip the setter on the prototype :-/
If I take a closer look at babel-plugin-transform-class-properties, I can see it has an option spec
, which by default is false. If option spec
is false (default), an assignment is emitted. And only of if spec
is true, it will use Object.defineProperty
to initialize.
Therefore: The default should be what I proposed in my last comment. And if one would be fancy, either read somehow the spec
option from transform-class-properties
or provide an own spec
option to still emit Object.defineProperty
for initialization.
spec
exists because it is what the spec dictates, and it will default to true
in future versions of Babel. The goal of this plugin is to mimic the behavior of decorators as they were implemented in Babel 5. I'm not looking to add enhancements like this.
What you want should be doable in future versions of the decorators spec, but implementing that is beyond the scope of this plugin. Until then, maybe you can get what you want by defining it as @syncFromFoo
instead?
How can I hook into the initializer? Right now there is no way I could react to the initialized value, for example to keep two properties synced.
Looking at the emitted code from
transform-decorators-legacy
, I can see no way to initializebar
with the initial value fromfoo
with just the decorator. Am I missing something?