node/tools/eslint/lib/rules/no-wrap-func.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

65 lines
1.8 KiB
JavaScript

/**
* @fileoverview Rule to flag wrapping non-iife in parens
* @author Ilya Volodin
* @copyright 2013 Ilya Volodin. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
/**
* Checks a function expression to see if its surrounded by parens.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkFunction(node) {
var previousToken, nextToken, isCall;
if (node.type === "ArrowFunctionExpression" &&
/(?:Call|New|Logical|Binary|Conditional|Update)Expression/.test(node.parent.type)
) {
return;
}
// (function() {}).foo
if (node.parent.type === "MemberExpression" && node.parent.object === node) {
return;
}
// (function(){})()
isCall = /CallExpression|NewExpression/.test(node.parent.type);
if (isCall && node.parent.callee === node) {
return;
}
previousToken = context.getTokenBefore(node);
nextToken = context.getTokenAfter(node);
// f(function(){}) and new f(function(){})
if (isCall) {
// if the previousToken is right after the callee
if (node.parent.callee.range[1] === previousToken.range[0]) {
return;
}
}
if (previousToken.value === "(" && nextToken.value === ")") {
context.report(node, "Wrapping non-IIFE function literals in parens is unnecessary.");
}
}
return {
"ArrowFunctionExpression": checkFunction,
"FunctionExpression": checkFunction
};
};
module.exports.schema = [];