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.
64 lines
2.0 KiB
64 lines
2.0 KiB
/**
|
|
* @fileoverview Rule to flag when IIFE is not wrapped in parens
|
|
* @author Ilya Volodin
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description: "require parentheses around immediate `function` invocations",
|
|
category: "Best Practices",
|
|
recommended: false
|
|
},
|
|
|
|
schema: [
|
|
{
|
|
enum: ["outside", "inside", "any"]
|
|
}
|
|
]
|
|
},
|
|
|
|
create: function(context) {
|
|
|
|
var style = context.options[0] || "outside";
|
|
|
|
/**
|
|
* Check if the node is wrapped in ()
|
|
* @param {ASTNode} node node to evaluate
|
|
* @returns {boolean} True if it is wrapped
|
|
* @private
|
|
*/
|
|
function wrapped(node) {
|
|
var previousToken = context.getTokenBefore(node),
|
|
nextToken = context.getTokenAfter(node);
|
|
|
|
return previousToken && previousToken.value === "(" &&
|
|
nextToken && nextToken.value === ")";
|
|
}
|
|
|
|
return {
|
|
|
|
CallExpression: function(node) {
|
|
if (node.callee.type === "FunctionExpression") {
|
|
var callExpressionWrapped = wrapped(node),
|
|
functionExpressionWrapped = wrapped(node.callee);
|
|
|
|
if (!callExpressionWrapped && !functionExpressionWrapped) {
|
|
context.report(node, "Wrap an immediate function invocation in parentheses.");
|
|
} else if (style === "inside" && !functionExpressionWrapped) {
|
|
context.report(node, "Wrap only the function expression in parens.");
|
|
} else if (style === "outside" && !callExpressionWrapped) {
|
|
context.report(node, "Move the invocation into the parens that contain the function.");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
}
|
|
};
|
|
|