Skip to content

Commit

Permalink
Introduce buildIsMochaFunctionCallAnswerer
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed May 26, 2021
1 parent 9f0bb3f commit 183e19d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/rules/no-mocha-arrows.js
Expand Up @@ -69,6 +69,7 @@ function fixArrowFunction(fixer, sourceCode, fn) {
`${formatFunctionHead(sourceCode, fn)}{ return ${bodyText}; }`
);
}

module.exports = {
meta: {
type: 'suggestion',
Expand All @@ -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({
Expand Down
13 changes: 12 additions & 1 deletion lib/util/ast.js
Expand Up @@ -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;
Expand Down Expand Up @@ -143,7 +153,8 @@ function createAstUtils(settings) {
findReturnStatement,
isReturnOfUndefined,
buildIsDescribeAnswerer,
buildIsTestCaseAnswerer
buildIsTestCaseAnswerer,
buildIsMochaFunctionCallAnswerer
};
}

Expand Down

0 comments on commit 183e19d

Please sign in to comment.