UNPKG

1.51 kBJavaScriptView Raw
1'use strict';
2
3const find = require('ramda/src/find');
4const astUtils = require('../util/ast');
5
6module.exports = function (context) {
7 function isAsyncFunction(functionExpression) {
8 return functionExpression.params.length === 1;
9 }
10
11 function findParamInScope(paramName, scope) {
12 return find(function (variable) {
13 return variable.name === paramName && variable.defs[0].type === 'Parameter';
14 }, scope.variables);
15 }
16
17 function isReferenceHandled(reference) {
18 const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;
19
20 return parent.type === 'CallExpression';
21 }
22
23 function hasHandledReferences(references) {
24 return references.some(isReferenceHandled);
25 }
26
27 function checkAsyncMochaFunction(functionExpression) {
28 const scope = context.getScope();
29 const callback = functionExpression.params[0];
30 const callbackName = callback.name;
31 const callbackVariable = findParamInScope(callbackName, scope);
32
33 if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
34 context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
35 }
36 }
37
38 function check(node) {
39 if (astUtils.hasParentMochaFunctionCall(node) && isAsyncFunction(node)) {
40 checkAsyncMochaFunction(node);
41 }
42 }
43
44 return {
45 FunctionExpression: check,
46 ArrowFunctionExpression: check
47 };
48};