✅ 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.
This rule disallows:
this.get('someProperty')
whenthis.someProperty
can 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
unknownProperty
method
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')
(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) (defaultfalse
)boolean
--catchSafeObjects
-- whether the rule should catch non-this
imported usages likeget(foo, 'bar')
(defaulttrue
)boolean
--catchUnsafeObjects
-- whether the rule should catch non-this
usages likefoo.get('bar')
even though we don't know for sure iffoo
is 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