-
Notifications
You must be signed in to change notification settings - Fork 226
/
prefer-to-contain.ts
146 lines (135 loc) · 4.08 KB
/
prefer-to-contain.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
CallExpressionWithSingleArgument,
KnownCallExpression,
MaybeTypeCast,
ModifierName,
ParsedEqualityMatcherCall,
ParsedExpectMatcher,
createRule,
followTypeAssertionChain,
hasOnlyOneArgument,
isExpectCall,
isParsedEqualityMatcherCall,
isSupportedAccessor,
parseExpectCall,
} from './utils';
const isBooleanLiteral = (
node: TSESTree.Node,
): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
type ParsedBooleanEqualityMatcherCall = ParsedEqualityMatcherCall<
MaybeTypeCast<TSESTree.BooleanLiteral>
>;
/**
* Checks if the given `ParsedExpectMatcher` is a call to one of the equality matchers,
* with a boolean literal as the sole argument.
*
* @example javascript
* toBe(true);
* toEqual(false);
*
* @param {ParsedExpectMatcher} matcher
*
* @return {matcher is ParsedBooleanEqualityMatcher}
*/
const isBooleanEqualityMatcher = (
matcher: ParsedExpectMatcher,
): matcher is ParsedBooleanEqualityMatcherCall =>
isParsedEqualityMatcherCall(matcher) &&
isBooleanLiteral(followTypeAssertionChain(matcher.arguments[0]));
type FixableIncludesCallExpression = KnownCallExpression<'includes'> &
CallExpressionWithSingleArgument;
/**
* Checks if the given `node` is a `CallExpression` representing the calling
* of an `includes`-like method that can be 'fixed' (using `toContain`).
*
* @param {CallExpression} node
*
* @return {node is FixableIncludesCallExpression}
*/
const isFixableIncludesCallExpression = (
node: TSESTree.Node,
): node is FixableIncludesCallExpression =>
node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property, 'includes') &&
hasOnlyOneArgument(node);
// expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>)
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `toContain()`',
recommended: false,
},
messages: {
useToContain: 'Use toContain() instead',
},
fixable: 'code',
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}
const {
expect: {
arguments: [includesCall],
range: [, expectCallEnd],
},
matcher,
modifier,
} = parseExpectCall(node);
if (
!matcher ||
!includesCall ||
(modifier && modifier.name !== ModifierName.not) ||
!isBooleanEqualityMatcher(matcher) ||
!isFixableIncludesCallExpression(includesCall)
) {
return;
}
context.report({
fix(fixer) {
const sourceCode = context.getSourceCode();
// we need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
const addNotModifier =
followTypeAssertionChain(matcher.arguments[0]).value ===
!!modifier;
return [
// remove the "includes" call entirely
fixer.removeRange([
includesCall.callee.property.range[0] - 1,
includesCall.range[1],
]),
// replace the current matcher with "toContain", adding "not" if needed
fixer.replaceTextRange(
[expectCallEnd, matcher.node.range[1]],
addNotModifier
? `.${ModifierName.not}.toContain`
: '.toContain',
),
// replace the matcher argument with the value from the "includes"
fixer.replaceText(
matcher.arguments[0],
sourceCode.getText(includesCall.arguments[0]),
),
];
},
messageId: 'useToContain',
node: (modifier || matcher).node.property,
});
},
};
},
});