Skip to content

Commit

Permalink
Change import statement
Browse files Browse the repository at this point in the history
  • Loading branch information
steventsao committed Nov 19, 2019
1 parent f9d1a56 commit cc80588
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/rules/no-get-with-default.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ This rule **allows** the following:
const test = this.key === undefined ? [] : this.key;
```

```js
// the behavior of this is different because `test` would be assigned `[]` on any falsy value instead of on only `undefined`.
const test = this.key || [];
```

## References

- [RFC](https://github.com/emberjs/rfcs/pull/554/) to deprecate `getWithDefault`

## Related Rules

- [no-get](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/no-get.md)
- [spec](http://api.emberjs.com/ember/3.13/functions/@ember%2Fobject/getWithDefault)
6 changes: 3 additions & 3 deletions lib/rules/no-get-with-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const types = require('../utils/types');

function makeErrorMessageForGetWithDefault(property, isImportedGetWithDefault) {
return isImportedGetWithDefault
? `Use \`this.${property}\` instead of \`getWithDefault(this, '${property}', ...)\``
: `Use \`this.${property}\` instead of \`this.getWithDefault('${property}', ...)\``;
? "Use \`||\` or the ternary operator instead of \`getWithDefault()\`."
: "Use \`this.property || defaultValue\` or the \`this.property === undefined ? defaultValue : this.property\` instead of \`getWithDefault(this.property, defaultValue)\`.";
}

module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Use the `||` operator instead of `getWithDefault` for more expected behaviors',
description: 'Use the \`||\` operator instead of \`getWithDefault\` for more expected behaviors',
category: 'Best Practices',
recommended: false,
octane: true,
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/no-get-with-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ ruleTester.run('no-get-with-default', rule, {
code: "const test = this.getWithDefault('key', []);",
errors: [
{
message: "Use `this.key` instead of `this.getWithDefault('key', ...)`",
message: "Use `this.property || defaultValue` or the `this.property === undefined ? defaultValue : this.property` instead of `getWithDefault(this.property, defaultValue)`.",
type: 'CallExpression',
},
],
output: null,
},
{
code: "const test = getWithDefault(this, 'key', []);",
errors: [
{
message: "Use `this.key` instead of `getWithDefault(this, 'key', ...)`",
message: "Use \`||\` or the ternary operator instead of \`getWithDefault()\`.",
type: 'CallExpression',
},
],
output: null,
},
],
});

0 comments on commit cc80588

Please sign in to comment.