2 * @fileoverview Disallow parenthesising higher precedence subexpressions.
3 * @author Michael Ficarra
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const { isParenthesized: isParenthesizedRaw } = require("eslint-utils");
12 const astUtils = require("./utils/ast-utils.js");
19 description: "disallow unnecessary parentheses",
20 category: "Possible Errors",
22 url: "https://eslint.org/docs/rules/no-extra-parens"
48 conditionalAssign: { type: "boolean" },
49 nestedBinaryExpressions: { type: "boolean" },
50 returnAssign: { type: "boolean" },
51 ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
52 enforceForArrowConditionals: { type: "boolean" },
53 enforceForSequenceExpressions: { type: "boolean" },
54 enforceForNewInMemberExpressions: { type: "boolean" }
56 additionalProperties: false
66 unexpected: "Unnecessary parentheses around expression."
71 const sourceCode = context.getSourceCode();
73 const tokensToIgnore = new WeakSet();
74 const precedence = astUtils.getPrecedence;
75 const ALL_NODES = context.options[0] !== "functions";
76 const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false;
77 const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false;
78 const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false;
79 const IGNORE_JSX = ALL_NODES && context.options[1] && context.options[1].ignoreJSX;
80 const IGNORE_ARROW_CONDITIONALS = ALL_NODES && context.options[1] &&
81 context.options[1].enforceForArrowConditionals === false;
82 const IGNORE_SEQUENCE_EXPRESSIONS = ALL_NODES && context.options[1] &&
83 context.options[1].enforceForSequenceExpressions === false;
84 const IGNORE_NEW_IN_MEMBER_EXPR = ALL_NODES && context.options[1] &&
85 context.options[1].enforceForNewInMemberExpressions === false;
87 const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
88 const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
93 * Determines if this rule should be enforced for a node given the current configuration.
94 * @param {ASTNode} node The node to be checked.
95 * @returns {boolean} True if the rule should be enforced for this node.
98 function ruleApplies(node) {
99 if (node.type === "JSXElement" || node.type === "JSXFragment") {
100 const isSingleLine = node.loc.start.line === node.loc.end.line;
102 switch (IGNORE_JSX) {
104 // Exclude this JSX element from linting
108 // Exclude this JSX element if it is multi-line element
112 // Exclude this JSX element if it is single-line element
114 return !isSingleLine;
116 // Nothing special to be done for JSX elements
124 if (node.type === "SequenceExpression" && IGNORE_SEQUENCE_EXPRESSIONS) {
128 return ALL_NODES || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
132 * Determines if a node is surrounded by parentheses.
133 * @param {ASTNode} node The node to be checked.
134 * @returns {boolean} True if the node is parenthesised.
137 function isParenthesised(node) {
138 return isParenthesizedRaw(1, node, sourceCode);
142 * Determines if a node is surrounded by parentheses twice.
143 * @param {ASTNode} node The node to be checked.
144 * @returns {boolean} True if the node is doubly parenthesised.
147 function isParenthesisedTwice(node) {
148 return isParenthesizedRaw(2, node, sourceCode);
152 * Determines if a node is surrounded by (potentially) invalid parentheses.
153 * @param {ASTNode} node The node to be checked.
154 * @returns {boolean} True if the node is incorrectly parenthesised.
157 function hasExcessParens(node) {
158 return ruleApplies(node) && isParenthesised(node);
162 * Determines if a node that is expected to be parenthesised is surrounded by
163 * (potentially) invalid extra parentheses.
164 * @param {ASTNode} node The node to be checked.
165 * @returns {boolean} True if the node is has an unexpected extra pair of parentheses.
168 function hasDoubleExcessParens(node) {
169 return ruleApplies(node) && isParenthesisedTwice(node);
173 * Determines if a node test expression is allowed to have a parenthesised assignment
174 * @param {ASTNode} node The node to be checked.
175 * @returns {boolean} True if the assignment can be parenthesised.
178 function isCondAssignException(node) {
179 return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression";
183 * Determines if a node is in a return statement
184 * @param {ASTNode} node The node to be checked.
185 * @returns {boolean} True if the node is in a return statement.
188 function isInReturnStatement(node) {
189 for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
191 currentNode.type === "ReturnStatement" ||
192 (currentNode.type === "ArrowFunctionExpression" && currentNode.body.type !== "BlockStatement")
202 * Determines if a constructor function is newed-up with parens
203 * @param {ASTNode} newExpression The NewExpression node to be checked.
204 * @returns {boolean} True if the constructor is called with parens.
207 function isNewExpressionWithParens(newExpression) {
208 const lastToken = sourceCode.getLastToken(newExpression);
209 const penultimateToken = sourceCode.getTokenBefore(lastToken);
211 return newExpression.arguments.length > 0 ||
214 // The expression should end with its own parens, e.g., new new foo() is not a new expression with parens
215 astUtils.isOpeningParenToken(penultimateToken) &&
216 astUtils.isClosingParenToken(lastToken) &&
217 newExpression.callee.range[1] < newExpression.range[1]
222 * Determines if a node is or contains an assignment expression
223 * @param {ASTNode} node The node to be checked.
224 * @returns {boolean} True if the node is or contains an assignment expression.
227 function containsAssignment(node) {
228 if (node.type === "AssignmentExpression") {
231 if (node.type === "ConditionalExpression" &&
232 (node.consequent.type === "AssignmentExpression" || node.alternate.type === "AssignmentExpression")) {
235 if ((node.left && node.left.type === "AssignmentExpression") ||
236 (node.right && node.right.type === "AssignmentExpression")) {
244 * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment
245 * @param {ASTNode} node The node to be checked.
246 * @returns {boolean} True if the assignment can be parenthesised.
249 function isReturnAssignException(node) {
250 if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) {
254 if (node.type === "ReturnStatement") {
255 return node.argument && containsAssignment(node.argument);
257 if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
258 return containsAssignment(node.body);
260 return containsAssignment(node);
265 * Determines if a node following a [no LineTerminator here] restriction is
266 * surrounded by (potentially) invalid extra parentheses.
267 * @param {Token} token The token preceding the [no LineTerminator here] restriction.
268 * @param {ASTNode} node The node to be checked.
269 * @returns {boolean} True if the node is incorrectly parenthesised.
272 function hasExcessParensNoLineTerminator(token, node) {
273 if (token.loc.end.line === node.loc.start.line) {
274 return hasExcessParens(node);
277 return hasDoubleExcessParens(node);
281 * Determines whether a node should be preceded by an additional space when removing parens
282 * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
283 * @returns {boolean} `true` if a space should be inserted before the node
286 function requiresLeadingSpace(node) {
287 const leftParenToken = sourceCode.getTokenBefore(node);
288 const tokenBeforeLeftParen = sourceCode.getTokenBefore(node, 1);
289 const firstToken = sourceCode.getFirstToken(node);
291 return tokenBeforeLeftParen &&
292 tokenBeforeLeftParen.range[1] === leftParenToken.range[0] &&
293 leftParenToken.range[1] === firstToken.range[0] &&
294 !astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, firstToken);
298 * Determines whether a node should be followed by an additional space when removing parens
299 * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
300 * @returns {boolean} `true` if a space should be inserted after the node
303 function requiresTrailingSpace(node) {
304 const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 });
305 const rightParenToken = nextTwoTokens[0];
306 const tokenAfterRightParen = nextTwoTokens[1];
307 const tokenBeforeRightParen = sourceCode.getLastToken(node);
309 return rightParenToken && tokenAfterRightParen &&
310 !sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) &&
311 !astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen);
315 * Determines if a given expression node is an IIFE
316 * @param {ASTNode} node The node to check
317 * @returns {boolean} `true` if the given node is an IIFE
319 function isIIFE(node) {
320 return node.type === "CallExpression" && node.callee.type === "FunctionExpression";
325 * @param {ASTNode} node node to evaluate
329 function report(node) {
330 const leftParenToken = sourceCode.getTokenBefore(node);
331 const rightParenToken = sourceCode.getTokenAfter(node);
333 if (!isParenthesisedTwice(node)) {
334 if (tokensToIgnore.has(sourceCode.getFirstToken(node))) {
338 if (isIIFE(node) && !isParenthesised(node.callee)) {
348 function finishReport() {
351 loc: leftParenToken.loc,
352 messageId: "unexpected",
354 const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]);
356 return fixer.replaceTextRange([
357 leftParenToken.range[0],
358 rightParenToken.range[1]
359 ], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : ""));
365 reportsBuffer.reports.push({ node, finishReport });
373 * Evaluate Unary update
374 * @param {ASTNode} node node to evaluate
378 function checkUnaryUpdate(node) {
379 if (node.type === "UnaryExpression" && node.argument.type === "BinaryExpression" && node.argument.operator === "**") {
383 if (hasExcessParens(node.argument) && precedence(node.argument) >= precedence(node)) {
384 report(node.argument);
389 * Check if a member expression contains a call expression
390 * @param {ASTNode} node MemberExpression node to evaluate
391 * @returns {boolean} true if found, false if not
393 function doesMemberExpressionContainCallExpression(node) {
394 let currentNode = node.object;
395 let currentNodeType = node.object.type;
397 while (currentNodeType === "MemberExpression") {
398 currentNode = currentNode.object;
399 currentNodeType = currentNode.type;
402 return currentNodeType === "CallExpression";
406 * Evaluate a new call
407 * @param {ASTNode} node node to evaluate
411 function checkCallNew(node) {
412 const callee = node.callee;
414 if (hasExcessParens(callee) && precedence(callee) >= precedence(node)) {
415 const hasNewParensException = callee.type === "NewExpression" && !isNewExpressionWithParens(callee);
418 hasDoubleExcessParens(callee) ||
419 !isIIFE(node) && !hasNewParensException && !(
422 * Allow extra parens around a new expression if
423 * there are intervening parentheses.
425 (callee.type === "MemberExpression" && doesMemberExpressionContainCallExpression(callee))
432 .filter(arg => hasExcessParens(arg) && precedence(arg) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
437 * Evaluate binary logicals
438 * @param {ASTNode} node node to evaluate
442 function checkBinaryLogical(node) {
443 const prec = precedence(node);
444 const leftPrecedence = precedence(node.left);
445 const rightPrecedence = precedence(node.right);
446 const isExponentiation = node.operator === "**";
447 const shouldSkipLeft = (NESTED_BINARY && (node.left.type === "BinaryExpression" || node.left.type === "LogicalExpression")) ||
448 node.left.type === "UnaryExpression" && isExponentiation;
449 const shouldSkipRight = NESTED_BINARY && (node.right.type === "BinaryExpression" || node.right.type === "LogicalExpression");
451 if (!shouldSkipLeft && hasExcessParens(node.left) && (leftPrecedence > prec || (leftPrecedence === prec && !isExponentiation))) {
454 if (!shouldSkipRight && hasExcessParens(node.right) && (rightPrecedence > prec || (rightPrecedence === prec && isExponentiation))) {
460 * Check the parentheses around the super class of the given class definition.
461 * @param {ASTNode} node The node of class declarations to check.
464 function checkClass(node) {
465 if (!node.superClass) {
470 * If `node.superClass` is a LeftHandSideExpression, parentheses are extra.
471 * Otherwise, parentheses are needed.
473 const hasExtraParens = precedence(node.superClass) > PRECEDENCE_OF_UPDATE_EXPR
474 ? hasExcessParens(node.superClass)
475 : hasDoubleExcessParens(node.superClass);
477 if (hasExtraParens) {
478 report(node.superClass);
483 * Check the parentheses around the argument of the given spread operator.
484 * @param {ASTNode} node The node of spread elements/properties to check.
487 function checkSpreadOperator(node) {
488 const hasExtraParens = precedence(node.argument) >= PRECEDENCE_OF_ASSIGNMENT_EXPR
489 ? hasExcessParens(node.argument)
490 : hasDoubleExcessParens(node.argument);
492 if (hasExtraParens) {
493 report(node.argument);
498 * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration
499 * @param {ASTNode} node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node
502 function checkExpressionOrExportStatement(node) {
503 const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node);
504 const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken);
505 const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null;
506 const tokenAfterClosingParens = secondToken ? sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken) : null;
509 astUtils.isOpeningParenToken(firstToken) &&
511 astUtils.isOpeningBraceToken(secondToken) ||
512 secondToken.type === "Keyword" && (
513 secondToken.value === "function" ||
514 secondToken.value === "class" ||
515 secondToken.value === "let" &&
516 tokenAfterClosingParens &&
518 astUtils.isOpeningBracketToken(tokenAfterClosingParens) ||
519 tokenAfterClosingParens.type === "Identifier"
522 secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function"
525 tokensToIgnore.add(secondToken);
528 if (hasExcessParens(node)) {
534 * Finds the path from the given node to the specified ancestor.
535 * @param {ASTNode} node First node in the path.
536 * @param {ASTNode} ancestor Last node in the path.
537 * @returns {ASTNode[]} Path, including both nodes.
538 * @throws {Error} If the given node does not have the specified ancestor.
540 function pathToAncestor(node, ancestor) {
542 let currentNode = node;
544 while (currentNode !== ancestor) {
546 currentNode = currentNode.parent;
548 /* istanbul ignore if */
549 if (currentNode === null) {
550 throw new Error("Nodes are not in the ancestor-descendant relationship.");
553 path.push(currentNode);
560 * Finds the path from the given node to the specified descendant.
561 * @param {ASTNode} node First node in the path.
562 * @param {ASTNode} descendant Last node in the path.
563 * @returns {ASTNode[]} Path, including both nodes.
564 * @throws {Error} If the given node does not have the specified descendant.
566 function pathToDescendant(node, descendant) {
567 return pathToAncestor(descendant, node).reverse();
571 * Checks whether the syntax of the given ancestor of an 'in' expression inside a for-loop initializer
572 * is preventing the 'in' keyword from being interpreted as a part of an ill-formed for-in loop.
573 * @param {ASTNode} node Ancestor of an 'in' expression.
574 * @param {ASTNode} child Child of the node, ancestor of the same 'in' expression or the 'in' expression itself.
575 * @returns {boolean} True if the keyword 'in' would be interpreted as the 'in' operator, without any parenthesis.
577 function isSafelyEnclosingInExpression(node, child) {
579 case "ArrayExpression":
581 case "BlockStatement":
582 case "ObjectExpression":
583 case "ObjectPattern":
584 case "TemplateLiteral":
586 case "ArrowFunctionExpression":
587 case "FunctionExpression":
588 return node.params.includes(child);
589 case "CallExpression":
590 case "NewExpression":
591 return node.arguments.includes(child);
592 case "MemberExpression":
593 return node.computed && node.property === child;
594 case "ConditionalExpression":
595 return node.consequent === child;
602 * Starts a new reports buffering. Warnings will be stored in a buffer instead of being reported immediately.
603 * An additional logic that requires multiple nodes (e.g. a whole subtree) may dismiss some of the stored warnings.
606 function startNewReportsBuffering() {
608 upper: reportsBuffer,
609 inExpressionNodes: [],
615 * Ends the current reports buffering.
618 function endCurrentReportsBuffering() {
619 const { upper, inExpressionNodes, reports } = reportsBuffer;
622 upper.inExpressionNodes.push(...inExpressionNodes);
623 upper.reports.push(...reports);
626 // flush remaining reports
627 reports.forEach(({ finishReport }) => finishReport());
630 reportsBuffer = upper;
634 * Checks whether the given node is in the current reports buffer.
635 * @param {ASTNode} node Node to check.
636 * @returns {boolean} True if the node is in the current buffer, false otherwise.
638 function isInCurrentReportsBuffer(node) {
639 return reportsBuffer.reports.some(r => r.node === node);
643 * Removes the given node from the current reports buffer.
644 * @param {ASTNode} node Node to remove.
647 function removeFromCurrentReportsBuffer(node) {
648 reportsBuffer.reports = reportsBuffer.reports.filter(r => r.node !== node);
652 ArrayExpression(node) {
654 .filter(e => e && hasExcessParens(e) && precedence(e) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
658 ArrowFunctionExpression(node) {
659 if (isReturnAssignException(node)) {
663 if (node.body.type === "ConditionalExpression" &&
664 IGNORE_ARROW_CONDITIONALS &&
665 !isParenthesisedTwice(node.body)
670 if (node.body.type !== "BlockStatement") {
671 const firstBodyToken = sourceCode.getFirstToken(node.body, astUtils.isNotOpeningParenToken);
672 const tokenBeforeFirst = sourceCode.getTokenBefore(firstBodyToken);
674 if (astUtils.isOpeningParenToken(tokenBeforeFirst) && astUtils.isOpeningBraceToken(firstBodyToken)) {
675 tokensToIgnore.add(firstBodyToken);
677 if (hasExcessParens(node.body) && precedence(node.body) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
683 AssignmentExpression(node) {
684 if (isReturnAssignException(node)) {
688 if (hasExcessParens(node.right) && precedence(node.right) >= precedence(node)) {
693 BinaryExpression(node) {
694 if (reportsBuffer && node.operator === "in") {
695 reportsBuffer.inExpressionNodes.push(node);
698 checkBinaryLogical(node);
701 CallExpression: checkCallNew,
705 .filter(member => member.type === "MethodDefinition" && member.computed &&
706 member.key && hasExcessParens(member.key) && precedence(member.key) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
707 .forEach(member => report(member.key));
710 ConditionalExpression(node) {
711 if (isReturnAssignException(node)) {
715 if (hasExcessParens(node.test) && precedence(node.test) >= precedence({ type: "LogicalExpression", operator: "||" })) {
719 if (hasExcessParens(node.consequent) && precedence(node.consequent) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
720 report(node.consequent);
723 if (hasExcessParens(node.alternate) && precedence(node.alternate) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
724 report(node.alternate);
728 DoWhileStatement(node) {
729 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
734 ExportDefaultDeclaration: node => checkExpressionOrExportStatement(node.declaration),
735 ExpressionStatement: node => checkExpressionOrExportStatement(node.expression),
737 "ForInStatement, ForOfStatement"(node) {
738 if (node.left.type !== "VariableDeclarator") {
739 const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken);
742 firstLeftToken.value === "let" && (
745 * If `let` is the only thing on the left side of the loop, it's the loop variable: `for ((let) of foo);`
746 * Removing it will cause a syntax error, because it will be parsed as the start of a VariableDeclarator.
748 (firstLeftToken.range[1] === node.left.range[1] || /*
749 * If `let` is followed by a `[` token, it's a property access on the `let` value: `for ((let[foo]) of bar);`
750 * Removing it will cause the property access to be parsed as a destructuring declaration of `foo` instead.
752 astUtils.isOpeningBracketToken(
753 sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken)
757 tokensToIgnore.add(firstLeftToken);
760 if (!(node.type === "ForOfStatement" && node.right.type === "SequenceExpression") && hasExcessParens(node.right)) {
763 if (hasExcessParens(node.left)) {
769 if (node.test && hasExcessParens(node.test) && !isCondAssignException(node)) {
773 if (node.update && hasExcessParens(node.update)) {
778 startNewReportsBuffering();
780 if (hasExcessParens(node.init)) {
786 "ForStatement > *.init:exit"(node) {
789 * Removing parentheses around `in` expressions might change semantics and cause errors.
791 * For example, this valid for loop:
792 * for (let a = (b in c); ;);
793 * after removing parentheses would be treated as an invalid for-in loop:
794 * for (let a = b in c; ;);
797 if (reportsBuffer.reports.length) {
798 reportsBuffer.inExpressionNodes.forEach(inExpressionNode => {
799 const path = pathToDescendant(node, inExpressionNode);
802 for (let i = 0; i < path.length; i++) {
803 const pathNode = path[i];
805 if (i < path.length - 1) {
806 const nextPathNode = path[i + 1];
808 if (isSafelyEnclosingInExpression(pathNode, nextPathNode)) {
810 // The 'in' expression in safely enclosed by the syntax of its ancestor nodes (e.g. by '{}' or '[]').
815 if (isParenthesised(pathNode)) {
816 if (isInCurrentReportsBuffer(pathNode)) {
818 // This node was supposed to be reported, but parentheses might be necessary.
820 if (isParenthesisedTwice(pathNode)) {
823 * This node is parenthesised twice, it certainly has at least one pair of `extra` parentheses.
824 * If the --fix option is on, the current fixing iteration will remove only one pair of parentheses.
825 * The remaining pair is safely enclosing the 'in' expression.
830 // Exclude the outermost node only.
831 if (!nodeToExclude) {
832 nodeToExclude = pathNode;
835 // Don't break the loop here, there might be some safe nodes or parentheses that will stay inside.
839 // This node will stay parenthesised, the 'in' expression in safely enclosed by '()'.
845 // Exclude the node from the list (i.e. treat parentheses as necessary)
846 removeFromCurrentReportsBuffer(nodeToExclude);
850 endCurrentReportsBuffering();
854 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
859 ImportExpression(node) {
860 const { source } = node;
862 if (source.type === "SequenceExpression") {
863 if (hasDoubleExcessParens(source)) {
866 } else if (hasExcessParens(source)) {
871 LogicalExpression: checkBinaryLogical,
873 MemberExpression(node) {
874 const nodeObjHasExcessParens = hasExcessParens(node.object);
877 nodeObjHasExcessParens &&
878 precedence(node.object) >= precedence(node) &&
882 astUtils.isDecimalInteger(node.object) ||
884 // RegExp literal is allowed to have parens (#1589)
885 (node.object.type === "Literal" && node.object.regex)
892 if (nodeObjHasExcessParens &&
893 node.object.type === "CallExpression" &&
894 node.parent.type !== "NewExpression") {
898 if (nodeObjHasExcessParens &&
899 !IGNORE_NEW_IN_MEMBER_EXPR &&
900 node.object.type === "NewExpression" &&
901 isNewExpressionWithParens(node.object)) {
905 if (node.computed && hasExcessParens(node.property)) {
906 report(node.property);
910 NewExpression: checkCallNew,
912 ObjectExpression(node) {
914 .filter(property => {
915 const value = property.value;
917 return value && hasExcessParens(value) && precedence(value) >= PRECEDENCE_OF_ASSIGNMENT_EXPR;
918 }).forEach(property => report(property.value));
923 const { key } = node;
925 if (key && hasExcessParens(key) && precedence(key) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
931 ReturnStatement(node) {
932 const returnToken = sourceCode.getFirstToken(node);
934 if (isReturnAssignException(node)) {
939 hasExcessParensNoLineTerminator(returnToken, node.argument) &&
941 // RegExp literal is allowed to have parens (#1589)
942 !(node.argument.type === "Literal" && node.argument.regex)) {
943 report(node.argument);
947 SequenceExpression(node) {
949 .filter(e => hasExcessParens(e) && precedence(e) >= precedence(node))
954 if (node.test && hasExcessParens(node.test)) {
959 SwitchStatement(node) {
960 if (hasExcessParens(node.discriminant)) {
961 report(node.discriminant);
965 ThrowStatement(node) {
966 const throwToken = sourceCode.getFirstToken(node);
968 if (hasExcessParensNoLineTerminator(throwToken, node.argument)) {
969 report(node.argument);
973 UnaryExpression: checkUnaryUpdate,
974 UpdateExpression: checkUnaryUpdate,
975 AwaitExpression: checkUnaryUpdate,
977 VariableDeclarator(node) {
978 if (node.init && hasExcessParens(node.init) &&
979 precedence(node.init) >= PRECEDENCE_OF_ASSIGNMENT_EXPR &&
981 // RegExp literal is allowed to have parens (#1589)
982 !(node.init.type === "Literal" && node.init.regex)) {
987 WhileStatement(node) {
988 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
993 WithStatement(node) {
994 if (hasExcessParens(node.object)) {
999 YieldExpression(node) {
1000 if (node.argument) {
1001 const yieldToken = sourceCode.getFirstToken(node);
1003 if ((precedence(node.argument) >= precedence(node) &&
1004 hasExcessParensNoLineTerminator(yieldToken, node.argument)) ||
1005 hasDoubleExcessParens(node.argument)) {
1006 report(node.argument);
1011 ClassDeclaration: checkClass,
1012 ClassExpression: checkClass,
1014 SpreadElement: checkSpreadOperator,
1015 SpreadProperty: checkSpreadOperator,
1016 ExperimentalSpreadProperty: checkSpreadOperator,
1018 TemplateLiteral(node) {
1020 .filter(e => e && hasExcessParens(e))
1024 AssignmentPattern(node) {
1025 const { right } = node;
1027 if (right && hasExcessParens(right) && precedence(right) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {