💼 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.
This rule disallows:
this.get('someProperty')whenthis.somePropertycan be usedthis.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
unknownPropertymethod
In addition, mirage/config.js will be excluded from this rule.
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()`.
}
});This rule takes an optional object containing:
boolean--ignoreGetProperties-- whether the rule should ignoregetProperties(defaultfalse)boolean--ignoreNestedPaths-- whether the rule should ignorethis.get('some.nested.property')(can't be enabled at the same time asuseOptionalChaining) (defaultfalse)boolean--useOptionalChaining-- whether the rule should use the optional chaining operator?.to autofix nested paths such asthis.get('some.nested.property')tothis.some?.nested?.property(when this option is off, these nested paths won't be autofixed at all) (defaulttrue)boolean--catchSafeObjects-- whether the rule should catch non-thisimported usages likeget(foo, 'bar')(defaulttrue)boolean--catchUnsafeObjects-- whether the rule should catch non-thisusages likefoo.get('bar')even though we don't know for sure iffoois an Ember object (defaultfalse)
- Ember 3.1 Release Notes describing "ES5 Getters for Computed Properties"
- Ember get Spec
- Ember getProperties Spec
- Ember ES5 Getter RFC
- es5-getter-ember-codemod
- More context about the proxy object exception to this rule