-
Notifications
You must be signed in to change notification settings - Fork 234
/
prefer-mock-promise-shorthand.ts
128 lines (112 loc) · 3.42 KB
/
prefer-mock-promise-shorthand.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
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type AccessorNode,
type FunctionExpression,
createRule,
getAccessorValue,
getNodeName,
getSourceCode,
isFunction,
isSupportedAccessor,
} from './utils';
const withOnce = (name: string, addOnce: boolean): string => {
return `${name}${addOnce ? 'Once' : ''}`;
};
const findSingleReturnArgumentNode = (
fnNode: FunctionExpression,
): TSESTree.Expression | null => {
if (fnNode.body.type !== AST_NODE_TYPES.BlockStatement) {
return fnNode.body;
}
if (fnNode.body.body[0]?.type === AST_NODE_TYPES.ReturnStatement) {
return fnNode.body.body[0].argument;
}
return null;
};
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Prefer mock resolved/rejected shorthands for promises',
},
messages: {
useMockShorthand: 'Prefer {{ replacement }}',
},
schema: [],
type: 'suggestion',
fixable: 'code',
},
defaultOptions: [],
create(context) {
const report = (
property: AccessorNode,
isOnce: boolean,
outerArgNode: TSESTree.Node,
innerArgNode: TSESTree.Node | null = outerArgNode,
) => {
if (innerArgNode?.type !== AST_NODE_TYPES.CallExpression) {
return;
}
const argName = getNodeName(innerArgNode);
if (argName !== 'Promise.resolve' && argName !== 'Promise.reject') {
return;
}
const replacement = withOnce(
argName.endsWith('reject') ? 'mockRejectedValue' : 'mockResolvedValue',
isOnce,
);
context.report({
node: property,
messageId: 'useMockShorthand',
data: { replacement },
fix(fixer) {
const sourceCode = getSourceCode(context);
// there shouldn't be more than one argument, but if there is don't try
// fixing since we have no idea what to do with the extra arguments
if (innerArgNode.arguments.length > 1) {
return null;
}
return [
fixer.replaceText(property, replacement),
fixer.replaceText(
outerArgNode,
// the value argument for both Promise methods is optional,
// whereas for Jest they're required so use an explicit undefined
// if no argument is being passed to the call we're replacing
innerArgNode.arguments.length === 1
? sourceCode.getText(innerArgNode.arguments[0])
: 'undefined',
),
];
},
});
};
return {
CallExpression(node) {
if (
node.callee.type !== AST_NODE_TYPES.MemberExpression ||
!isSupportedAccessor(node.callee.property) ||
node.arguments.length === 0
) {
return;
}
const mockFnName = getAccessorValue(node.callee.property);
const isOnce = mockFnName.endsWith('Once');
if (mockFnName === withOnce('mockReturnValue', isOnce)) {
report(node.callee.property, isOnce, node.arguments[0]);
} else if (mockFnName === withOnce('mockImplementation', isOnce)) {
const [arg] = node.arguments;
if (!isFunction(arg) || arg.params.length !== 0) {
return;
}
report(
node.callee.property,
isOnce,
arg,
findSingleReturnArgumentNode(arg),
);
}
},
};
},
});