-
Notifications
You must be signed in to change notification settings - Fork 62
/
ast.js
97 lines (76 loc) · 2.68 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
'use strict';
const R = require('ramda');
const isDefined = R.complement(R.isNil);
const isCallExpression = R.both(isDefined, R.propEq('type', 'CallExpression'));
const describeAliases = [
'describe', 'xdescribe', 'describe.only', 'describe.skip',
'context', 'xcontext', 'context.only', 'context.skip',
'suite', 'xsuite', 'suite.only', 'suite.skip'
];
const hooks = [ 'before', 'after', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll' ];
const testCaseNames = [
'it', 'it.only', 'it.skip', 'xit',
'test', 'test.only', 'test.skip',
'specify', 'specify.only', 'specify.skip', 'xspecify'
];
function getPropertyName(property) {
return property.name || property.value;
}
function getNodeName(node) {
if (node.type === 'MemberExpression') {
return `${getNodeName(node.object) }.${ getPropertyName(node.property)}`;
}
return node.name;
}
function isDescribe(node, additionalSuiteNames) {
return isCallExpression(node) &&
describeAliases.concat(additionalSuiteNames).indexOf(getNodeName(node.callee)) > -1;
}
function isHookIdentifier(node) {
return node &&
node.type === 'Identifier' &&
hooks.indexOf(node.name) !== -1;
}
function isHookCall(node) {
return isCallExpression(node) && isHookIdentifier(node.callee);
}
function isTestCase(node) {
return isCallExpression(node) && testCaseNames.indexOf(getNodeName(node.callee)) > -1;
}
function findReference(scope, node) {
const hasSameRangeAsNode = R.pathEq([ 'identifier', 'range' ], node.range);
return R.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 isMochaFunctionCall(node, scope) {
if (isCallToShadowedReference(node, scope)) {
return false;
}
return isTestCase(node) || isDescribe(node) || isHookCall(node);
}
function isStringLiteral(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}
function hasParentMochaFunctionCall(functionExpression) {
return isTestCase(functionExpression.parent) || isHookCall(functionExpression.parent);
}
const findReturnStatement = R.find(R.propEq('type', 'ReturnStatement'));
module.exports = {
isDescribe,
isHookIdentifier,
isTestCase,
getPropertyName,
getNodeName,
isMochaFunctionCall,
isHookCall,
isStringLiteral,
hasParentMochaFunctionCall,
findReturnStatement
};