]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-extra-parens.js
first commit
[pve-eslint.git] / eslint / lib / rules / no-extra-parens.js
1 /**
2 * @fileoverview Disallow parenthesising higher precedence subexpressions.
3 * @author Michael Ficarra
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 const { isParenthesized: isParenthesizedRaw } = require("eslint-utils");
12 const astUtils = require("./utils/ast-utils.js");
13
14 module.exports = {
15 meta: {
16 type: "layout",
17
18 docs: {
19 description: "disallow unnecessary parentheses",
20 category: "Possible Errors",
21 recommended: false,
22 url: "https://eslint.org/docs/rules/no-extra-parens"
23 },
24
25 fixable: "code",
26
27 schema: {
28 anyOf: [
29 {
30 type: "array",
31 items: [
32 {
33 enum: ["functions"]
34 }
35 ],
36 minItems: 0,
37 maxItems: 1
38 },
39 {
40 type: "array",
41 items: [
42 {
43 enum: ["all"]
44 },
45 {
46 type: "object",
47 properties: {
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" }
55 },
56 additionalProperties: false
57 }
58 ],
59 minItems: 0,
60 maxItems: 2
61 }
62 ]
63 },
64
65 messages: {
66 unexpected: "Unnecessary parentheses around expression."
67 }
68 },
69
70 create(context) {
71 const sourceCode = context.getSourceCode();
72
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;
86
87 const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
88 const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
89
90 let reportsBuffer;
91
92 /**
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.
96 * @private
97 */
98 function ruleApplies(node) {
99 if (node.type === "JSXElement" || node.type === "JSXFragment") {
100 const isSingleLine = node.loc.start.line === node.loc.end.line;
101
102 switch (IGNORE_JSX) {
103
104 // Exclude this JSX element from linting
105 case "all":
106 return false;
107
108 // Exclude this JSX element if it is multi-line element
109 case "multi-line":
110 return isSingleLine;
111
112 // Exclude this JSX element if it is single-line element
113 case "single-line":
114 return !isSingleLine;
115
116 // Nothing special to be done for JSX elements
117 case "none":
118 break;
119
120 // no default
121 }
122 }
123
124 if (node.type === "SequenceExpression" && IGNORE_SEQUENCE_EXPRESSIONS) {
125 return false;
126 }
127
128 return ALL_NODES || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
129 }
130
131 /**
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.
135 * @private
136 */
137 function isParenthesised(node) {
138 return isParenthesizedRaw(1, node, sourceCode);
139 }
140
141 /**
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.
145 * @private
146 */
147 function isParenthesisedTwice(node) {
148 return isParenthesizedRaw(2, node, sourceCode);
149 }
150
151 /**
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.
155 * @private
156 */
157 function hasExcessParens(node) {
158 return ruleApplies(node) && isParenthesised(node);
159 }
160
161 /**
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.
166 * @private
167 */
168 function hasDoubleExcessParens(node) {
169 return ruleApplies(node) && isParenthesisedTwice(node);
170 }
171
172 /**
173 * Determines if a node that is expected to be parenthesised is surrounded by
174 * (potentially) invalid extra parentheses with considering precedence level of the node.
175 * If the preference level of the node is not higher or equal to precedence lower limit, it also checks
176 * whether the node is surrounded by parentheses twice or not.
177 * @param {ASTNode} node The node to be checked.
178 * @param {number} precedenceLowerLimit The lower limit of precedence.
179 * @returns {boolean} True if the node is has an unexpected extra pair of parentheses.
180 * @private
181 */
182 function hasExcessParensWithPrecedence(node, precedenceLowerLimit) {
183 if (ruleApplies(node) && isParenthesised(node)) {
184 if (
185 precedence(node) >= precedenceLowerLimit ||
186 isParenthesisedTwice(node)
187 ) {
188 return true;
189 }
190 }
191 return false;
192 }
193
194 /**
195 * Determines if a node test expression is allowed to have a parenthesised assignment
196 * @param {ASTNode} node The node to be checked.
197 * @returns {boolean} True if the assignment can be parenthesised.
198 * @private
199 */
200 function isCondAssignException(node) {
201 return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression";
202 }
203
204 /**
205 * Determines if a node is in a return statement
206 * @param {ASTNode} node The node to be checked.
207 * @returns {boolean} True if the node is in a return statement.
208 * @private
209 */
210 function isInReturnStatement(node) {
211 for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
212 if (
213 currentNode.type === "ReturnStatement" ||
214 (currentNode.type === "ArrowFunctionExpression" && currentNode.body.type !== "BlockStatement")
215 ) {
216 return true;
217 }
218 }
219
220 return false;
221 }
222
223 /**
224 * Determines if a constructor function is newed-up with parens
225 * @param {ASTNode} newExpression The NewExpression node to be checked.
226 * @returns {boolean} True if the constructor is called with parens.
227 * @private
228 */
229 function isNewExpressionWithParens(newExpression) {
230 const lastToken = sourceCode.getLastToken(newExpression);
231 const penultimateToken = sourceCode.getTokenBefore(lastToken);
232
233 return newExpression.arguments.length > 0 ||
234 (
235
236 // The expression should end with its own parens, e.g., new new foo() is not a new expression with parens
237 astUtils.isOpeningParenToken(penultimateToken) &&
238 astUtils.isClosingParenToken(lastToken) &&
239 newExpression.callee.range[1] < newExpression.range[1]
240 );
241 }
242
243 /**
244 * Determines if a node is or contains an assignment expression
245 * @param {ASTNode} node The node to be checked.
246 * @returns {boolean} True if the node is or contains an assignment expression.
247 * @private
248 */
249 function containsAssignment(node) {
250 if (node.type === "AssignmentExpression") {
251 return true;
252 }
253 if (node.type === "ConditionalExpression" &&
254 (node.consequent.type === "AssignmentExpression" || node.alternate.type === "AssignmentExpression")) {
255 return true;
256 }
257 if ((node.left && node.left.type === "AssignmentExpression") ||
258 (node.right && node.right.type === "AssignmentExpression")) {
259 return true;
260 }
261
262 return false;
263 }
264
265 /**
266 * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment
267 * @param {ASTNode} node The node to be checked.
268 * @returns {boolean} True if the assignment can be parenthesised.
269 * @private
270 */
271 function isReturnAssignException(node) {
272 if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) {
273 return false;
274 }
275
276 if (node.type === "ReturnStatement") {
277 return node.argument && containsAssignment(node.argument);
278 }
279 if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
280 return containsAssignment(node.body);
281 }
282 return containsAssignment(node);
283
284 }
285
286 /**
287 * Determines if a node following a [no LineTerminator here] restriction is
288 * surrounded by (potentially) invalid extra parentheses.
289 * @param {Token} token The token preceding the [no LineTerminator here] restriction.
290 * @param {ASTNode} node The node to be checked.
291 * @returns {boolean} True if the node is incorrectly parenthesised.
292 * @private
293 */
294 function hasExcessParensNoLineTerminator(token, node) {
295 if (token.loc.end.line === node.loc.start.line) {
296 return hasExcessParens(node);
297 }
298
299 return hasDoubleExcessParens(node);
300 }
301
302 /**
303 * Determines whether a node should be preceded by an additional space when removing parens
304 * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
305 * @returns {boolean} `true` if a space should be inserted before the node
306 * @private
307 */
308 function requiresLeadingSpace(node) {
309 const leftParenToken = sourceCode.getTokenBefore(node);
310 const tokenBeforeLeftParen = sourceCode.getTokenBefore(leftParenToken, { includeComments: true });
311 const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParenToken, { includeComments: true });
312
313 return tokenBeforeLeftParen &&
314 tokenBeforeLeftParen.range[1] === leftParenToken.range[0] &&
315 leftParenToken.range[1] === tokenAfterLeftParen.range[0] &&
316 !astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, tokenAfterLeftParen);
317 }
318
319 /**
320 * Determines whether a node should be followed by an additional space when removing parens
321 * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
322 * @returns {boolean} `true` if a space should be inserted after the node
323 * @private
324 */
325 function requiresTrailingSpace(node) {
326 const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 });
327 const rightParenToken = nextTwoTokens[0];
328 const tokenAfterRightParen = nextTwoTokens[1];
329 const tokenBeforeRightParen = sourceCode.getLastToken(node);
330
331 return rightParenToken && tokenAfterRightParen &&
332 !sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) &&
333 !astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen);
334 }
335
336 /**
337 * Determines if a given expression node is an IIFE
338 * @param {ASTNode} node The node to check
339 * @returns {boolean} `true` if the given node is an IIFE
340 */
341 function isIIFE(node) {
342 return node.type === "CallExpression" && node.callee.type === "FunctionExpression";
343 }
344
345 /**
346 * Determines if the given node can be the assignment target in destructuring or the LHS of an assignment.
347 * This is to avoid an autofix that could change behavior because parsers mistakenly allow invalid syntax,
348 * such as `(a = b) = c` and `[(a = b) = c] = []`. Ideally, this function shouldn't be necessary.
349 * @param {ASTNode} [node] The node to check
350 * @returns {boolean} `true` if the given node can be a valid assignment target
351 */
352 function canBeAssignmentTarget(node) {
353 return node && (node.type === "Identifier" || node.type === "MemberExpression");
354 }
355
356 /**
357 * Report the node
358 * @param {ASTNode} node node to evaluate
359 * @returns {void}
360 * @private
361 */
362 function report(node) {
363 const leftParenToken = sourceCode.getTokenBefore(node);
364 const rightParenToken = sourceCode.getTokenAfter(node);
365
366 if (!isParenthesisedTwice(node)) {
367 if (tokensToIgnore.has(sourceCode.getFirstToken(node))) {
368 return;
369 }
370
371 if (isIIFE(node) && !isParenthesised(node.callee)) {
372 return;
373 }
374 }
375
376 /**
377 * Finishes reporting
378 * @returns {void}
379 * @private
380 */
381 function finishReport() {
382 context.report({
383 node,
384 loc: leftParenToken.loc,
385 messageId: "unexpected",
386 fix(fixer) {
387 const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]);
388
389 return fixer.replaceTextRange([
390 leftParenToken.range[0],
391 rightParenToken.range[1]
392 ], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : ""));
393 }
394 });
395 }
396
397 if (reportsBuffer) {
398 reportsBuffer.reports.push({ node, finishReport });
399 return;
400 }
401
402 finishReport();
403 }
404
405 /**
406 * Evaluate a argument of the node.
407 * @param {ASTNode} node node to evaluate
408 * @returns {void}
409 * @private
410 */
411 function checkArgumentWithPrecedence(node) {
412 if (hasExcessParensWithPrecedence(node.argument, precedence(node))) {
413 report(node.argument);
414 }
415 }
416
417 /**
418 * Check if a member expression contains a call expression
419 * @param {ASTNode} node MemberExpression node to evaluate
420 * @returns {boolean} true if found, false if not
421 */
422 function doesMemberExpressionContainCallExpression(node) {
423 let currentNode = node.object;
424 let currentNodeType = node.object.type;
425
426 while (currentNodeType === "MemberExpression") {
427 currentNode = currentNode.object;
428 currentNodeType = currentNode.type;
429 }
430
431 return currentNodeType === "CallExpression";
432 }
433
434 /**
435 * Evaluate a new call
436 * @param {ASTNode} node node to evaluate
437 * @returns {void}
438 * @private
439 */
440 function checkCallNew(node) {
441 const callee = node.callee;
442
443 if (hasExcessParensWithPrecedence(callee, precedence(node))) {
444 const hasNewParensException = callee.type === "NewExpression" && !isNewExpressionWithParens(callee);
445
446 if (
447 hasDoubleExcessParens(callee) ||
448 !isIIFE(node) && !hasNewParensException && !(
449
450 // Allow extra parens around a new expression if they are intervening parentheses.
451 node.type === "NewExpression" &&
452 callee.type === "MemberExpression" &&
453 doesMemberExpressionContainCallExpression(callee)
454 )
455 ) {
456 report(node.callee);
457 }
458 }
459 node.arguments
460 .filter(arg => hasExcessParensWithPrecedence(arg, PRECEDENCE_OF_ASSIGNMENT_EXPR))
461 .forEach(report);
462 }
463
464 /**
465 * Evaluate binary logicals
466 * @param {ASTNode} node node to evaluate
467 * @returns {void}
468 * @private
469 */
470 function checkBinaryLogical(node) {
471 const prec = precedence(node);
472 const leftPrecedence = precedence(node.left);
473 const rightPrecedence = precedence(node.right);
474 const isExponentiation = node.operator === "**";
475 const shouldSkipLeft = NESTED_BINARY && (node.left.type === "BinaryExpression" || node.left.type === "LogicalExpression");
476 const shouldSkipRight = NESTED_BINARY && (node.right.type === "BinaryExpression" || node.right.type === "LogicalExpression");
477
478 if (!shouldSkipLeft && hasExcessParens(node.left)) {
479 if (
480 !(node.left.type === "UnaryExpression" && isExponentiation) &&
481 (leftPrecedence > prec || (leftPrecedence === prec && !isExponentiation)) ||
482 isParenthesisedTwice(node.left)
483 ) {
484 report(node.left);
485 }
486 }
487
488 if (!shouldSkipRight && hasExcessParens(node.right)) {
489 if (
490 (rightPrecedence > prec || (rightPrecedence === prec && isExponentiation)) ||
491 isParenthesisedTwice(node.right)
492 ) {
493 report(node.right);
494 }
495 }
496 }
497
498 /**
499 * Check the parentheses around the super class of the given class definition.
500 * @param {ASTNode} node The node of class declarations to check.
501 * @returns {void}
502 */
503 function checkClass(node) {
504 if (!node.superClass) {
505 return;
506 }
507
508 /*
509 * If `node.superClass` is a LeftHandSideExpression, parentheses are extra.
510 * Otherwise, parentheses are needed.
511 */
512 const hasExtraParens = precedence(node.superClass) > PRECEDENCE_OF_UPDATE_EXPR
513 ? hasExcessParens(node.superClass)
514 : hasDoubleExcessParens(node.superClass);
515
516 if (hasExtraParens) {
517 report(node.superClass);
518 }
519 }
520
521 /**
522 * Check the parentheses around the argument of the given spread operator.
523 * @param {ASTNode} node The node of spread elements/properties to check.
524 * @returns {void}
525 */
526 function checkSpreadOperator(node) {
527 if (hasExcessParensWithPrecedence(node.argument, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
528 report(node.argument);
529 }
530 }
531
532 /**
533 * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration
534 * @param {ASTNode} node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node
535 * @returns {void}
536 */
537 function checkExpressionOrExportStatement(node) {
538 const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node);
539 const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken);
540 const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null;
541 const tokenAfterClosingParens = secondToken ? sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken) : null;
542
543 if (
544 astUtils.isOpeningParenToken(firstToken) &&
545 (
546 astUtils.isOpeningBraceToken(secondToken) ||
547 secondToken.type === "Keyword" && (
548 secondToken.value === "function" ||
549 secondToken.value === "class" ||
550 secondToken.value === "let" &&
551 tokenAfterClosingParens &&
552 (
553 astUtils.isOpeningBracketToken(tokenAfterClosingParens) ||
554 tokenAfterClosingParens.type === "Identifier"
555 )
556 ) ||
557 secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function"
558 )
559 ) {
560 tokensToIgnore.add(secondToken);
561 }
562
563 if (hasExcessParens(node)) {
564 report(node);
565 }
566 }
567
568 /**
569 * Finds the path from the given node to the specified ancestor.
570 * @param {ASTNode} node First node in the path.
571 * @param {ASTNode} ancestor Last node in the path.
572 * @returns {ASTNode[]} Path, including both nodes.
573 * @throws {Error} If the given node does not have the specified ancestor.
574 */
575 function pathToAncestor(node, ancestor) {
576 const path = [node];
577 let currentNode = node;
578
579 while (currentNode !== ancestor) {
580
581 currentNode = currentNode.parent;
582
583 /* istanbul ignore if */
584 if (currentNode === null) {
585 throw new Error("Nodes are not in the ancestor-descendant relationship.");
586 }
587
588 path.push(currentNode);
589 }
590
591 return path;
592 }
593
594 /**
595 * Finds the path from the given node to the specified descendant.
596 * @param {ASTNode} node First node in the path.
597 * @param {ASTNode} descendant Last node in the path.
598 * @returns {ASTNode[]} Path, including both nodes.
599 * @throws {Error} If the given node does not have the specified descendant.
600 */
601 function pathToDescendant(node, descendant) {
602 return pathToAncestor(descendant, node).reverse();
603 }
604
605 /**
606 * Checks whether the syntax of the given ancestor of an 'in' expression inside a for-loop initializer
607 * is preventing the 'in' keyword from being interpreted as a part of an ill-formed for-in loop.
608 * @param {ASTNode} node Ancestor of an 'in' expression.
609 * @param {ASTNode} child Child of the node, ancestor of the same 'in' expression or the 'in' expression itself.
610 * @returns {boolean} True if the keyword 'in' would be interpreted as the 'in' operator, without any parenthesis.
611 */
612 function isSafelyEnclosingInExpression(node, child) {
613 switch (node.type) {
614 case "ArrayExpression":
615 case "ArrayPattern":
616 case "BlockStatement":
617 case "ObjectExpression":
618 case "ObjectPattern":
619 case "TemplateLiteral":
620 return true;
621 case "ArrowFunctionExpression":
622 case "FunctionExpression":
623 return node.params.includes(child);
624 case "CallExpression":
625 case "NewExpression":
626 return node.arguments.includes(child);
627 case "MemberExpression":
628 return node.computed && node.property === child;
629 case "ConditionalExpression":
630 return node.consequent === child;
631 default:
632 return false;
633 }
634 }
635
636 /**
637 * Starts a new reports buffering. Warnings will be stored in a buffer instead of being reported immediately.
638 * An additional logic that requires multiple nodes (e.g. a whole subtree) may dismiss some of the stored warnings.
639 * @returns {void}
640 */
641 function startNewReportsBuffering() {
642 reportsBuffer = {
643 upper: reportsBuffer,
644 inExpressionNodes: [],
645 reports: []
646 };
647 }
648
649 /**
650 * Ends the current reports buffering.
651 * @returns {void}
652 */
653 function endCurrentReportsBuffering() {
654 const { upper, inExpressionNodes, reports } = reportsBuffer;
655
656 if (upper) {
657 upper.inExpressionNodes.push(...inExpressionNodes);
658 upper.reports.push(...reports);
659 } else {
660
661 // flush remaining reports
662 reports.forEach(({ finishReport }) => finishReport());
663 }
664
665 reportsBuffer = upper;
666 }
667
668 /**
669 * Checks whether the given node is in the current reports buffer.
670 * @param {ASTNode} node Node to check.
671 * @returns {boolean} True if the node is in the current buffer, false otherwise.
672 */
673 function isInCurrentReportsBuffer(node) {
674 return reportsBuffer.reports.some(r => r.node === node);
675 }
676
677 /**
678 * Removes the given node from the current reports buffer.
679 * @param {ASTNode} node Node to remove.
680 * @returns {void}
681 */
682 function removeFromCurrentReportsBuffer(node) {
683 reportsBuffer.reports = reportsBuffer.reports.filter(r => r.node !== node);
684 }
685
686 return {
687 ArrayExpression(node) {
688 node.elements
689 .filter(e => e && hasExcessParensWithPrecedence(e, PRECEDENCE_OF_ASSIGNMENT_EXPR))
690 .forEach(report);
691 },
692
693 ArrayPattern(node) {
694 node.elements
695 .filter(e => canBeAssignmentTarget(e) && hasExcessParens(e))
696 .forEach(report);
697 },
698
699 ArrowFunctionExpression(node) {
700 if (isReturnAssignException(node)) {
701 return;
702 }
703
704 if (node.body.type === "ConditionalExpression" &&
705 IGNORE_ARROW_CONDITIONALS
706 ) {
707 return;
708 }
709
710 if (node.body.type !== "BlockStatement") {
711 const firstBodyToken = sourceCode.getFirstToken(node.body, astUtils.isNotOpeningParenToken);
712 const tokenBeforeFirst = sourceCode.getTokenBefore(firstBodyToken);
713
714 if (astUtils.isOpeningParenToken(tokenBeforeFirst) && astUtils.isOpeningBraceToken(firstBodyToken)) {
715 tokensToIgnore.add(firstBodyToken);
716 }
717 if (hasExcessParensWithPrecedence(node.body, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
718 report(node.body);
719 }
720 }
721 },
722
723 AssignmentExpression(node) {
724 if (canBeAssignmentTarget(node.left) && hasExcessParens(node.left)) {
725 report(node.left);
726 }
727
728 if (!isReturnAssignException(node) && hasExcessParensWithPrecedence(node.right, precedence(node))) {
729 report(node.right);
730 }
731 },
732
733 BinaryExpression(node) {
734 if (reportsBuffer && node.operator === "in") {
735 reportsBuffer.inExpressionNodes.push(node);
736 }
737
738 checkBinaryLogical(node);
739 },
740
741 CallExpression: checkCallNew,
742
743 ClassBody(node) {
744 node.body
745 .filter(member => member.type === "MethodDefinition" && member.computed && member.key)
746 .filter(member => hasExcessParensWithPrecedence(member.key, PRECEDENCE_OF_ASSIGNMENT_EXPR))
747 .forEach(member => report(member.key));
748 },
749
750 ConditionalExpression(node) {
751 if (isReturnAssignException(node)) {
752 return;
753 }
754 if (
755 !isCondAssignException(node) &&
756 hasExcessParensWithPrecedence(node.test, precedence({ type: "LogicalExpression", operator: "||" }))
757 ) {
758 report(node.test);
759 }
760
761 if (hasExcessParensWithPrecedence(node.consequent, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
762 report(node.consequent);
763 }
764
765 if (hasExcessParensWithPrecedence(node.alternate, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
766 report(node.alternate);
767 }
768 },
769
770 DoWhileStatement(node) {
771 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
772 report(node.test);
773 }
774 },
775
776 ExportDefaultDeclaration: node => checkExpressionOrExportStatement(node.declaration),
777 ExpressionStatement: node => checkExpressionOrExportStatement(node.expression),
778
779 "ForInStatement, ForOfStatement"(node) {
780 if (node.left.type !== "VariableDeclarator") {
781 const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken);
782
783 if (
784 firstLeftToken.value === "let" && (
785
786 /*
787 * If `let` is the only thing on the left side of the loop, it's the loop variable: `for ((let) of foo);`
788 * Removing it will cause a syntax error, because it will be parsed as the start of a VariableDeclarator.
789 */
790 (firstLeftToken.range[1] === node.left.range[1] || /*
791 * If `let` is followed by a `[` token, it's a property access on the `let` value: `for ((let[foo]) of bar);`
792 * Removing it will cause the property access to be parsed as a destructuring declaration of `foo` instead.
793 */
794 astUtils.isOpeningBracketToken(
795 sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken)
796 ))
797 )
798 ) {
799 tokensToIgnore.add(firstLeftToken);
800 }
801 }
802
803 if (node.type === "ForOfStatement") {
804 const hasExtraParens = node.right.type === "SequenceExpression"
805 ? hasDoubleExcessParens(node.right)
806 : hasExcessParens(node.right);
807
808 if (hasExtraParens) {
809 report(node.right);
810 }
811 } else if (hasExcessParens(node.right)) {
812 report(node.right);
813 }
814
815 if (hasExcessParens(node.left)) {
816 report(node.left);
817 }
818 },
819
820 ForStatement(node) {
821 if (node.test && hasExcessParens(node.test) && !isCondAssignException(node)) {
822 report(node.test);
823 }
824
825 if (node.update && hasExcessParens(node.update)) {
826 report(node.update);
827 }
828
829 if (node.init) {
830 startNewReportsBuffering();
831
832 if (hasExcessParens(node.init)) {
833 report(node.init);
834 }
835 }
836 },
837
838 "ForStatement > *.init:exit"(node) {
839
840 /*
841 * Removing parentheses around `in` expressions might change semantics and cause errors.
842 *
843 * For example, this valid for loop:
844 * for (let a = (b in c); ;);
845 * after removing parentheses would be treated as an invalid for-in loop:
846 * for (let a = b in c; ;);
847 */
848
849 if (reportsBuffer.reports.length) {
850 reportsBuffer.inExpressionNodes.forEach(inExpressionNode => {
851 const path = pathToDescendant(node, inExpressionNode);
852 let nodeToExclude;
853
854 for (let i = 0; i < path.length; i++) {
855 const pathNode = path[i];
856
857 if (i < path.length - 1) {
858 const nextPathNode = path[i + 1];
859
860 if (isSafelyEnclosingInExpression(pathNode, nextPathNode)) {
861
862 // The 'in' expression in safely enclosed by the syntax of its ancestor nodes (e.g. by '{}' or '[]').
863 return;
864 }
865 }
866
867 if (isParenthesised(pathNode)) {
868 if (isInCurrentReportsBuffer(pathNode)) {
869
870 // This node was supposed to be reported, but parentheses might be necessary.
871
872 if (isParenthesisedTwice(pathNode)) {
873
874 /*
875 * This node is parenthesised twice, it certainly has at least one pair of `extra` parentheses.
876 * If the --fix option is on, the current fixing iteration will remove only one pair of parentheses.
877 * The remaining pair is safely enclosing the 'in' expression.
878 */
879 return;
880 }
881
882 // Exclude the outermost node only.
883 if (!nodeToExclude) {
884 nodeToExclude = pathNode;
885 }
886
887 // Don't break the loop here, there might be some safe nodes or parentheses that will stay inside.
888
889 } else {
890
891 // This node will stay parenthesised, the 'in' expression in safely enclosed by '()'.
892 return;
893 }
894 }
895 }
896
897 // Exclude the node from the list (i.e. treat parentheses as necessary)
898 removeFromCurrentReportsBuffer(nodeToExclude);
899 });
900 }
901
902 endCurrentReportsBuffering();
903 },
904
905 IfStatement(node) {
906 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
907 report(node.test);
908 }
909 },
910
911 ImportExpression(node) {
912 const { source } = node;
913
914 if (source.type === "SequenceExpression") {
915 if (hasDoubleExcessParens(source)) {
916 report(source);
917 }
918 } else if (hasExcessParens(source)) {
919 report(source);
920 }
921 },
922
923 LogicalExpression: checkBinaryLogical,
924
925 MemberExpression(node) {
926 const nodeObjHasExcessParens = hasExcessParens(node.object);
927
928 if (
929 nodeObjHasExcessParens &&
930 precedence(node.object) >= precedence(node) &&
931 (
932 node.computed ||
933 !(
934 astUtils.isDecimalInteger(node.object) ||
935
936 // RegExp literal is allowed to have parens (#1589)
937 (node.object.type === "Literal" && node.object.regex)
938 )
939 )
940 ) {
941 report(node.object);
942 }
943
944 if (nodeObjHasExcessParens &&
945 node.object.type === "CallExpression" &&
946 node.parent.type !== "NewExpression") {
947 report(node.object);
948 }
949
950 if (nodeObjHasExcessParens &&
951 !IGNORE_NEW_IN_MEMBER_EXPR &&
952 node.object.type === "NewExpression" &&
953 isNewExpressionWithParens(node.object)) {
954 report(node.object);
955 }
956
957 if (node.computed && hasExcessParens(node.property)) {
958 report(node.property);
959 }
960 },
961
962 NewExpression: checkCallNew,
963
964 ObjectExpression(node) {
965 node.properties
966 .filter(property => property.value && hasExcessParensWithPrecedence(property.value, PRECEDENCE_OF_ASSIGNMENT_EXPR))
967 .forEach(property => report(property.value));
968 },
969
970 ObjectPattern(node) {
971 node.properties
972 .filter(property => {
973 const value = property.value;
974
975 return canBeAssignmentTarget(value) && hasExcessParens(value);
976 }).forEach(property => report(property.value));
977 },
978
979 Property(node) {
980 if (node.computed) {
981 const { key } = node;
982
983 if (key && hasExcessParensWithPrecedence(key, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
984 report(key);
985 }
986 }
987 },
988
989 RestElement(node) {
990 const argument = node.argument;
991
992 if (canBeAssignmentTarget(argument) && hasExcessParens(argument)) {
993 report(argument);
994 }
995 },
996
997 ReturnStatement(node) {
998 const returnToken = sourceCode.getFirstToken(node);
999
1000 if (isReturnAssignException(node)) {
1001 return;
1002 }
1003
1004 if (node.argument &&
1005 hasExcessParensNoLineTerminator(returnToken, node.argument) &&
1006
1007 // RegExp literal is allowed to have parens (#1589)
1008 !(node.argument.type === "Literal" && node.argument.regex)) {
1009 report(node.argument);
1010 }
1011 },
1012
1013 SequenceExpression(node) {
1014 const precedenceOfNode = precedence(node);
1015
1016 node.expressions
1017 .filter(e => hasExcessParensWithPrecedence(e, precedenceOfNode))
1018 .forEach(report);
1019 },
1020
1021 SwitchCase(node) {
1022 if (node.test && hasExcessParens(node.test)) {
1023 report(node.test);
1024 }
1025 },
1026
1027 SwitchStatement(node) {
1028 if (hasExcessParens(node.discriminant)) {
1029 report(node.discriminant);
1030 }
1031 },
1032
1033 ThrowStatement(node) {
1034 const throwToken = sourceCode.getFirstToken(node);
1035
1036 if (hasExcessParensNoLineTerminator(throwToken, node.argument)) {
1037 report(node.argument);
1038 }
1039 },
1040
1041 UnaryExpression: checkArgumentWithPrecedence,
1042 UpdateExpression: checkArgumentWithPrecedence,
1043 AwaitExpression: checkArgumentWithPrecedence,
1044
1045 VariableDeclarator(node) {
1046 if (
1047 node.init && hasExcessParensWithPrecedence(node.init, PRECEDENCE_OF_ASSIGNMENT_EXPR) &&
1048
1049 // RegExp literal is allowed to have parens (#1589)
1050 !(node.init.type === "Literal" && node.init.regex)
1051 ) {
1052 report(node.init);
1053 }
1054 },
1055
1056 WhileStatement(node) {
1057 if (hasExcessParens(node.test) && !isCondAssignException(node)) {
1058 report(node.test);
1059 }
1060 },
1061
1062 WithStatement(node) {
1063 if (hasExcessParens(node.object)) {
1064 report(node.object);
1065 }
1066 },
1067
1068 YieldExpression(node) {
1069 if (node.argument) {
1070 const yieldToken = sourceCode.getFirstToken(node);
1071
1072 if ((precedence(node.argument) >= precedence(node) &&
1073 hasExcessParensNoLineTerminator(yieldToken, node.argument)) ||
1074 hasDoubleExcessParens(node.argument)) {
1075 report(node.argument);
1076 }
1077 }
1078 },
1079
1080 ClassDeclaration: checkClass,
1081 ClassExpression: checkClass,
1082
1083 SpreadElement: checkSpreadOperator,
1084 SpreadProperty: checkSpreadOperator,
1085 ExperimentalSpreadProperty: checkSpreadOperator,
1086
1087 TemplateLiteral(node) {
1088 node.expressions
1089 .filter(e => e && hasExcessParens(e))
1090 .forEach(report);
1091 },
1092
1093 AssignmentPattern(node) {
1094 const { left, right } = node;
1095
1096 if (canBeAssignmentTarget(left) && hasExcessParens(left)) {
1097 report(left);
1098 }
1099
1100 if (right && hasExcessParensWithPrecedence(right, PRECEDENCE_OF_ASSIGNMENT_EXPR)) {
1101 report(right);
1102 }
1103 }
1104 };
1105
1106 }
1107 };