Skip to content

Commit

Permalink
Fix: no-extra-parens reported some parenthesized IIFEs (fixes #9140) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Aug 28, 2017
1 parent e6b115c commit d6e436f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
9 changes: 2 additions & 7 deletions lib/ast-utils.js
Expand Up @@ -808,19 +808,14 @@ module.exports = {
return 17;

case "CallExpression":

// IIFE is allowed to have parens in any position (#655)
if (node.callee.type === "FunctionExpression") {
return -1;
}
return 18;

case "NewExpression":
return 19;

// no default
default:
return 20;
}
return 20;
},

/**
Expand Down
34 changes: 23 additions & 11 deletions lib/rules/no-extra-parens.js
Expand Up @@ -276,6 +276,15 @@ module.exports = {
!astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen);
}

/**
* Determines if a given expression node is an IIFE
* @param {ASTNode} node The node to check
* @returns {boolean} `true` if the given node is an IIFE
*/
function isIIFE(node) {
return node.type === "CallExpression" && node.callee.type === "FunctionExpression";
}

/**
* Report the node
* @param {ASTNode} node node to evaluate
Expand All @@ -286,8 +295,14 @@ module.exports = {
const leftParenToken = sourceCode.getTokenBefore(node);
const rightParenToken = sourceCode.getTokenAfter(node);

if (tokensToIgnore.has(sourceCode.getFirstToken(node)) && !isParenthesisedTwice(node)) {
return;
if (!isParenthesisedTwice(node)) {
if (tokensToIgnore.has(sourceCode.getFirstToken(node))) {
return;
}

if (isIIFE(node) && !isParenthesised(node.callee)) {
return;
}
}

context.report({
Expand Down Expand Up @@ -328,15 +343,12 @@ module.exports = {
* @private
*/
function checkCallNew(node) {
if (hasExcessParens(node.callee) && precedence(node.callee) >= precedence(node) && !(
node.type === "CallExpression" &&
(node.callee.type === "FunctionExpression" ||
node.callee.type === "NewExpression" && !isNewExpressionWithParens(node.callee)) &&

// One set of parentheses are allowed for a function expression
!hasDoubleExcessParens(node.callee)
)) {
report(node.callee);
if (hasExcessParens(node.callee) && precedence(node.callee) >= precedence(node)) {
const hasNewParensException = node.callee.type === "NewExpression" && !isNewExpressionWithParens(node.callee);

if (hasDoubleExcessParens(node.callee) || !isIIFE(node) && !hasNewParensException) {
report(node.callee);
}
}
if (node.arguments.length === 1) {
if (hasDoubleExcessParens(node.arguments[0]) && precedence(node.arguments[0]) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -175,6 +175,7 @@ ruleTester.run("no-extra-parens", rule, {
"var o = { foo: (function() { return bar(); })() };",
"o.foo = (function(){ return bar(); })();",
"(function(){ return bar(); })(), (function(){ return bar(); })()",
"function foo() { return (function(){}()); }",

// parens are required around yield
"var foo = (function*() { if ((yield foo()) + 1) { return; } }())",
Expand Down

0 comments on commit d6e436f

Please sign in to comment.