-
Notifications
You must be signed in to change notification settings - Fork 227
/
prefer_to_have_length.js
43 lines (40 loc) · 1.21 KB
/
prefer_to_have_length.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'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 argumentObject = node.arguments[0].object;
const argumentProperty = node.arguments[0].property;
if (propertyName === 'toBe' && argumentProperty.name === 'length') {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
argumentObject,
argumentProperty,
token => token.value === '.'
);
context.report({
fix(fixer) {
return [
fixer.remove(propertyDot),
fixer.remove(argumentProperty),
fixer.replaceText(parentProperty, 'toHaveLength'),
];
},
message: 'Use toHaveLength() instead',
node: parentProperty,
});
}
}
},
};
};