-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from steventsao/master
Add new rule `no-get-with-default`
- Loading branch information
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# No `getWithDefault` (no-get-with-default) | ||
|
||
This rule attempts to catch and prevent the use of `getWithDefault`. | ||
|
||
## Rule Details | ||
|
||
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. This rule encourages developers to use | ||
the native `||` operator instead. | ||
|
||
In addition, [Nullish Coalescing Operator `??`](https://github.com/tc39/proposal-nullish-coalescing) will land in the JavaScript language soon so developers can leverage safe property access with native support instead of using `getWithDefault`. | ||
|
||
This rule **forbids** the following: | ||
|
||
```js | ||
const test = this.getWithDefault('key', []); | ||
``` | ||
|
||
```js | ||
const test = getWithDefault(this, 'key', []); | ||
``` | ||
|
||
This rule **allows** the following: | ||
|
||
```js | ||
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` | ||
- [spec](http://api.emberjs.com/ember/3.13/functions/@ember%2Fobject/getWithDefault) | ||
|
||
## Related Rules | ||
|
||
- [no-get](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/no-get.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const types = require('../utils/types'); | ||
|
||
const ERROR_MESSAGE = 'Use `||` or the ternary operator instead of `getWithDefault()`'; | ||
|
||
module.exports = { | ||
ERROR_MESSAGE, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: "Disallows use of the Ember's `getWithDefault` function", | ||
category: 'Best Practices', | ||
recommended: false, | ||
octane: true, | ||
url: | ||
'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-get-with-default.md', | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if ( | ||
types.isMemberExpression(node.callee) && | ||
types.isThisExpression(node.callee.object) && | ||
types.isIdentifier(node.callee.property) && | ||
node.callee.property.name === 'getWithDefault' && | ||
node.arguments.length === 2 | ||
) { | ||
// Example: this.getWithDefault('foo', 'bar'); | ||
context.report(node, ERROR_MESSAGE); | ||
} | ||
|
||
if ( | ||
types.isIdentifier(node.callee) && | ||
node.callee.name === 'getWithDefault' && | ||
node.arguments.length === 3 && | ||
types.isThisExpression(node.arguments[0]) | ||
) { | ||
// Example: getWithDefault(this, 'foo', 'bar'); | ||
context.report(node, ERROR_MESSAGE); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const rule = require('../../../lib/rules/no-get-with-default'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const { ERROR_MESSAGE } = rule; | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-get-with-default', rule, { | ||
valid: [ | ||
"const test = this.get('key') || [];", | ||
"const test = get(this, 'target') || [];", | ||
"testClass.getWithDefault('key', [])", | ||
"getWithDefault.testMethod(testClass, 'key', [])", | ||
], | ||
invalid: [ | ||
{ | ||
code: "const test = this.getWithDefault('key', []);", | ||
errors: [ | ||
{ | ||
message: ERROR_MESSAGE, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
output: null, | ||
}, | ||
{ | ||
code: "const test = getWithDefault(this, 'key', []);", | ||
errors: [ | ||
{ | ||
message: ERROR_MESSAGE, | ||
type: 'CallExpression', | ||
}, | ||
], | ||
output: null, | ||
}, | ||
], | ||
}); |