eslint-plugin-ember icon indicating copy to clipboard operation
eslint-plugin-ember copied to clipboard

New Rule: no-computed-without-dependent-keys

Open Turbo87 opened this issue 7 years ago • 5 comments

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);
}

Turbo87 avatar Sep 07 '18 14:09 Turbo87

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...

rwjblue avatar Sep 08 '18 21:09 rwjblue

Note: I’m still in favor of the rule. The scenario I laid out above is much less common...

rwjblue avatar Sep 08 '18 21:09 rwjblue

Yeah, I guess in those situations it can make sense. That point should probably be added to the rule documentation then.

Turbo87 avatar Sep 09 '18 05:09 Turbo87

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 avatar Sep 19 '18 18:09 Kerrick

@Kerrick yes, putting it in init is usually better in these cases

Turbo87 avatar Sep 22 '18 16:09 Turbo87