Skip to content

Commit

Permalink
Merge pull request #5865 from vitorbal/metadata3
Browse files Browse the repository at this point in the history
Update: Add metadata to existing rules - Batch 3 (refs #5417)
  • Loading branch information
ilyavolodin committed Apr 15, 2016
2 parents 9b7f6a7 + 76913b6 commit 750cc09
Show file tree
Hide file tree
Showing 40 changed files with 2,578 additions and 2,174 deletions.
400 changes: 207 additions & 193 deletions lib/rules/max-len.js

Large diffs are not rendered by default.

162 changes: 86 additions & 76 deletions lib/rules/max-nested-callbacks.js
Expand Up @@ -10,94 +10,104 @@
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
module.exports = {
meta: {
docs: {
description: "enforce a maximum depth that callbacks can be nested",
category: "Stylistic Issues",
recommended: false
},

schema: [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "object",
"properties": {
"maximum": {
"type": "integer",
"minimum": 0
},
"max": {
"type": "integer",
"minimum": 0
}
},
"additionalProperties": false
}
]
}
]
},

//--------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------
var option = context.options[0],
THRESHOLD = 10;
create: function(context) {

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
THRESHOLD = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
THRESHOLD = option.max;
}
if (typeof option === "number") {
THRESHOLD = option;
}
//--------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------
var option = context.options[0],
THRESHOLD = 10;

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
THRESHOLD = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
THRESHOLD = option.max;
}
if (typeof option === "number") {
THRESHOLD = option;
}

var callbackStack = [];
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------

/**
* 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;
var callbackStack = [];

if (parent.type === "CallExpression") {
callbackStack.push(node);
}
/**
* 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 (callbackStack.length > THRESHOLD) {
var opts = {num: callbackStack.length, max: THRESHOLD};
if (parent.type === "CallExpression") {
callbackStack.push(node);
}

context.report(node, "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", opts);
}
}
if (callbackStack.length > THRESHOLD) {
var opts = {num: callbackStack.length, max: THRESHOLD};

/**
* Pops the call stack.
* @returns {void}
* @private
*/
function popStack() {
callbackStack.pop();
}
context.report(node, "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", opts);
}
}

//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
/**
* Pops the call stack.
* @returns {void}
* @private
*/
function popStack() {
callbackStack.pop();
}

return {
"ArrowFunctionExpression": checkFunction,
"ArrowFunctionExpression:exit": popStack,
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------

"FunctionExpression": checkFunction,
"FunctionExpression:exit": popStack
};
return {
"ArrowFunctionExpression": checkFunction,
"ArrowFunctionExpression:exit": popStack,

};
"FunctionExpression": checkFunction,
"FunctionExpression:exit": popStack
};

module.exports.schema = [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "object",
"properties": {
"maximum": {
"type": "integer",
"minimum": 0
},
"max": {
"type": "integer",
"minimum": 0
}
},
"additionalProperties": false
}
]
}
];
};
116 changes: 63 additions & 53 deletions lib/rules/max-params.js
Expand Up @@ -11,65 +11,75 @@
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
module.exports = {
meta: {
docs: {
description: "enforce a maximum number of parameters in `function` definitions",
category: "Stylistic Issues",
recommended: false
},

var option = context.options[0],
numParams = 3;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
numParams = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
numParams = option.max;
}
if (typeof option === "number") {
numParams = option;
}

/**
* Checks a function to see if it has too many parameters.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkFunction(node) {
if (node.params.length > numParams) {
context.report(node, "This function has too many parameters ({{count}}). Maximum allowed is {{max}}.", {
count: node.params.length,
max: numParams
});
}
}

return {
"FunctionDeclaration": checkFunction,
"ArrowFunctionExpression": checkFunction,
"FunctionExpression": checkFunction
};

};

module.exports.schema = [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
schema: [
{
"type": "object",
"properties": {
"maximum": {
"oneOf": [
{
"type": "integer",
"minimum": 0
},
"max": {
"type": "integer",
"minimum": 0
{
"type": "object",
"properties": {
"maximum": {
"type": "integer",
"minimum": 0
},
"max": {
"type": "integer",
"minimum": 0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
]
}
]
},

create: function(context) {

var option = context.options[0],
numParams = 3;

if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
numParams = option.maximum;
}
if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
numParams = option.max;
}
if (typeof option === "number") {
numParams = option;
}

/**
* Checks a function to see if it has too many parameters.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkFunction(node) {
if (node.params.length > numParams) {
context.report(node, "This function has too many parameters ({{count}}). Maximum allowed is {{max}}.", {
count: node.params.length,
max: numParams
});
}
}

return {
"FunctionDeclaration": checkFunction,
"ArrowFunctionExpression": checkFunction,
"FunctionExpression": checkFunction
};

}
];
};

0 comments on commit 750cc09

Please sign in to comment.