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
isStage1Descriptor: false positive, if passed a function #327
Comments
Taking the babel class transforms into account, I don't think there is any way to tell the difference between a We could either do it on a best effort basis, which will still fail in certain cases, or just outright "ban" users from passing a single function as a parameter. Arrow functions or Some ideas for heuristics:
We could also ship an extra babel transform, that transforms all decorator invocations with one singular parameter to be "tagged". @myDecorator(singleParam)
class Foo {} import { tag } from '@ember-decorators/utils/tag';
@myDecorator(tag(singleParam))
class Foo {} const tagged = new WeakSet();
export function tag(param) {
if (typeof param === 'function') {
tagged.add(param);
}
return param;
}
export function isTagged(param) {
return typeof param === 'function' || tagged.has(param);
} This would be 100 % safe. But is it overkill?
Seeing that this check was added with |
I would be up for writing such a transform, btw. |
If anyone else also runs into this, these are the ways out:
|
Yeah, not sure if it's really worth the effort given stage 1 decorators are on the way out. I think passing an arrow function is the best idea, though that could be tricky if the function needs to have |
Passing an arrow function also has another gotcha: Depending on your |
If you pass a
function() {}
as the only argument to a decorator,isStage1Descriptor
returns a false positive.ember-decorators/packages/utils/addon/decorator.js
Lines 31 to 35 in f6259d7
Do you think that there is any way to reliably tell a
function() {}
apart from aclass {}
?Funny coincidence, that "How Does React Tell a Class from a Function?" by Dan Abramov is trending on HN now. 😄
The text was updated successfully, but these errors were encountered: