ember/no-get

October 31, 2023 ยท View on GitHub

๐Ÿ’ผ This rule is enabled in the โœ… recommended config.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

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

NameDescriptionTypeDefault
catchSafeObjectsWhether the rule should catch non-this imported usages like get(foo, 'bar').Booleantrue
catchUnsafeObjectsWhether 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.Booleanfalse
ignoreGetPropertiesWhether the rule should ignore getProperties.Booleanfalse
ignoreNestedPathsWhether the rule should ignore this.get('some.nested.property') (can't be enabled at the same time as useOptionalChaining).Booleanfalse
useAtWhether the rule should use at(-1) Array.prototype.at() to replace lastObject.Booleantrue
useOptionalChainingWhether 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).Booleantrue

References