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

48 lines
1.6 KiB
JavaScript

/**
* @fileoverview Rule to flag when IIFE is not wrapped in parens
* @author Ilya Volodin
* @copyright 2013 Ilya Volodin. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
var style = context.options[0] || "outside";
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.");
}
}
}
};
};
module.exports.schema = [
{
"enum": ["outside", "inside", "any"]
}
];