Skip to content

Commit

Permalink
feat: add prefer-to-be-defined rule
Browse files Browse the repository at this point in the history
BREAKING CHANGE: prefer-to-be-undefined rule has been removed and merged
into this rule.
  • Loading branch information
xfumihiro committed Dec 5, 2017
1 parent e3f8884 commit 0f7c6ba
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 109 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ config file:
}
```

See
[ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
See [ESLint
documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files)
for more information about extending configuration files.

## Rules

| Rule | Description | Recommended | Fixable |
| -------------------------------------------------------------- | ----------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-unhandled-errors.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using toHaveLength() | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using toBeNull() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using toBeUndefined() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid expect() usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| Rule | Description | Recommended | Fixable |
| ------------------------------------------------------------ | --------------------------------------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------- |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-identical-title](docs/rules/no-unhandled-errors.md) | Disallow identical titles | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using toHaveLength() | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using toBeNull() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-to-be-defined](docs/rules/prefer-to-be-defined.md) | Suggest using toBeDefined() / toBeUndefined() | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid expect() usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |

## Credit

Expand Down
32 changes: 32 additions & 0 deletions docs/rules/prefer-to-be-defined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Suggest using `toBeDefined()` / `toBeUndefined()` (prefer-to-be-defined)

In order to have a better failure message, `toBeDefined()` or `toBeUndefined()`
should be used upon asserting expections on defined or undefined value.

## Rule details

This rule triggers a warning if `not.toBe()` is used to assert a undefined value
or `toBe()` is used to assert a undefined value.

```js
expect(true).not.toBe(undefined);
expect(undefined).toBe(undefined);
```

This rule is enabled by default.

### Default configuration

The following patterns are considered warning:

```js
expect(true).not.toBeUndefined();
expect(undefined).toBe(undefined);
```

The following patterns are not warning:

```js
expect(true).toBeDefined();
expect(undefined).toBeUndefined();
```
28 changes: 0 additions & 28 deletions docs/rules/prefer-to-be-undefined.md

This file was deleted.

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const noFocusedTests = require('./rules/no_focused_tests');
const noIdenticalTitle = require('./rules/no_identical_title');
const noLargeSnapshots = require('./rules/no_large_snapshots');
const preferToBeNull = require('./rules/prefer_to_be_null');
const preferToBeUndefined = require('./rules/prefer_to_be_undefined');
const preferToBeDefined = require('./rules/prefer_to_be_defined');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');

Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = {
'no-identical-title': noIdenticalTitle,
'no-large-snapshots': noLargeSnapshots,
'prefer-to-be-null': preferToBeNull,
'prefer-to-be-undefined': preferToBeUndefined,
'prefer-to-be-defined': preferToBeDefined,
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
},
Expand Down
75 changes: 75 additions & 0 deletions rules/__tests__/prefer_to_be_defined.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const rules = require('../../').rules;

const ruleTester = new RuleTester();

ruleTester.run('prefer_to_be_defined', rules['prefer-to-be-defined'], {
valid: [
'expect(true).toBeDefined();',
'expect(true).not.toBeDefined();',
'expect(true).toBe(true);',
'expect(undefined).toBeUndefined();',
'expect({}).toEqual({});',
'expect(null).toEqual(null);',
],

invalid: [
{
code: 'expect(true).not.toBe(undefined);',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
code: 'expect(true).not.toEqual(undefined);',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
code: 'expect(true).not.toBeUndefined();',
errors: [
{
message: 'Use toBeDefined() instead',
column: 14,
line: 1,
},
],
output: 'expect(true).toBeDefined();',
},
{
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();',
},
],
});
40 changes: 0 additions & 40 deletions rules/__tests__/prefer_to_be_undefined.test.js

This file was deleted.

59 changes: 59 additions & 0 deletions rules/prefer_to_be_defined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
const argument = require('./util').argument;
const argument2 = require('./util').argument2;
const expectNotToBeCase = require('./util').expectNotToBeCase;
const expectNotToBeUndefinedCase = require('./util').expectNotToBeUndefinedCase;
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
const expectToBeCase = require('./util').expectToBeCase;
const expectToEqualCase = require('./util').expectToEqualCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
if (
expectNotToBeCase(node, undefined) ||
expectNotToEqualCase(node, undefined) ||
expectNotToBeUndefinedCase(node)
) {
context.report({
fix(fixer) {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
method(node),
method2(node),
token => token.value === '.'
);
const fixes = [
fixer.remove(method(node)),
fixer.remove(propertyDot),
fixer.replaceText(method2(node), 'toBeDefined'),
];
if (argument2(node)) {
fixes.push(fixer.remove(argument2(node)));
}
return fixes;
},
message: 'Use toBeDefined() instead',
node: method(node),
});
} else if (
expectToBeCase(node, undefined) ||
expectToEqualCase(node, undefined)
) {
context.report({
fix(fixer) {
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
];
},
message: 'Use toBeUndefined() instead',
node: method(node),
});
}
},
};
};
27 changes: 0 additions & 27 deletions rules/prefer_to_be_undefined.js

This file was deleted.

0 comments on commit 0f7c6ba

Please sign in to comment.