New Rule: no-computed-without-dependent-keys
The proposed rule should warn about computed properties without any dependent keys:
foo: computed(function() {
return 42;
})
instead the property should be initialized in the constructor:
init() {
this._super(...arguments);
this.set('foo', 42);
}
I think I generally agree, but the two syntaxes aren’t identical. Using init means you are doing whatever setup work for each instance whereas using the CP form would only run that initialization when something asks for it. In the provided example that nuance isn’t too important, but I have absolutely seen this pattern used (over init) to avoid doing extra work that isn’t used by 80%+ of instances...
Note: I’m still in favor of the rule. The scenario I laid out above is much less common...
Yeah, I guess in those situations it can make sense. That point should probably be added to the rule documentation then.
We've found foo: computed(() => ({ some: 'stuff', goes: 'here' }), to be a helpful pattern to avoid ember/avoid-leaking-state-in-ember-objects when we want to have a hash accessible by both the JS and the template. Would the preferred pattern really be to set that in init? It would be harder to find while scanning the component's JS looking for a property you use in the template.
@Kerrick yes, putting it in init is usually better in these cases