mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
45 lines
1.3 KiB
10 years ago
|
/**
|
||
|
* @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;
|
||
|
|
||
|
if (node.type === "ArrowFunctionExpression" &&
|
||
|
/(?:Call|New|Logical|Binary|Conditional|Update)Expression/.test(node.parent.type)
|
||
|
) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!/CallExpression|NewExpression/.test(node.parent.type)) {
|
||
|
previousToken = context.getTokenBefore(node);
|
||
|
nextToken = context.getTokenAfter(node);
|
||
|
if (previousToken.value === "(" && nextToken.value === ")") {
|
||
|
context.report(node, "Wrapping non-IIFE function literals in parens is unnecessary.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
"ArrowFunctionExpression": checkFunction,
|
||
|
"FunctionExpression": checkFunction
|
||
|
};
|
||
|
|
||
|
};
|