diff --git a/lib/rules/no-mocha-arrows.js b/lib/rules/no-mocha-arrows.js index c09aacf..07d56a3 100644 --- a/lib/rules/no-mocha-arrows.js +++ b/lib/rules/no-mocha-arrows.js @@ -69,6 +69,7 @@ function fixArrowFunction(fixer, sourceCode, fn) { `${formatFunctionHead(sourceCode, fn)}{ return ${bodyText}; }` ); } + module.exports = { meta: { type: 'suggestion', @@ -81,12 +82,18 @@ module.exports = { create(context) { const astUtils = createAstUtils(context.settings); const sourceCode = context.getSourceCode(); + const isTestCase = astUtils.buildIsTestCaseAnswerer(); + const isDescribe = astUtils.buildIsDescribeAnswerer(); + const isMochaFunctionCall = astUtils.buildIsMochaFunctionCallAnswerer( + isTestCase, + isDescribe + ); return { CallExpression(node) { const name = astUtils.getNodeName(node.callee); - if (astUtils.isMochaFunctionCall(node, context.getScope())) { + if (isMochaFunctionCall(node, context.getScope())) { const fnArg = node.arguments.slice(-1)[0]; if (fnArg && fnArg.type === 'ArrowFunctionExpression') { context.report({ diff --git a/lib/util/ast.js b/lib/util/ast.js index 95fe823..37ffa55 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -107,6 +107,16 @@ function createAstUtils(settings) { return isCallExpression(node) && isSuiteConfigExpression(node.callee); } + function buildIsMochaFunctionCallAnswerer(_isTestCase, _isDescribe) { + return (node, scope) => { + if (isCallToShadowedReference(node, scope)) { + return false; + } + + return _isTestCase(node) || _isDescribe(node) || isHookCall(node); + }; + } + function isMochaFunctionCall(node, scope) { if (isCallToShadowedReference(node, scope)) { return false; @@ -143,7 +153,8 @@ function createAstUtils(settings) { findReturnStatement, isReturnOfUndefined, buildIsDescribeAnswerer, - buildIsTestCaseAnswerer + buildIsTestCaseAnswerer, + buildIsMochaFunctionCallAnswerer }; }