Skip to content

Commit bc74d33

Browse files
xfumihiroSimenB
authored andcommitted
Refactor prefer rules (#26)
1 parent a42d917 commit bc74d33

File tree

5 files changed

+149
-91
lines changed

5 files changed

+149
-91
lines changed

rules/__tests__/prefer_to_be_undefined.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const rules = require('../../').rules;
66
const ruleTester = new RuleTester();
77

88
ruleTester.run('prefer_to_be_undefined', rules['prefer-to-be-undefined'], {
9-
valid: ['expect(undefined).toBeUndefined();'],
9+
valid: [
10+
'expect(undefined).toBeUndefined();',
11+
'expect(true).not.toBeUndefined();',
12+
],
1013

1114
invalid: [
1215
{

rules/prefer_to_be_null.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
'use strict';
2+
const argument = require('./util').argument;
3+
const expectToBeCase = require('./util').expectToBeCase;
4+
const expectToEqualCase = require('./util').expectToEqualCase;
5+
const method = require('./util').method;
26

37
module.exports = context => {
48
return {
59
CallExpression(node) {
6-
const calleeName = node.callee.name;
7-
8-
if (
9-
calleeName === 'expect' &&
10-
node.arguments.length == 1 &&
11-
node.parent &&
12-
node.parent.type === 'MemberExpression' &&
13-
node.parent.parent
14-
) {
15-
const parentProperty = node.parent.property;
16-
const propertyName = parentProperty.name;
17-
const argument = node.parent.parent.arguments[0];
18-
19-
if (
20-
(propertyName === 'toBe' || propertyName === 'toEqual') &&
21-
argument.value === null
22-
) {
23-
context.report({
24-
fix(fixer) {
25-
return [
26-
fixer.replaceText(parentProperty, 'toBeNull'),
27-
fixer.remove(argument),
28-
];
29-
},
30-
message: 'Use toBeNull() instead',
31-
node: parentProperty,
32-
});
33-
}
10+
if (expectToBeCase(node, null) || expectToEqualCase(node, null)) {
11+
context.report({
12+
fix(fixer) {
13+
return [
14+
fixer.replaceText(method(node), 'toBeNull'),
15+
fixer.remove(argument(node)),
16+
];
17+
},
18+
message: 'Use toBeNull() instead',
19+
node: method(node),
20+
});
3421
}
3522
},
3623
};

rules/prefer_to_be_undefined.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
'use strict';
2+
const argument = require('./util').argument;
3+
const expectToBeCase = require('./util').expectToBeCase;
4+
const expectToEqualCase = require('./util').expectToEqualCase;
5+
const method = require('./util').method;
26

37
module.exports = context => {
48
return {
59
CallExpression(node) {
6-
const calleeName = node.callee.name;
7-
810
if (
9-
calleeName === 'expect' &&
10-
node.arguments.length == 1 &&
11-
node.parent &&
12-
node.parent.type === 'MemberExpression' &&
13-
node.parent.parent
11+
expectToBeCase(node, undefined) ||
12+
expectToEqualCase(node, undefined)
1413
) {
15-
const parentProperty = node.parent.property;
16-
const propertyName = parentProperty.name;
17-
const argument = node.parent.parent.arguments[0];
18-
19-
if (
20-
(propertyName === 'toBe' || propertyName === 'toEqual') &&
21-
argument.value === undefined
22-
) {
23-
context.report({
24-
fix(fixer) {
25-
return [
26-
fixer.replaceText(parentProperty, 'toBeUndefined'),
27-
fixer.remove(argument),
28-
];
29-
},
30-
message: 'Use toBeUndefined() instead',
31-
node: parentProperty,
32-
});
33-
}
14+
context.report({
15+
fix(fixer) {
16+
return [
17+
fixer.replaceText(method(node), 'toBeUndefined'),
18+
fixer.remove(argument(node)),
19+
];
20+
},
21+
message: 'Use toBeUndefined() instead',
22+
node: method(node),
23+
});
3424
}
3525
},
3626
};

rules/prefer_to_have_length.js

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
'use strict';
2+
const expectCase = require('./util').expectCase;
3+
const expectNotCase = require('./util').expectNotCase;
4+
const expectResolveCase = require('./util').expectResolveCase;
5+
const expectRejectCase = require('./util').expectRejectCase;
6+
const method = require('./util').method;
27

38
module.exports = context => {
49
return {
510
CallExpression(node) {
6-
const calleeName = node.callee.name;
7-
811
if (
9-
calleeName === 'expect' &&
10-
node.arguments.length == 1 &&
11-
node.parent &&
12-
node.parent.type === 'MemberExpression' &&
13-
node.parent.parent
12+
!(
13+
expectNotCase(node) ||
14+
expectResolveCase(node) ||
15+
expectRejectCase(node)
16+
) &&
17+
expectCase(node) &&
18+
(method(node).name === 'toBe' || method(node).name === 'toEqual') &&
19+
node.arguments[0].property &&
20+
node.arguments[0].property.name === 'length'
1421
) {
15-
const parentProperty = node.parent.property;
16-
const propertyName = parentProperty.name;
17-
const argumentObject = node.arguments[0].object;
18-
const argumentProperty = node.arguments[0].property;
19-
20-
if (
21-
(propertyName === 'toBe' || propertyName === 'toEqual') &&
22-
argumentProperty &&
23-
argumentProperty.name === 'length'
24-
) {
25-
const propertyDot = context
26-
.getSourceCode()
27-
.getFirstTokenBetween(
28-
argumentObject,
29-
argumentProperty,
30-
token => token.value === '.'
31-
);
32-
context.report({
33-
fix(fixer) {
34-
return [
35-
fixer.remove(propertyDot),
36-
fixer.remove(argumentProperty),
37-
fixer.replaceText(parentProperty, 'toHaveLength'),
38-
];
39-
},
40-
message: 'Use toHaveLength() instead',
41-
node: parentProperty,
42-
});
43-
}
22+
const propertyDot = context
23+
.getSourceCode()
24+
.getFirstTokenBetween(
25+
node.arguments[0].object,
26+
node.arguments[0].property,
27+
token => token.value === '.'
28+
);
29+
context.report({
30+
fix(fixer) {
31+
return [
32+
fixer.remove(propertyDot),
33+
fixer.remove(node.arguments[0].property),
34+
fixer.replaceText(method(node), 'toHaveLength'),
35+
];
36+
},
37+
message: 'Use toHaveLength() instead',
38+
node: method(node),
39+
});
4440
}
4541
},
4642
};

rules/util.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
const expectCase = node =>
4+
node.callee.name === 'expect' &&
5+
node.arguments.length == 1 &&
6+
node.parent &&
7+
node.parent.type === 'MemberExpression' &&
8+
node.parent.parent;
9+
10+
const expectNotCase = node =>
11+
expectCase(node) &&
12+
node.parent.parent.type === 'MemberExpression' &&
13+
methodName(node) === 'not';
14+
15+
const expectResolveCase = node =>
16+
expectCase(node) &&
17+
node.parent.parent.type === 'MemberExpression' &&
18+
methodName(node) === 'resolve';
19+
20+
const expectRejectCase = node =>
21+
expectCase(node) &&
22+
node.parent.parent.type === 'MemberExpression' &&
23+
methodName(node) === 'reject';
24+
25+
const expectToBeCase = (node, arg) =>
26+
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
27+
expectCase(node) &&
28+
methodName(node) === 'toBe' &&
29+
argument(node).value === arg;
30+
31+
const expectNotToBeCase = (node, arg) =>
32+
expectNotCase(node) &&
33+
methodName2(node) === 'toBe' &&
34+
argument2(node).value === arg;
35+
36+
const expectToEqualCase = (node, arg) =>
37+
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
38+
expectCase(node) &&
39+
methodName(node) === 'toEqual' &&
40+
argument(node).value === arg;
41+
42+
const expectNotToEqualCase = (node, arg) =>
43+
expectNotCase(node) &&
44+
methodName2(node) === 'toEqual' &&
45+
argument2(node).value === arg;
46+
47+
const expectToBeUndefinedCase = node =>
48+
!(expectNotCase(node) || expectResolveCase(node) || expectRejectCase(node)) &&
49+
expectCase(node) &&
50+
methodName(node) === 'toBeUndefined';
51+
52+
const expectNotToBeUndefinedCase = node =>
53+
expectNotCase(node) && methodName2(node) === 'toBeUndefined';
54+
55+
const method = node => node.parent.property;
56+
57+
const method2 = node => node.parent.parent.property;
58+
59+
const methodName = node => method(node).name;
60+
61+
const methodName2 = node => method2(node).name;
62+
63+
const argument = node => node.parent.parent.arguments[0];
64+
65+
const argument2 = node => node.parent.parent.parent.arguments[0];
66+
67+
module.exports = {
68+
method: method,
69+
method2: method2,
70+
argument: argument,
71+
argument2: argument2,
72+
expectCase: expectCase,
73+
expectNotCase: expectNotCase,
74+
expectResolveCase: expectResolveCase,
75+
expectRejectCase: expectRejectCase,
76+
expectToBeCase: expectToBeCase,
77+
expectNotToBeCase: expectNotToBeCase,
78+
expectToEqualCase: expectToEqualCase,
79+
expectNotToEqualCase: expectNotToEqualCase,
80+
expectToBeUndefinedCase: expectToBeUndefinedCase,
81+
expectNotToBeUndefinedCase: expectNotToBeUndefinedCase,
82+
};

0 commit comments

Comments
 (0)