-
Notifications
You must be signed in to change notification settings - Fork 228
/
prefer-to-contain.ts
125 lines (112 loc) · 3.62 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
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type CallExpressionWithSingleArgument,
EqualityMatcher,
type KnownCallExpression,
ModifierName,
createRule,
getAccessorValue,
getFirstMatcherArg,
getSourceCode,
hasOnlyOneArgument,
isBooleanLiteral,
isSupportedAccessor,
parseJestFnCall,
} from './utils';
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) &&
node.arguments[0].type !== AST_NODE_TYPES.SpreadElement;
// 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) {
const jestFnCall = parseJestFnCall(node, context);
if (jestFnCall?.type !== 'expect' || jestFnCall.args.length === 0) {
return;
}
const { parent: expect } = jestFnCall.head.node;
if (expect?.type !== AST_NODE_TYPES.CallExpression) {
return;
}
const {
arguments: [includesCall],
range: [, expectCallEnd],
} = expect;
const { matcher } = jestFnCall;
const matcherArg = getFirstMatcherArg(jestFnCall);
if (
!includesCall ||
matcherArg.type === AST_NODE_TYPES.SpreadElement ||
!EqualityMatcher.hasOwnProperty(getAccessorValue(matcher)) ||
!isBooleanLiteral(matcherArg) ||
!isFixableIncludesCallExpression(includesCall)
) {
return;
}
const hasNot = jestFnCall.modifiers.some(
nod => getAccessorValue(nod) === 'not',
);
context.report({
fix(fixer) {
const sourceCode = getSourceCode(context);
// we need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
const addNotModifier = matcherArg.value === hasNot;
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.parent.range[1]],
addNotModifier
? `.${ModifierName.not}.toContain`
: '.toContain',
),
// replace the matcher argument with the value from the "includes"
fixer.replaceText(
jestFnCall.args[0],
sourceCode.getText(includesCall.arguments[0]),
),
];
},
messageId: 'useToContain',
node: matcher,
});
},
};
},
});