node/tools/eslint/lib/rules/wrap-iife.js
Yosuke Furukawa f9dd34d301 tools: replace closure-linter with eslint
PR-URL: https://github.com/iojs/io.js/pull/1539
Fixes: https://github.com/iojs/io.js/issues/1253
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-05-09 12:09:52 +09:00

43 lines
1.5 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.");
}
}
}
};
};