Skip to content

Commit e25f7af

Browse files
committed
fix: make .not.to{Be,Equal} autofixable
1 parent c35f2a1 commit e25f7af

5 files changed

+98
-15
lines changed

rules/__tests__/prefer_to_be_null.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ ruleTester.run('prefer_to_be_null', rules['prefer-to-be-null'], {
99
valid: [
1010
'expect(null).toBeNull();',
1111
'expect(null).toEqual();',
12+
'expect(null).not.toBeNull();',
13+
'expect(null).not.toEqual();',
14+
'expect(null).toBe(undefined);',
15+
'expect(null).not.toBe(undefined);',
16+
'expect(null).toBe();',
17+
'expect(null).toMatchSnapshot();',
18+
'expect("a string").toMatchSnapshot(null);',
19+
'expect("a string").not.toMatchSnapshot();',
1220
"expect(something).toEqual('a string');",
1321
],
1422

@@ -35,5 +43,27 @@ ruleTester.run('prefer_to_be_null', rules['prefer-to-be-null'], {
3543
],
3644
output: 'expect(null).toBeNull();',
3745
},
46+
{
47+
code: 'expect("a string").not.toBe(null);',
48+
errors: [
49+
{
50+
message: 'Use toBeNull() instead',
51+
column: 24,
52+
line: 1,
53+
},
54+
],
55+
output: 'expect("a string").not.toBeNull();',
56+
},
57+
{
58+
code: 'expect("a string").not.toEqual(null);',
59+
errors: [
60+
{
61+
message: 'Use toBeNull() instead',
62+
column: 24,
63+
line: 1,
64+
},
65+
],
66+
output: 'expect("a string").not.toBeNull();',
67+
},
3868
],
3969
});

rules/__tests__/prefer_to_be_undefined.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,27 @@ ruleTester.run('prefer_to_be_undefined', rules['prefer-to-be-undefined'], {
4040
],
4141
output: 'expect(undefined).toBeUndefined();',
4242
},
43+
{
44+
code: 'expect("a string").not.toBe(undefined);',
45+
errors: [
46+
{
47+
message: 'Use toBeUndefined() instead',
48+
column: 24,
49+
line: 1,
50+
},
51+
],
52+
output: 'expect("a string").not.toBeUndefined();',
53+
},
54+
{
55+
code: 'expect("a string").not.toEqual(undefined);',
56+
errors: [
57+
{
58+
message: 'Use toBeUndefined() instead',
59+
column: 24,
60+
line: 1,
61+
},
62+
],
63+
output: 'expect("a string").not.toBeUndefined();',
64+
},
4365
],
4466
});

rules/prefer_to_be_null.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
'use strict';
22
const argument = require('./util').argument;
3+
const argument2 = require('./util').argument2;
34
const expectToBeCase = require('./util').expectToBeCase;
45
const expectToEqualCase = require('./util').expectToEqualCase;
6+
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
7+
const expectNotToBeCase = require('./util').expectNotToBeCase;
58
const method = require('./util').method;
9+
const method2 = require('./util').method2;
610

711
module.exports = context => {
812
return {
913
CallExpression(node) {
10-
if (expectToBeCase(node, null) || expectToEqualCase(node, null)) {
14+
const is = expectToBeCase(node, null) || expectToEqualCase(node, null);
15+
const isNot =
16+
expectNotToEqualCase(node, null) || expectNotToBeCase(node, null);
17+
18+
if (is || isNot) {
1119
context.report({
1220
fix(fixer) {
21+
if (is) {
22+
return [
23+
fixer.replaceText(method(node), 'toBeNull'),
24+
fixer.remove(argument(node)),
25+
];
26+
}
1327
return [
14-
fixer.replaceText(method(node), 'toBeNull'),
15-
fixer.remove(argument(node)),
28+
fixer.replaceText(method2(node), 'toBeNull'),
29+
fixer.remove(argument2(node)),
1630
];
1731
},
1832
message: 'Use toBeNull() instead',
19-
node: method(node),
33+
node: is ? method(node) : method2(node),
2034
});
2135
}
2236
},

rules/prefer_to_be_undefined.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
'use strict';
22
const argument = require('./util').argument;
3+
const argument2 = require('./util').argument2;
34
const expectToBeCase = require('./util').expectToBeCase;
5+
const expectNotToBeCase = require('./util').expectNotToBeCase;
46
const expectToEqualCase = require('./util').expectToEqualCase;
7+
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
58
const method = require('./util').method;
9+
const method2 = require('./util').method2;
610

711
module.exports = context => {
812
return {
913
CallExpression(node) {
10-
if (
11-
expectToBeCase(node, undefined) ||
12-
expectToEqualCase(node, undefined)
13-
) {
14+
const is =
15+
expectToBeCase(node, undefined) || expectToEqualCase(node, undefined);
16+
const isNot =
17+
expectNotToEqualCase(node, undefined) ||
18+
expectNotToBeCase(node, undefined);
19+
20+
if (is || isNot) {
1421
context.report({
1522
fix(fixer) {
23+
if (is) {
24+
return [
25+
fixer.replaceText(method(node), 'toBeUndefined'),
26+
fixer.remove(argument(node)),
27+
];
28+
}
1629
return [
17-
fixer.replaceText(method(node), 'toBeUndefined'),
18-
fixer.remove(argument(node)),
30+
fixer.replaceText(method2(node), 'toBeUndefined'),
31+
fixer.remove(argument2(node)),
1932
];
2033
},
2134
message: 'Use toBeUndefined() instead',
22-
node: method(node),
35+
node: is ? method(node) : method2(node),
2336
});
2437
}
2538
},

rules/util.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ const expectNotToBeCase = (node, arg) =>
3636
expectNotCase(node) &&
3737
methodName2(node) === 'toBe' &&
3838
argument2(node) &&
39-
argument2(node).value === arg &&
40-
(arg === null || argument2(node).name);
39+
((argument2(node).type === 'Literal' &&
40+
argument2(node).value === null &&
41+
arg === null) ||
42+
(argument2(node).name === 'undefined' && arg === undefined));
4143

4244
const expectToEqualCase = (node, arg) =>
4345
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
@@ -53,8 +55,10 @@ const expectNotToEqualCase = (node, arg) =>
5355
expectNotCase(node) &&
5456
methodName2(node) === 'toEqual' &&
5557
argument2(node) &&
56-
argument2(node).value === arg &&
57-
(arg === null || argument2(node).name);
58+
((argument2(node).type === 'Literal' &&
59+
argument2(node).value === null &&
60+
arg === null) ||
61+
(argument2(node).name === 'undefined' && arg === undefined));
5862

5963
const expectToBeUndefinedCase = node =>
6064
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&

0 commit comments

Comments
 (0)