Skip to content

Commit 0276985

Browse files
authored
refactor(expect-expect): use ESQuery selector syntax (#171)
1 parent be29fba commit 0276985

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

rules/expect-expect.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,25 @@ module.exports = {
2626
],
2727
},
2828
create(context) {
29-
// variables should be defined here
3029
const unchecked = [];
3130
const assertFunctionNames =
3231
context.options[0] && context.options[0].assertFunctionNames
3332
? context.options[0].assertFunctionNames
3433
: ['expect'];
3534

36-
//----------------------------------------------------------------------
37-
// Helpers
38-
//----------------------------------------------------------------------
39-
const isExpectCall = node =>
40-
// if we're not calling a function, ignore
41-
node.type === 'CallExpression' &&
42-
// if we're not calling allowed assertion
43-
assertFunctionNames.some(name => name === node.callee.name);
44-
//----------------------------------------------------------------------
45-
// Public
46-
//----------------------------------------------------------------------
4735
return {
48-
// give me methods
49-
CallExpression(node) {
50-
// keep track of `it` calls
51-
if (['it', 'test'].indexOf(node.callee.name) > -1) {
52-
unchecked.push(node);
53-
return;
54-
}
55-
if (!isExpectCall(node)) {
56-
return;
57-
}
58-
// here, we do have a call to expect
59-
// use `some` to return early (in case of nested `it`s
60-
context.getAncestors().some(ancestor => {
36+
'CallExpression[callee.name=/^it|test$/]'(node) {
37+
unchecked.push(node);
38+
},
39+
[`CallExpression[callee.name=/^${assertFunctionNames.join('|')}$/]`]() {
40+
// Return early in case of nested `it` statements.
41+
for (const ancestor of context.getAncestors()) {
6142
const index = unchecked.indexOf(ancestor);
6243
if (index !== -1) {
6344
unchecked.splice(index, 1);
64-
return true;
45+
break;
6546
}
66-
return false;
67-
});
47+
}
6848
},
6949
'Program:exit'() {
7050
unchecked.forEach(node =>

0 commit comments

Comments
 (0)