From cc80588e3a8f8b77af53c694e7d0cb34a6b504f8 Mon Sep 17 00:00:00 2001 From: Steven Tsao Date: Mon, 18 Nov 2019 21:05:00 -0800 Subject: [PATCH] Change import statement --- docs/rules/no-get-with-default.md | 6 ++++++ lib/rules/no-get-with-default.js | 6 +++--- tests/lib/rules/no-get-with-default.js | 6 ++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/rules/no-get-with-default.md b/docs/rules/no-get-with-default.md index fdc6e88427..36cc4ba24c 100644 --- a/docs/rules/no-get-with-default.md +++ b/docs/rules/no-get-with-default.md @@ -26,6 +26,11 @@ 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` @@ -33,3 +38,4 @@ const test = this.key === undefined ? [] : this.key; ## 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) \ No newline at end of file diff --git a/lib/rules/no-get-with-default.js b/lib/rules/no-get-with-default.js index da8ec58be1..fe10304450 100644 --- a/lib/rules/no-get-with-default.js +++ b/lib/rules/no-get-with-default.js @@ -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, diff --git a/tests/lib/rules/no-get-with-default.js b/tests/lib/rules/no-get-with-default.js index fd19949d5e..f8eea35f99 100644 --- a/tests/lib/rules/no-get-with-default.js +++ b/tests/lib/rules/no-get-with-default.js @@ -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, }, ], });