-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.js
99 lines (87 loc) · 2.7 KB
/
index.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
const { VEST_KEYWORD, VEST_IDENTIFIER_TEST } = require('../../constants');
const { closest } = require('../../helpers');
const { isTestCall, looksLikeExclusion, errorMessage } = require('./helpers');
module.exports = {
meta: {
docs: {
category: 'Possible Errors',
description:
'Makes sure vest exclusion hooks are not put before your test calls',
recommended: true,
type: 'problem',
},
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
if (!looksLikeExclusion(node.callee)) {
return;
}
const { name } = node.callee.property;
const scope = context.getScope();
const res = scope.references.reduce(
(accumulator, reference) => {
const { identifier } = reference;
// Shouldn't really happen
if (!identifier) {
return accumulator;
}
// Checks if current reference is a reference we care about.
if (
identifier.name !== VEST_KEYWORD &&
identifier.name !== VEST_IDENTIFIER_TEST
) {
return accumulator;
}
// If current reference is the first `test` call
// Sets `firstTest` to current index so we know not
// To allow any more exclusion hooks
if (isTestCall(identifier)) {
return {
...accumulator,
firstTest: accumulator.firstTest
? accumulator.firstTest
: identifier,
};
}
// If the current reference belongs to the current node
// And `test` was already called, mark shouldWarn as true.
if (accumulator.firstTest) {
return {
...accumulator,
shouldWarn: true,
};
}
// All good. Carry on.
return accumulator;
},
{
firstTest: null,
shouldWarn: false,
}
);
// No warnings.
if (!res.shouldWarn) {
return;
}
const callExpression = closest(node, 'ExpressionStatement');
context.report({
node: callExpression,
message: errorMessage(name),
fix(fixer) {
const sourceCode = context.getSourceCode();
const text = sourceCode.getText(callExpression);
return [
fixer.remove(callExpression),
fixer.insertTextBefore(
closest(res.firstTest, 'ExpressionStatement'),
`${text}\n`
),
];
},
});
},
};
},
};