node/tools/eslint/lib/rules/max-nested-callbacks.js
Roman Reiss d91e10b3bd tools: update eslint to 0.24.0
PR-URL: https://github.com/nodejs/io.js/pull/2072
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alex Kocharin <alex@kocharin.ru>
2015-06-29 19:02:17 +02:00

73 lines
2 KiB
JavaScript

/**
* @fileoverview Rule to enforce a maximum number of nested callbacks.
* @author Ian Christian Myers
* @copyright 2013 Ian Christian Myers. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//--------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------
var THRESHOLD = context.options[0];
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
var callbackStack = [];
/**
* Checks a given function node for too many callbacks.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkFunction(node) {
var parent = node.parent;
if (parent.type === "CallExpression") {
callbackStack.push(node);
}
if (callbackStack.length > THRESHOLD) {
var opts = {num: callbackStack.length, max: THRESHOLD};
context.report(node, "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", opts);
}
}
/**
* Pops the call stack.
* @returns {void}
* @private
*/
function popStack() {
callbackStack.pop();
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"ArrowFunctionExpression": checkFunction,
"ArrowFunctionExpression:exit": popStack,
"FunctionExpression": checkFunction,
"FunctionExpression:exit": popStack
};
};
module.exports.schema = [
{
"type": "integer"
}
];