/
prefer-mock-promise-shorthand.ts
129 lines (113 loc) · 3.44 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
129
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import {
AccessorNode,
FunctionExpression,
createRule,
getAccessorValue,
getNodeName,
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: {
category: 'Best Practices',
description: 'Prefer mock resolved/rejected shorthands for promises',
recommended: false,
},
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 = context.getSourceCode();
// 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),
);
}
},
};
},
});