UNPKG

1.87 kBJavaScriptView Raw
1'use strict';
2
3const { expectCase, getDocsUrl, method } = require('./util');
4
5module.exports = {
6 meta: {
7 docs: {
8 url: getDocsUrl(__filename),
9 },
10 fixable: 'code',
11 },
12 create(context) {
13 // The Jest methods which have aliases. The canonical name is the first
14 // index of each item.
15 const methodNames = [
16 ['toHaveBeenCalled', 'toBeCalled'],
17 ['toHaveBeenCalledTimes', 'toBeCalledTimes'],
18 ['toHaveBeenCalledWith', 'toBeCalledWith'],
19 ['toHaveBeenLastCalledWith', 'lastCalledWith'],
20 ['toHaveBeenNthCalledWith', 'nthCalledWith'],
21 ['toHaveReturned', 'toReturn'],
22 ['toHaveReturnedTimes', 'toReturnTimes'],
23 ['toHaveReturnedWith', 'toReturnWith'],
24 ['toHaveLastReturnedWith', 'lastReturnedWith'],
25 ['toHaveNthReturnedWith', 'nthReturnedWith'],
26 ['toThrow', 'toThrowError'],
27 ];
28
29 return {
30 CallExpression(node) {
31 if (!expectCase(node)) {
32 return;
33 }
34
35 let targetNode = method(node);
36 if (
37 targetNode.name === 'resolves' ||
38 targetNode.name === 'rejects' ||
39 targetNode.name === 'not'
40 ) {
41 targetNode = method(node.parent);
42 }
43
44 if (!targetNode) {
45 return;
46 }
47
48 // Check if the method used matches any of ours
49 const methodItem = methodNames.find(
50 item => item[1] === targetNode.name
51 );
52
53 if (methodItem) {
54 context.report({
55 message: `Replace {{ replace }}() with its canonical name of {{ canonical }}()`,
56 data: {
57 replace: methodItem[1],
58 canonical: methodItem[0],
59 },
60 node: targetNode,
61 fix(fixer) {
62 return [fixer.replaceText(targetNode, methodItem[0])];
63 },
64 });
65 }
66 },
67 };
68 },
69};