-
Notifications
You must be signed in to change notification settings - Fork 227
/
no-alias-methods.ts
68 lines (62 loc) · 1.88 KB
/
no-alias-methods.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
import { createRule, isExpectCall, parseExpectCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow alias methods',
recommended: 'warn',
},
messages: {
replaceAlias: `Replace {{ replace }}() with its canonical name of {{ canonical }}()`,
},
fixable: 'code',
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
// The Jest methods which have aliases. The canonical name is the first
// index of each item.
// todo: replace w/ Map
const methodNames = [
['toHaveBeenCalled', 'toBeCalled'],
['toHaveBeenCalledTimes', 'toBeCalledTimes'],
['toHaveBeenCalledWith', 'toBeCalledWith'],
['toHaveBeenLastCalledWith', 'lastCalledWith'],
['toHaveBeenNthCalledWith', 'nthCalledWith'],
['toHaveReturned', 'toReturn'],
['toHaveReturnedTimes', 'toReturnTimes'],
['toHaveReturnedWith', 'toReturnWith'],
['toHaveLastReturnedWith', 'lastReturnedWith'],
['toHaveNthReturnedWith', 'nthReturnedWith'],
['toThrow', 'toThrowError'],
];
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}
const { matcher } = parseExpectCall(node);
if (!matcher) {
return;
}
// Check if the method used matches any of ours
const methodItem = methodNames.find(item => item[1] === matcher.name);
if (methodItem) {
context.report({
messageId: 'replaceAlias',
data: {
replace: methodItem[1],
canonical: methodItem[0],
},
node: matcher.node.property,
fix: fixer => [
fixer.replaceText(matcher.node.property, methodItem[0]),
],
});
}
},
};
},
});