From be29fba2b8c8788cb8aee8dddb0eadbe8fe2070b Mon Sep 17 00:00:00 2001 From: mackie Date: Tue, 2 Oct 2018 15:32:24 -0700 Subject: [PATCH] refactor(prefer-expect-assertions): use ESQuery selectors (#170) --- rules/prefer-expect-assertions.js | 37 ++++++++----------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/rules/prefer-expect-assertions.js b/rules/prefer-expect-assertions.js index c90e56151..cac04f9a9 100644 --- a/rules/prefer-expect-assertions.js +++ b/rules/prefer-expect-assertions.js @@ -31,14 +31,6 @@ const isExpectAssertionsOrHasAssertionsCall = expression => { } }; -const isTestOrItFunction = node => { - return ( - node.type === 'CallExpression' && - node.callee && - (node.callee.name === 'it' || node.callee.name === 'test') - ); -}; - const getFunctionFirstLine = functionBody => { return functionBody[0] && functionBody[0].expression; }; @@ -47,14 +39,6 @@ const isFirstLineExprStmt = functionBody => { return functionBody[0] && functionBody[0].type === 'ExpressionStatement'; }; -const getTestFunctionBody = node => { - try { - return node.arguments[1].body.body; - } catch (e) { - return undefined; - } -}; - const reportMsg = (context, node) => { context.report({ message: ruleMsg, @@ -70,18 +54,15 @@ module.exports = { }, create(context) { return { - CallExpression(node) { - if (isTestOrItFunction(node)) { - const testFuncBody = getTestFunctionBody(node); - if (testFuncBody) { - if (!isFirstLineExprStmt(testFuncBody)) { - reportMsg(context, node); - } else { - const testFuncFirstLine = getFunctionFirstLine(testFuncBody); - if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) { - reportMsg(context, node); - } - } + 'CallExpression[callee.name=/^it|test$/][arguments.1.body.body]'(node) { + const testFuncBody = node.arguments[1].body.body; + + if (!isFirstLineExprStmt(testFuncBody)) { + reportMsg(context, node); + } else { + const testFuncFirstLine = getFunctionFirstLine(testFuncBody); + if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) { + reportMsg(context, node); } } },