Skip to content

Commit

Permalink
fix: make .not.to{Be,Equal} autofixable
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 6, 2017
1 parent c35f2a1 commit e25f7af
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 15 deletions.
30 changes: 30 additions & 0 deletions rules/__tests__/prefer_to_be_null.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ ruleTester.run('prefer_to_be_null', rules['prefer-to-be-null'], {
valid: [
'expect(null).toBeNull();',
'expect(null).toEqual();',
'expect(null).not.toBeNull();',
'expect(null).not.toEqual();',
'expect(null).toBe(undefined);',
'expect(null).not.toBe(undefined);',
'expect(null).toBe();',
'expect(null).toMatchSnapshot();',
'expect("a string").toMatchSnapshot(null);',
'expect("a string").not.toMatchSnapshot();',
"expect(something).toEqual('a string');",
],

Expand All @@ -35,5 +43,27 @@ ruleTester.run('prefer_to_be_null', rules['prefer-to-be-null'], {
],
output: 'expect(null).toBeNull();',
},
{
code: 'expect("a string").not.toBe(null);',
errors: [
{
message: 'Use toBeNull() instead',
column: 24,
line: 1,
},
],
output: 'expect("a string").not.toBeNull();',
},
{
code: 'expect("a string").not.toEqual(null);',
errors: [
{
message: 'Use toBeNull() instead',
column: 24,
line: 1,
},
],
output: 'expect("a string").not.toBeNull();',
},
],
});
22 changes: 22 additions & 0 deletions rules/__tests__/prefer_to_be_undefined.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,27 @@ ruleTester.run('prefer_to_be_undefined', rules['prefer-to-be-undefined'], {
],
output: 'expect(undefined).toBeUndefined();',
},
{
code: 'expect("a string").not.toBe(undefined);',
errors: [
{
message: 'Use toBeUndefined() instead',
column: 24,
line: 1,
},
],
output: 'expect("a string").not.toBeUndefined();',
},
{
code: 'expect("a string").not.toEqual(undefined);',
errors: [
{
message: 'Use toBeUndefined() instead',
column: 24,
line: 1,
},
],
output: 'expect("a string").not.toBeUndefined();',
},
],
});
22 changes: 18 additions & 4 deletions rules/prefer_to_be_null.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
'use strict';
const argument = require('./util').argument;
const argument2 = require('./util').argument2;
const expectToBeCase = require('./util').expectToBeCase;
const expectToEqualCase = require('./util').expectToEqualCase;
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
const expectNotToBeCase = require('./util').expectNotToBeCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
if (expectToBeCase(node, null) || expectToEqualCase(node, null)) {
const is = expectToBeCase(node, null) || expectToEqualCase(node, null);
const isNot =
expectNotToEqualCase(node, null) || expectNotToBeCase(node, null);

if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
return [
fixer.replaceText(method(node), 'toBeNull'),
fixer.remove(argument(node)),
];
}
return [
fixer.replaceText(method(node), 'toBeNull'),
fixer.remove(argument(node)),
fixer.replaceText(method2(node), 'toBeNull'),
fixer.remove(argument2(node)),
];
},
message: 'Use toBeNull() instead',
node: method(node),
node: is ? method(node) : method2(node),
});
}
},
Expand Down
27 changes: 20 additions & 7 deletions rules/prefer_to_be_undefined.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
'use strict';
const argument = require('./util').argument;
const argument2 = require('./util').argument2;
const expectToBeCase = require('./util').expectToBeCase;
const expectNotToBeCase = require('./util').expectNotToBeCase;
const expectToEqualCase = require('./util').expectToEqualCase;
const expectNotToEqualCase = require('./util').expectNotToEqualCase;
const method = require('./util').method;
const method2 = require('./util').method2;

module.exports = context => {
return {
CallExpression(node) {
if (
expectToBeCase(node, undefined) ||
expectToEqualCase(node, undefined)
) {
const is =
expectToBeCase(node, undefined) || expectToEqualCase(node, undefined);
const isNot =
expectNotToEqualCase(node, undefined) ||
expectNotToBeCase(node, undefined);

if (is || isNot) {
context.report({
fix(fixer) {
if (is) {
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
];
}
return [
fixer.replaceText(method(node), 'toBeUndefined'),
fixer.remove(argument(node)),
fixer.replaceText(method2(node), 'toBeUndefined'),
fixer.remove(argument2(node)),
];
},
message: 'Use toBeUndefined() instead',
node: method(node),
node: is ? method(node) : method2(node),
});
}
},
Expand Down
12 changes: 8 additions & 4 deletions rules/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ const expectNotToBeCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toBe' &&
argument2(node) &&
argument2(node).value === arg &&
(arg === null || argument2(node).name);
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));

const expectToEqualCase = (node, arg) =>
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
Expand All @@ -53,8 +55,10 @@ const expectNotToEqualCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toEqual' &&
argument2(node) &&
argument2(node).value === arg &&
(arg === null || argument2(node).name);
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));

const expectToBeUndefinedCase = node =>
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
Expand Down

0 comments on commit e25f7af

Please sign in to comment.