1 | 'use strict';
|
2 |
|
3 | const { getDocsUrl, getNodeName, scopeHasLocalReference } = require('./util');
|
4 |
|
5 | module.exports = {
|
6 | meta: {
|
7 | docs: {
|
8 | url: getDocsUrl(__filename),
|
9 | },
|
10 | fixable: 'code',
|
11 | messages: {
|
12 | illegalGlobal:
|
13 | 'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`',
|
14 | illegalMethod:
|
15 | 'Illegal usage of `{{ method }}`, prefer `{{ replacement }}`',
|
16 | illegalFail:
|
17 | 'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback',
|
18 | illegalPending:
|
19 | 'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`',
|
20 | illegalJasmine: 'Illegal usage of jasmine global',
|
21 | },
|
22 | },
|
23 | create(context) {
|
24 | return {
|
25 | CallExpression(node) {
|
26 | const calleeName = getNodeName(node.callee);
|
27 |
|
28 | if (!calleeName) {
|
29 | return;
|
30 | }
|
31 | if (
|
32 | calleeName === 'spyOn' ||
|
33 | calleeName === 'spyOnProperty' ||
|
34 | calleeName === 'fail' ||
|
35 | calleeName === 'pending'
|
36 | ) {
|
37 | if (scopeHasLocalReference(context.getScope(), calleeName)) {
|
38 |
|
39 | return;
|
40 | }
|
41 |
|
42 | switch (calleeName) {
|
43 | case 'spyOn':
|
44 | case 'spyOnProperty':
|
45 | context.report({
|
46 | node,
|
47 | messageId: 'illegalGlobal',
|
48 | data: { global: calleeName, replacement: 'jest.spyOn' },
|
49 | });
|
50 | break;
|
51 | case 'fail':
|
52 | context.report({
|
53 | node,
|
54 | messageId: 'illegalFail',
|
55 | });
|
56 | break;
|
57 | case 'pending':
|
58 | context.report({
|
59 | node,
|
60 | messageId: 'illegalPending',
|
61 | });
|
62 | break;
|
63 | }
|
64 | return;
|
65 | }
|
66 |
|
67 | if (calleeName.startsWith('jasmine.')) {
|
68 | const functionName = calleeName.replace('jasmine.', '');
|
69 |
|
70 | if (
|
71 | functionName === 'any' ||
|
72 | functionName === 'anything' ||
|
73 | functionName === 'arrayContaining' ||
|
74 | functionName === 'objectContaining' ||
|
75 | functionName === 'stringMatching'
|
76 | ) {
|
77 | context.report({
|
78 | fix(fixer) {
|
79 | return [fixer.replaceText(node.callee.object, 'expect')];
|
80 | },
|
81 | node,
|
82 | messageId: 'illegalMethod',
|
83 | data: {
|
84 | method: calleeName,
|
85 | replacement: `expect.${functionName}`,
|
86 | },
|
87 | });
|
88 | return;
|
89 | }
|
90 |
|
91 | if (functionName === 'addMatchers') {
|
92 | context.report({
|
93 | node,
|
94 | messageId: 'illegalMethod',
|
95 | data: {
|
96 | method: calleeName,
|
97 | replacement: `expect.extend`,
|
98 | },
|
99 | });
|
100 | return;
|
101 | }
|
102 |
|
103 | if (functionName === 'createSpy') {
|
104 | context.report({
|
105 | node,
|
106 | messageId: 'illegalMethod',
|
107 | data: {
|
108 | method: calleeName,
|
109 | replacement: 'jest.fn',
|
110 | },
|
111 | });
|
112 | return;
|
113 | }
|
114 |
|
115 | context.report({
|
116 | node,
|
117 | messageId: 'illegalJasmine',
|
118 | });
|
119 | }
|
120 | },
|
121 | MemberExpression(node) {
|
122 | if (node.object.name === 'jasmine') {
|
123 | if (node.parent.type === 'AssignmentExpression') {
|
124 | if (node.property.name === 'DEFAULT_TIMEOUT_INTERVAL') {
|
125 | context.report({
|
126 | fix(fixer) {
|
127 | return [
|
128 | fixer.replaceText(
|
129 | node.parent,
|
130 | `jest.setTimeout(${node.parent.right.value})`
|
131 | ),
|
132 | ];
|
133 | },
|
134 | node,
|
135 | message: 'Illegal usage of jasmine global',
|
136 | });
|
137 | return;
|
138 | }
|
139 |
|
140 | context.report({
|
141 | node,
|
142 | message: 'Illegal usage of jasmine global',
|
143 | });
|
144 | }
|
145 | }
|
146 | },
|
147 | };
|
148 | },
|
149 | };
|