Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-extra-parens reported some parenthesized IIFEs (fixes #9140) #9158

Merged
merged 1 commit into from Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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