Skip to content

Latest commit

 

History

History
111 lines (77 loc) · 3.98 KB

no-get.md

File metadata and controls

111 lines (77 loc) · 3.98 KB

no-get

✅ The "extends": "plugin:ember/recommended" property in a configuration file enables this rule.

🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

Starting in Ember 3.1, native ES5 getters are available, which eliminates much of the need to use get / getProperties on Ember objects.

Rule Details

This rule disallows:

  • this.get('someProperty') when this.someProperty can be used
  • this.getProperties('prop1', 'prop2') when { prop1: this.prop1, prop2: this.prop2 } can be used

WARNING: there are a number of circumstances where get / getProperties still need to be used, and you may need to manually disable the rule for these (although the rule will attempt to ignore them):

  • Ember proxy objects (ObjectProxy, ArrayProxy)
  • Objects implementing the unknownProperty method

In addition, mirage/config.js will be excluded from this rule.

Examples

Examples of incorrect code for this rule:

const foo = this.get('someProperty');
import { get } from '@ember/object';

const foo = get(this, 'someProperty');
const { prop1, prop2 } = this.getProperties('prop1', 'prop2');
import { getProperties } from '@ember/object';

const foo = getProperties(this, 'prop1', 'prop2');

Examples of correct code for this rule:

const foo = this.someProperty;
const foo = this.nested?.path; // Optional chaining can be useful if the nested path can have null or undefined properties in it.
const foo = this.get('some.nested.property'); // Allowed if `ignoreNestedPaths` option is enabled.
const { prop1, prop2 } = this;
const foo = { prop1: this.prop1, prop2: this.prop2 };
import ObjectProxy from '@ember/object/proxy';

export default ObjectProxy.extend({
  someFunction() {
    const foo = this.get('propertyInsideProxyObject'); // Allowed because inside proxy object.
  }
});
import EmberObject from '@ember/object';

export default EmberObject.extend({
  unknownProperty(key) {},
  someFunction() {
    const foo = this.get('property'); // Allowed because inside object implementing `unknownProperty()`.
  }
});

Configuration

This rule takes an optional object containing:

  • boolean -- ignoreGetProperties -- whether the rule should ignore getProperties (default false)
  • boolean -- ignoreNestedPaths -- whether the rule should ignore this.get('some.nested.property') (can't be enabled at the same time as useOptionalChaining) (default false)
  • boolean -- useOptionalChaining -- whether the rule should use the optional chaining operator ?. to autofix nested paths such as this.get('some.nested.property') to this.some?.nested?.property (when this option is off, these nested paths won't be autofixed at all) (default true)
  • boolean -- catchSafeObjects -- whether the rule should catch non-this imported usages like get(foo, 'bar') (default true)
  • boolean -- catchUnsafeObjects -- whether the rule should catch non-this usages like foo.get('bar') even though we don't know for sure if foo is an Ember object (default false)

Related Rules

References