-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prefer-to-be-undefined rule (#9)
- Loading branch information
Showing
5 changed files
with
104 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,28 @@ | ||
# Suggest using `toBeUndefined()` (prefer-to-be-undefined) | ||
|
||
In order to have a better failure message, `toBeUndefined()` should be used upon | ||
asserting expections on undefined value. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toBe()` is used to assert a undefined value. | ||
|
||
```js | ||
expect(undefined).toBe(undefined); | ||
``` | ||
|
||
This rule is enabled by default. | ||
|
||
### Default configuration | ||
|
||
The following pattern is considered warning: | ||
|
||
```js | ||
expect(undefined).toBe(undefined); | ||
``` | ||
|
||
The following pattern is not warning: | ||
|
||
```js | ||
expect(undefined).toBeUndefined(); | ||
``` |
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,35 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../../').rules; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('prefer_to_be_undefined', rules['prefer-to-be-undefined'], { | ||
valid: ['expect(undefined).toBeUndefined();'], | ||
|
||
invalid: [ | ||
{ | ||
code: 'expect(undefined).toBe(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeUndefined() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(undefined).toBeUndefined();', | ||
}, | ||
{ | ||
code: 'expect(undefined).toEqual(undefined);', | ||
errors: [ | ||
{ | ||
message: 'Use toBeUndefined() instead', | ||
column: 19, | ||
line: 1, | ||
}, | ||
], | ||
output: 'expect(undefined).toBeUndefined();', | ||
}, | ||
], | ||
}); |
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,37 @@ | ||
'use strict'; | ||
|
||
module.exports = context => { | ||
return { | ||
CallExpression(node) { | ||
const calleeName = node.callee.name; | ||
|
||
if ( | ||
calleeName === 'expect' && | ||
node.arguments.length == 1 && | ||
node.parent && | ||
node.parent.type === 'MemberExpression' && | ||
node.parent.parent | ||
) { | ||
const parentProperty = node.parent.property; | ||
const propertyName = parentProperty.name; | ||
const argument = node.parent.parent.arguments[0]; | ||
|
||
if ( | ||
(propertyName === 'toBe' || propertyName === 'toEqual') && | ||
argument.value === undefined | ||
) { | ||
context.report({ | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceText(parentProperty, 'toBeUndefined'), | ||
fixer.remove(argument), | ||
]; | ||
}, | ||
message: 'Use toBeUndefined() instead', | ||
node: parentProperty, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |