💼 This rule is enabled in the ✅ recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
This rule attempts to catch and prevent the use of getWithDefault
.
Even though the behavior for getWithDefault
is more defined such that it only falls back to the default value on undefined
, its inconsistency with the native ||
is confusing to many developers who assume otherwise. Instead, this rule encourages developers to use:
||
operator- ternary operator
In addition, Nullish Coalescing Operator ??
will land in the JavaScript language soon so developers can leverage safe property access with native support instead of using getWithDefault
. But note that ??
checks for either undefined
or null
whereas getWithDefault
only checks for undefined
.
Examples of incorrect code for this rule:
const test = this.getWithDefault('key', []);
import { getWithDefault } from '@ember/object';
const test = getWithDefault(this, 'key', []);
Examples of correct code for this rule:
const test = this.key === undefined ? [] : this.key;
// the behavior of this is different because `test` would be assigned `[]` on any falsy value instead of on only `undefined`.
const test = this.key || [];
This rule takes an optional object containing:
boolean
--catchSafeObjects
-- whether the rule should catch non-this
imported usages likegetWithDefault(person, 'name', '')
(defaulttrue
)boolean
--catchUnsafeObjects
-- whether the rule should catch non-this
usages likeperson.getWithDefault('name', '')
even though we don't know for sure ifperson
is an Ember object (defaulttrue
)