pzuraq/macro-decorators

Cannot use with `#privateProperty`

lougreenwood opened this issue · 2 comments

Previously discussed on Discord - https://discord.com/channels/480462759797063690/608346628163633192/781996880547938344

TLDR;

Because the specification for JS private class properties does not support dynamic lookup, macro decorators such as @reads() etc are not able to work with a string path passed to the decorator, since the decorator can't dynamically pick the string path from the this context - see here.

Would it be (technically) possible & desirable to allow passing in the class property itself, e.g:

@empty(this.#somePrivateThing) #thingExists;

I'm guessing not, since passing the path as a string allows the decorator getter to be lazy?

So the decorator executes when the class is defined, not when the class is initialized. This means that this in your example code is bound to the external scope, not the class itself. In order to access the private field, you would need to be passed a function which can access the value instead, which could then be called with the class instance as the backing value:

@empty(function() { return this.#somePrivateThing }) #thingExists;

This function would also need to have access to the private name, which is not a normal thing that can happen. It would need to be included in the spec for decorators I believe. This is a good thing to bring up in that proposal, maybe you could open an issue on the decorators repo? https://github.com/tc39/proposal-decorators