Skip to content

Latest commit

 

History

History
118 lines (82 loc) · 6.06 KB

no-get.md

File metadata and controls

118 lines (82 loc) · 6.06 KB

ember/no-get

💼 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

Name Description Type Default
catchSafeObjects Whether the rule should catch non-this imported usages like get(foo, 'bar'). Boolean true
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. Boolean false
ignoreGetProperties Whether the rule should ignore getProperties. Boolean false
ignoreNestedPaths Whether the rule should ignore this.get('some.nested.property') (can't be enabled at the same time as useOptionalChaining). Boolean false
useAt Whether the rule should use at(-1) Array.prototype.at() to replace lastObject. Boolean true
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). Boolean true

Related Rules

References