/
ast.js
183 lines (146 loc) · 4.96 KB
/
ast.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const { complement, both, isNil, propEq, pathEq, find } = require('rambda');
const { getTestCaseNames, getSuiteNames } = require('./names');
const { getAddtionalNames } = require('./settings');
const isDefined = complement(isNil);
const isCallExpression = both(isDefined, propEq('type', 'CallExpression'));
const hooks = [
'before',
'after',
'beforeEach',
'afterEach',
'beforeAll',
'afterAll',
'setup',
'teardown',
'suiteSetup',
'suiteTeardown'
];
const suiteConfig = [ 'timeout', 'slow', 'retries' ];
const findReturnStatement = find(propEq('type', 'ReturnStatement'));
function getPropertyName(property) {
return property.name || property.value;
}
function getNodeName(node) {
if (node.type === 'ThisExpression') {
return 'this';
}
if (node.type === 'CallExpression') {
return `${getNodeName(node.callee)}()`;
}
if (node.type === 'MemberExpression') {
return `${getNodeName(node.object)}.${getPropertyName(node.property)}`;
}
return node.name;
}
function isHookIdentifier(node) {
return node && node.type === 'Identifier' && hooks.includes(node.name);
}
function isHookCall(node) {
return isCallExpression(node) && isHookIdentifier(node.callee);
}
function findReference(scope, node) {
const hasSameRangeAsNode = pathEq([ 'identifier', 'range' ], node.range);
return find(hasSameRangeAsNode, scope.references);
}
function isShadowed(scope, identifier) {
const reference = findReference(scope, identifier);
return (
reference && reference.resolved && reference.resolved.defs.length > 0
);
}
function isCallToShadowedReference(node, scope) {
const identifier =
node.callee.type === 'MemberExpression' ?
node.callee.object :
node.callee;
return isShadowed(scope, identifier);
}
function isFunctionCallWithName(node, names) {
return isCallExpression(node) && names.includes(getNodeName(node.callee));
}
// eslint-disable-next-line max-statements
function createAstUtils(settings) {
const additionalCustomNames = getAddtionalNames(settings);
function buildIsDescribeAnswerer(options = {}) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const describeAliases = getSuiteNames({
modifiersOnly,
modifiers,
additionalCustomNames
});
return (node) => isFunctionCallWithName(node, describeAliases);
}
function isDescribe(node, options = {}) {
return buildIsDescribeAnswerer(options)(node);
}
function buildIsTestCaseAnswerer(options = {}) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const testCaseNames = getTestCaseNames({
modifiersOnly,
modifiers,
additionalCustomNames
});
return (node) => isFunctionCallWithName(node, testCaseNames);
}
function isTestCase(node, options = {}) {
return buildIsTestCaseAnswerer(options)(node);
}
function isSuiteConfigExpression(node) {
if (node.type !== 'MemberExpression') {
return false;
}
const usingThis = node.object.type === 'ThisExpression';
if (usingThis || isTestCase(node.object)) {
return suiteConfig.includes(getPropertyName(node.property));
}
return false;
}
function isSuiteConfigCall(node) {
return isCallExpression(node) && isSuiteConfigExpression(node.callee);
}
function buildIsMochaFunctionCallAnswerer(_isTestCase, _isDescribe) {
function isMochaFunctionCall(node) {
return _isTestCase(node) || _isDescribe(node) || isHookCall(node);
}
return (node, context) => {
if (isMochaFunctionCall(node)) {
const scope = context.sourceCode.getScope(node);
if (!isCallToShadowedReference(node, scope)) {
return true;
}
}
return false;
};
}
function hasParentMochaFunctionCall(functionExpression, options) {
return (
isTestCase(functionExpression.parent, options) ||
isHookCall(functionExpression.parent)
);
}
function isExplicitUndefined(node) {
return node && node.type === 'Identifier' && node.name === 'undefined';
}
function isReturnOfUndefined(node) {
const argument = node.argument;
const isImplicitUndefined = argument === null;
return isImplicitUndefined || isExplicitUndefined(argument);
}
return {
isDescribe,
isHookIdentifier,
isTestCase,
getPropertyName,
getNodeName,
isHookCall,
isSuiteConfigCall,
hasParentMochaFunctionCall,
findReturnStatement,
isReturnOfUndefined,
buildIsDescribeAnswerer,
buildIsTestCaseAnswerer,
buildIsMochaFunctionCallAnswerer
};
}
module.exports = createAstUtils;