/
util.js
152 lines (123 loc) · 4.64 KB
/
util.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { basename } from 'path';
import { version } from '../../package.json';
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
export const expectCase = node =>
node && node.callee && node.callee.name === 'expect';
export const expectCaseWithParent = node =>
expectCase(node) &&
node.parent &&
node.parent.type === 'MemberExpression' &&
node.parent.parent;
export const expectNotCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'not';
export const expectResolvesCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'resolves';
export const expectNotResolvesCase = node =>
expectNotCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node.parent) === 'resolves';
export const expectRejectsCase = node =>
expectCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node) === 'rejects';
export const expectNotRejectsCase = node =>
expectNotCase(node) &&
node.parent.parent.type === 'MemberExpression' &&
methodName(node.parent) === 'rejects';
export const expectToBeCase = (node, arg) =>
!(
expectNotCase(node) ||
expectResolvesCase(node) ||
expectRejectsCase(node)
) &&
expectCase(node) &&
methodName(node) === 'toBe' &&
argument(node) &&
((argument(node).type === 'Literal' &&
argument(node).value === null &&
arg === null) ||
(argument(node).name === 'undefined' && arg === undefined));
export const expectNotToBeCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toBe' &&
argument2(node) &&
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));
export const expectToEqualCase = (node, arg) =>
!(
expectNotCase(node) ||
expectResolvesCase(node) ||
expectRejectsCase(node)
) &&
expectCase(node) &&
methodName(node) === 'toEqual' &&
argument(node) &&
((argument(node).type === 'Literal' &&
argument(node).value === null &&
arg === null) ||
(argument(node).name === 'undefined' && arg === undefined));
export const expectNotToEqualCase = (node, arg) =>
expectNotCase(node) &&
methodName2(node) === 'toEqual' &&
argument2(node) &&
((argument2(node).type === 'Literal' &&
argument2(node).value === null &&
arg === null) ||
(argument2(node).name === 'undefined' && arg === undefined));
export const method = node => node.parent.property;
export const method2 = node => node.parent.parent.property;
const methodName = node => method(node).name;
const methodName2 = node => method2(node).name;
export const argument = node =>
node.parent.parent.arguments && node.parent.parent.arguments[0];
export const argument2 = node =>
node.parent.parent.parent.arguments && node.parent.parent.parent.arguments[0];
const describeAliases = new Set(['describe', 'fdescribe', 'xdescribe']);
const testCaseNames = new Set(['fit', 'it', 'test', 'xit', 'xtest']);
export const isTestCase = node =>
node &&
node.type === 'CallExpression' &&
((node.callee.type === 'Identifier' && testCaseNames.has(node.callee.name)) ||
(node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
testCaseNames.has(node.callee.object.name)));
export const isDescribe = node =>
node &&
node.type === 'CallExpression' &&
((node.callee.type === 'Identifier' &&
describeAliases.has(node.callee.name)) ||
(node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
describeAliases.has(node.callee.object.name)));
export const isFunction = node =>
node &&
(node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression');
export const isString = node =>
node &&
((node.type === 'Literal' && typeof node.value === 'string') ||
isTemplateLiteral(node));
export const isTemplateLiteral = node =>
node && node.type === 'TemplateLiteral';
export const hasExpressions = node =>
node && node.expressions && node.expressions.length > 0;
export const getStringValue = arg =>
isTemplateLiteral(arg) ? arg.quasis[0].value.raw : arg.value;
/**
* Generates the URL to documentation for the given rule name. It uses the
* package version to build the link to a tagged version of the
* documentation file.
*
* @param {string} filename - Name of the eslint rule
* @returns {string} URL to the documentation for the given rule
*/
export const getDocsUrl = filename => {
const ruleName = basename(filename, '.js');
return `${REPO_URL}/blob/v${version}/docs/rules/${ruleName}.md`;
};