Skip to content

Commit be29fba

Browse files
authored
refactor(prefer-expect-assertions): use ESQuery selectors (#170)
1 parent 6fa003b commit be29fba

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

rules/prefer-expect-assertions.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ const isExpectAssertionsOrHasAssertionsCall = expression => {
3131
}
3232
};
3333

34-
const isTestOrItFunction = node => {
35-
return (
36-
node.type === 'CallExpression' &&
37-
node.callee &&
38-
(node.callee.name === 'it' || node.callee.name === 'test')
39-
);
40-
};
41-
4234
const getFunctionFirstLine = functionBody => {
4335
return functionBody[0] && functionBody[0].expression;
4436
};
@@ -47,14 +39,6 @@ const isFirstLineExprStmt = functionBody => {
4739
return functionBody[0] && functionBody[0].type === 'ExpressionStatement';
4840
};
4941

50-
const getTestFunctionBody = node => {
51-
try {
52-
return node.arguments[1].body.body;
53-
} catch (e) {
54-
return undefined;
55-
}
56-
};
57-
5842
const reportMsg = (context, node) => {
5943
context.report({
6044
message: ruleMsg,
@@ -70,18 +54,15 @@ module.exports = {
7054
},
7155
create(context) {
7256
return {
73-
CallExpression(node) {
74-
if (isTestOrItFunction(node)) {
75-
const testFuncBody = getTestFunctionBody(node);
76-
if (testFuncBody) {
77-
if (!isFirstLineExprStmt(testFuncBody)) {
78-
reportMsg(context, node);
79-
} else {
80-
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
81-
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
82-
reportMsg(context, node);
83-
}
84-
}
57+
'CallExpression[callee.name=/^it|test$/][arguments.1.body.body]'(node) {
58+
const testFuncBody = node.arguments[1].body.body;
59+
60+
if (!isFirstLineExprStmt(testFuncBody)) {
61+
reportMsg(context, node);
62+
} else {
63+
const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
64+
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
65+
reportMsg(context, node);
8566
}
8667
}
8768
},

0 commit comments

Comments
 (0)