项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.
 
 
 
 

752 строки
29 KiB

  1. /**
  2. * @fileoverview Disallow parenthesising higher precedence subexpressions.
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils.js");
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow unnecessary parentheses",
  14. category: "Possible Errors",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-extra-parens"
  17. },
  18. fixable: "code",
  19. schema: {
  20. anyOf: [
  21. {
  22. type: "array",
  23. items: [
  24. {
  25. enum: ["functions"]
  26. }
  27. ],
  28. minItems: 0,
  29. maxItems: 1
  30. },
  31. {
  32. type: "array",
  33. items: [
  34. {
  35. enum: ["all"]
  36. },
  37. {
  38. type: "object",
  39. properties: {
  40. conditionalAssign: { type: "boolean" },
  41. nestedBinaryExpressions: { type: "boolean" },
  42. returnAssign: { type: "boolean" },
  43. ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
  44. enforceForArrowConditionals: { type: "boolean" }
  45. },
  46. additionalProperties: false
  47. }
  48. ],
  49. minItems: 0,
  50. maxItems: 2
  51. }
  52. ]
  53. },
  54. messages: {
  55. unexpected: "Gratuitous parentheses around expression."
  56. }
  57. },
  58. create(context) {
  59. const sourceCode = context.getSourceCode();
  60. const tokensToIgnore = new WeakSet();
  61. const isParenthesised = astUtils.isParenthesised.bind(astUtils, sourceCode);
  62. const precedence = astUtils.getPrecedence;
  63. const ALL_NODES = context.options[0] !== "functions";
  64. const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false;
  65. const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false;
  66. const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false;
  67. const IGNORE_JSX = ALL_NODES && context.options[1] && context.options[1].ignoreJSX;
  68. const IGNORE_ARROW_CONDITIONALS = ALL_NODES && context.options[1] &&
  69. context.options[1].enforceForArrowConditionals === false;
  70. const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
  71. const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
  72. /**
  73. * Determines if this rule should be enforced for a node given the current configuration.
  74. * @param {ASTNode} node - The node to be checked.
  75. * @returns {boolean} True if the rule should be enforced for this node.
  76. * @private
  77. */
  78. function ruleApplies(node) {
  79. if (node.type === "JSXElement") {
  80. const isSingleLine = node.loc.start.line === node.loc.end.line;
  81. switch (IGNORE_JSX) {
  82. // Exclude this JSX element from linting
  83. case "all":
  84. return false;
  85. // Exclude this JSX element if it is multi-line element
  86. case "multi-line":
  87. return isSingleLine;
  88. // Exclude this JSX element if it is single-line element
  89. case "single-line":
  90. return !isSingleLine;
  91. // Nothing special to be done for JSX elements
  92. case "none":
  93. break;
  94. // no default
  95. }
  96. }
  97. return ALL_NODES || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
  98. }
  99. /**
  100. * Determines if a node is surrounded by parentheses twice.
  101. * @param {ASTNode} node - The node to be checked.
  102. * @returns {boolean} True if the node is doubly parenthesised.
  103. * @private
  104. */
  105. function isParenthesisedTwice(node) {
  106. const previousToken = sourceCode.getTokenBefore(node, 1),
  107. nextToken = sourceCode.getTokenAfter(node, 1);
  108. return isParenthesised(node) && previousToken && nextToken &&
  109. astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
  110. astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
  111. }
  112. /**
  113. * Determines if a node is surrounded by (potentially) invalid parentheses.
  114. * @param {ASTNode} node - The node to be checked.
  115. * @returns {boolean} True if the node is incorrectly parenthesised.
  116. * @private
  117. */
  118. function hasExcessParens(node) {
  119. return ruleApplies(node) && isParenthesised(node);
  120. }
  121. /**
  122. * Determines if a node that is expected to be parenthesised is surrounded by
  123. * (potentially) invalid extra parentheses.
  124. * @param {ASTNode} node - The node to be checked.
  125. * @returns {boolean} True if the node is has an unexpected extra pair of parentheses.
  126. * @private
  127. */
  128. function hasDoubleExcessParens(node) {
  129. return ruleApplies(node) && isParenthesisedTwice(node);
  130. }
  131. /**
  132. * Determines if a node test expression is allowed to have a parenthesised assignment
  133. * @param {ASTNode} node - The node to be checked.
  134. * @returns {boolean} True if the assignment can be parenthesised.
  135. * @private
  136. */
  137. function isCondAssignException(node) {
  138. return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression";
  139. }
  140. /**
  141. * Determines if a node is in a return statement
  142. * @param {ASTNode} node - The node to be checked.
  143. * @returns {boolean} True if the node is in a return statement.
  144. * @private
  145. */
  146. function isInReturnStatement(node) {
  147. for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
  148. if (
  149. currentNode.type === "ReturnStatement" ||
  150. (currentNode.type === "ArrowFunctionExpression" && currentNode.body.type !== "BlockStatement")
  151. ) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * Determines if a constructor function is newed-up with parens
  159. * @param {ASTNode} newExpression - The NewExpression node to be checked.
  160. * @returns {boolean} True if the constructor is called with parens.
  161. * @private
  162. */
  163. function isNewExpressionWithParens(newExpression) {
  164. const lastToken = sourceCode.getLastToken(newExpression);
  165. const penultimateToken = sourceCode.getTokenBefore(lastToken);
  166. return newExpression.arguments.length > 0 || astUtils.isOpeningParenToken(penultimateToken) && astUtils.isClosingParenToken(lastToken);
  167. }
  168. /**
  169. * Determines if a node is or contains an assignment expression
  170. * @param {ASTNode} node - The node to be checked.
  171. * @returns {boolean} True if the node is or contains an assignment expression.
  172. * @private
  173. */
  174. function containsAssignment(node) {
  175. if (node.type === "AssignmentExpression") {
  176. return true;
  177. }
  178. if (node.type === "ConditionalExpression" &&
  179. (node.consequent.type === "AssignmentExpression" || node.alternate.type === "AssignmentExpression")) {
  180. return true;
  181. }
  182. if ((node.left && node.left.type === "AssignmentExpression") ||
  183. (node.right && node.right.type === "AssignmentExpression")) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment
  190. * @param {ASTNode} node - The node to be checked.
  191. * @returns {boolean} True if the assignment can be parenthesised.
  192. * @private
  193. */
  194. function isReturnAssignException(node) {
  195. if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) {
  196. return false;
  197. }
  198. if (node.type === "ReturnStatement") {
  199. return node.argument && containsAssignment(node.argument);
  200. }
  201. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  202. return containsAssignment(node.body);
  203. }
  204. return containsAssignment(node);
  205. }
  206. /**
  207. * Determines if a node following a [no LineTerminator here] restriction is
  208. * surrounded by (potentially) invalid extra parentheses.
  209. * @param {Token} token - The token preceding the [no LineTerminator here] restriction.
  210. * @param {ASTNode} node - The node to be checked.
  211. * @returns {boolean} True if the node is incorrectly parenthesised.
  212. * @private
  213. */
  214. function hasExcessParensNoLineTerminator(token, node) {
  215. if (token.loc.end.line === node.loc.start.line) {
  216. return hasExcessParens(node);
  217. }
  218. return hasDoubleExcessParens(node);
  219. }
  220. /**
  221. * Determines whether a node should be preceded by an additional space when removing parens
  222. * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
  223. * @returns {boolean} `true` if a space should be inserted before the node
  224. * @private
  225. */
  226. function requiresLeadingSpace(node) {
  227. const leftParenToken = sourceCode.getTokenBefore(node);
  228. const tokenBeforeLeftParen = sourceCode.getTokenBefore(node, 1);
  229. const firstToken = sourceCode.getFirstToken(node);
  230. return tokenBeforeLeftParen &&
  231. tokenBeforeLeftParen.range[1] === leftParenToken.range[0] &&
  232. leftParenToken.range[1] === firstToken.range[0] &&
  233. !astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, firstToken);
  234. }
  235. /**
  236. * Determines whether a node should be followed by an additional space when removing parens
  237. * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
  238. * @returns {boolean} `true` if a space should be inserted after the node
  239. * @private
  240. */
  241. function requiresTrailingSpace(node) {
  242. const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 });
  243. const rightParenToken = nextTwoTokens[0];
  244. const tokenAfterRightParen = nextTwoTokens[1];
  245. const tokenBeforeRightParen = sourceCode.getLastToken(node);
  246. return rightParenToken && tokenAfterRightParen &&
  247. !sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) &&
  248. !astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen);
  249. }
  250. /**
  251. * Determines if a given expression node is an IIFE
  252. * @param {ASTNode} node The node to check
  253. * @returns {boolean} `true` if the given node is an IIFE
  254. */
  255. function isIIFE(node) {
  256. return node.type === "CallExpression" && node.callee.type === "FunctionExpression";
  257. }
  258. /**
  259. * Report the node
  260. * @param {ASTNode} node node to evaluate
  261. * @returns {void}
  262. * @private
  263. */
  264. function report(node) {
  265. const leftParenToken = sourceCode.getTokenBefore(node);
  266. const rightParenToken = sourceCode.getTokenAfter(node);
  267. if (!isParenthesisedTwice(node)) {
  268. if (tokensToIgnore.has(sourceCode.getFirstToken(node))) {
  269. return;
  270. }
  271. if (isIIFE(node) && !isParenthesised(node.callee)) {
  272. return;
  273. }
  274. }
  275. context.report({
  276. node,
  277. loc: leftParenToken.loc.start,
  278. messageId: "unexpected",
  279. fix(fixer) {
  280. const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]);
  281. return fixer.replaceTextRange([
  282. leftParenToken.range[0],
  283. rightParenToken.range[1]
  284. ], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : ""));
  285. }
  286. });
  287. }
  288. /**
  289. * Evaluate Unary update
  290. * @param {ASTNode} node node to evaluate
  291. * @returns {void}
  292. * @private
  293. */
  294. function checkUnaryUpdate(node) {
  295. if (node.type === "UnaryExpression" && node.argument.type === "BinaryExpression" && node.argument.operator === "**") {
  296. return;
  297. }
  298. if (hasExcessParens(node.argument) && precedence(node.argument) >= precedence(node)) {
  299. report(node.argument);
  300. }
  301. }
  302. /**
  303. * Check if a member expression contains a call expression
  304. * @param {ASTNode} node MemberExpression node to evaluate
  305. * @returns {boolean} true if found, false if not
  306. */
  307. function doesMemberExpressionContainCallExpression(node) {
  308. let currentNode = node.object;
  309. let currentNodeType = node.object.type;
  310. while (currentNodeType === "MemberExpression") {
  311. currentNode = currentNode.object;
  312. currentNodeType = currentNode.type;
  313. }
  314. return currentNodeType === "CallExpression";
  315. }
  316. /**
  317. * Evaluate a new call
  318. * @param {ASTNode} node node to evaluate
  319. * @returns {void}
  320. * @private
  321. */
  322. function checkCallNew(node) {
  323. const callee = node.callee;
  324. if (hasExcessParens(callee) && precedence(callee) >= precedence(node)) {
  325. const hasNewParensException = callee.type === "NewExpression" && !isNewExpressionWithParens(callee);
  326. if (
  327. hasDoubleExcessParens(callee) ||
  328. !isIIFE(node) && !hasNewParensException && !(
  329. /*
  330. * Allow extra parens around a new expression if
  331. * there are intervening parentheses.
  332. */
  333. callee.type === "MemberExpression" &&
  334. doesMemberExpressionContainCallExpression(callee)
  335. )
  336. ) {
  337. report(node.callee);
  338. }
  339. }
  340. if (node.arguments.length === 1) {
  341. if (hasDoubleExcessParens(node.arguments[0]) && precedence(node.arguments[0]) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
  342. report(node.arguments[0]);
  343. }
  344. } else {
  345. node.arguments
  346. .filter(arg => hasExcessParens(arg) && precedence(arg) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
  347. .forEach(report);
  348. }
  349. }
  350. /**
  351. * Evaluate binary logicals
  352. * @param {ASTNode} node node to evaluate
  353. * @returns {void}
  354. * @private
  355. */
  356. function checkBinaryLogical(node) {
  357. const prec = precedence(node);
  358. const leftPrecedence = precedence(node.left);
  359. const rightPrecedence = precedence(node.right);
  360. const isExponentiation = node.operator === "**";
  361. const shouldSkipLeft = (NESTED_BINARY && (node.left.type === "BinaryExpression" || node.left.type === "LogicalExpression")) ||
  362. node.left.type === "UnaryExpression" && isExponentiation;
  363. const shouldSkipRight = NESTED_BINARY && (node.right.type === "BinaryExpression" || node.right.type === "LogicalExpression");
  364. if (!shouldSkipLeft && hasExcessParens(node.left) && (leftPrecedence > prec || (leftPrecedence === prec && !isExponentiation))) {
  365. report(node.left);
  366. }
  367. if (!shouldSkipRight && hasExcessParens(node.right) && (rightPrecedence > prec || (rightPrecedence === prec && isExponentiation))) {
  368. report(node.right);
  369. }
  370. }
  371. /**
  372. * Check the parentheses around the super class of the given class definition.
  373. * @param {ASTNode} node The node of class declarations to check.
  374. * @returns {void}
  375. */
  376. function checkClass(node) {
  377. if (!node.superClass) {
  378. return;
  379. }
  380. /*
  381. * If `node.superClass` is a LeftHandSideExpression, parentheses are extra.
  382. * Otherwise, parentheses are needed.
  383. */
  384. const hasExtraParens = precedence(node.superClass) > PRECEDENCE_OF_UPDATE_EXPR
  385. ? hasExcessParens(node.superClass)
  386. : hasDoubleExcessParens(node.superClass);
  387. if (hasExtraParens) {
  388. report(node.superClass);
  389. }
  390. }
  391. /**
  392. * Check the parentheses around the argument of the given spread operator.
  393. * @param {ASTNode} node The node of spread elements/properties to check.
  394. * @returns {void}
  395. */
  396. function checkSpreadOperator(node) {
  397. const hasExtraParens = precedence(node.argument) >= PRECEDENCE_OF_ASSIGNMENT_EXPR
  398. ? hasExcessParens(node.argument)
  399. : hasDoubleExcessParens(node.argument);
  400. if (hasExtraParens) {
  401. report(node.argument);
  402. }
  403. }
  404. /**
  405. * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration
  406. * @param {ASTNode} node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node
  407. * @returns {void}
  408. */
  409. function checkExpressionOrExportStatement(node) {
  410. const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node) : sourceCode.getFirstToken(node);
  411. const secondToken = sourceCode.getTokenAfter(firstToken, astUtils.isNotOpeningParenToken);
  412. const thirdToken = secondToken ? sourceCode.getTokenAfter(secondToken) : null;
  413. if (
  414. astUtils.isOpeningParenToken(firstToken) &&
  415. (
  416. astUtils.isOpeningBraceToken(secondToken) ||
  417. secondToken.type === "Keyword" && (
  418. secondToken.value === "function" ||
  419. secondToken.value === "class" ||
  420. secondToken.value === "let" && astUtils.isOpeningBracketToken(sourceCode.getTokenAfter(secondToken, astUtils.isNotClosingParenToken))
  421. ) ||
  422. secondToken && secondToken.type === "Identifier" && secondToken.value === "async" && thirdToken && thirdToken.type === "Keyword" && thirdToken.value === "function"
  423. )
  424. ) {
  425. tokensToIgnore.add(secondToken);
  426. }
  427. if (hasExcessParens(node)) {
  428. report(node);
  429. }
  430. }
  431. return {
  432. ArrayExpression(node) {
  433. node.elements
  434. .filter(e => e && hasExcessParens(e) && precedence(e) >= PRECEDENCE_OF_ASSIGNMENT_EXPR)
  435. .forEach(report);
  436. },
  437. ArrowFunctionExpression(node) {
  438. if (isReturnAssignException(node)) {
  439. return;
  440. }
  441. if (node.body.type === "ConditionalExpression" &&
  442. IGNORE_ARROW_CONDITIONALS &&
  443. !isParenthesisedTwice(node.body)
  444. ) {
  445. return;
  446. }
  447. if (node.body.type !== "BlockStatement") {
  448. const firstBodyToken = sourceCode.getFirstToken(node.body, astUtils.isNotOpeningParenToken);
  449. const tokenBeforeFirst = sourceCode.getTokenBefore(firstBodyToken);
  450. if (astUtils.isOpeningParenToken(tokenBeforeFirst) && astUtils.isOpeningBraceToken(firstBodyToken)) {
  451. tokensToIgnore.add(firstBodyToken);
  452. }
  453. if (hasExcessParens(node.body) && precedence(node.body) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
  454. report(node.body);
  455. }
  456. }
  457. },
  458. AssignmentExpression(node) {
  459. if (isReturnAssignException(node)) {
  460. return;
  461. }
  462. if (hasExcessParens(node.right) && precedence(node.right) >= precedence(node)) {
  463. report(node.right);
  464. }
  465. },
  466. BinaryExpression: checkBinaryLogical,
  467. CallExpression: checkCallNew,
  468. ConditionalExpression(node) {
  469. if (isReturnAssignException(node)) {
  470. return;
  471. }
  472. if (hasExcessParens(node.test) && precedence(node.test) >= precedence({ type: "LogicalExpression", operator: "||" })) {
  473. report(node.test);
  474. }
  475. if (hasExcessParens(node.consequent) && precedence(node.consequent) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
  476. report(node.consequent);
  477. }
  478. if (hasExcessParens(node.alternate) && precedence(node.alternate) >= PRECEDENCE_OF_ASSIGNMENT_EXPR) {
  479. report(node.alternate);
  480. }
  481. },
  482. DoWhileStatement(node) {
  483. if (hasDoubleExcessParens(node.test) && !isCondAssignException(node)) {
  484. report(node.test);
  485. }
  486. },
  487. ExportDefaultDeclaration: node => checkExpressionOrExportStatement(node.declaration),
  488. ExpressionStatement: node => checkExpressionOrExportStatement(node.expression),
  489. "ForInStatement, ForOfStatement"(node) {
  490. if (node.left.type !== "VariableDeclarator") {
  491. const firstLeftToken = sourceCode.getFirstToken(node.left, astUtils.isNotOpeningParenToken);
  492. if (
  493. firstLeftToken.value === "let" && (
  494. /*
  495. * If `let` is the only thing on the left side of the loop, it's the loop variable: `for ((let) of foo);`
  496. * Removing it will cause a syntax error, because it will be parsed as the start of a VariableDeclarator.
  497. */
  498. firstLeftToken.range[1] === node.left.range[1] ||
  499. /*
  500. * If `let` is followed by a `[` token, it's a property access on the `let` value: `for ((let[foo]) of bar);`
  501. * Removing it will cause the property access to be parsed as a destructuring declaration of `foo` instead.
  502. */
  503. astUtils.isOpeningBracketToken(
  504. sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken)
  505. )
  506. )
  507. ) {
  508. tokensToIgnore.add(firstLeftToken);
  509. }
  510. }
  511. if (!(node.type === "ForOfStatement" && node.right.type === "SequenceExpression") && hasExcessParens(node.right)) {
  512. report(node.right);
  513. }
  514. if (hasExcessParens(node.left)) {
  515. report(node.left);
  516. }
  517. },
  518. ForStatement(node) {
  519. if (node.init && hasExcessParens(node.init)) {
  520. report(node.init);
  521. }
  522. if (node.test && hasExcessParens(node.test) && !isCondAssignException(node)) {
  523. report(node.test);
  524. }
  525. if (node.update && hasExcessParens(node.update)) {
  526. report(node.update);
  527. }
  528. },
  529. IfStatement(node) {
  530. if (hasDoubleExcessParens(node.test) && !isCondAssignException(node)) {
  531. report(node.test);
  532. }
  533. },
  534. LogicalExpression: checkBinaryLogical,
  535. MemberExpression(node) {
  536. const nodeObjHasExcessParens = hasExcessParens(node.object);
  537. if (
  538. nodeObjHasExcessParens &&
  539. precedence(node.object) >= precedence(node) &&
  540. (
  541. node.computed ||
  542. !(
  543. astUtils.isDecimalInteger(node.object) ||
  544. // RegExp literal is allowed to have parens (#1589)
  545. (node.object.type === "Literal" && node.object.regex)
  546. )
  547. )
  548. ) {
  549. report(node.object);
  550. }
  551. if (nodeObjHasExcessParens &&
  552. node.object.type === "CallExpression" &&
  553. node.parent.type !== "NewExpression") {
  554. report(node.object);
  555. }
  556. if (node.computed && hasExcessParens(node.property)) {
  557. report(node.property);
  558. }
  559. },
  560. NewExpression: checkCallNew,
  561. ObjectExpression(node) {
  562. node.properties
  563. .filter(property => {
  564. const value = property.value;
  565. return value && hasExcessParens(value) && precedence(value) >= PRECEDENCE_OF_ASSIGNMENT_EXPR;
  566. }).forEach(property => report(property.value));
  567. },
  568. ReturnStatement(node) {
  569. const returnToken = sourceCode.getFirstToken(node);
  570. if (isReturnAssignException(node)) {
  571. return;
  572. }
  573. if (node.argument &&
  574. hasExcessParensNoLineTerminator(returnToken, node.argument) &&
  575. // RegExp literal is allowed to have parens (#1589)
  576. !(node.argument.type === "Literal" && node.argument.regex)) {
  577. report(node.argument);
  578. }
  579. },
  580. SequenceExpression(node) {
  581. node.expressions
  582. .filter(e => hasExcessParens(e) && precedence(e) >= precedence(node))
  583. .forEach(report);
  584. },
  585. SwitchCase(node) {
  586. if (node.test && hasExcessParens(node.test)) {
  587. report(node.test);
  588. }
  589. },
  590. SwitchStatement(node) {
  591. if (hasDoubleExcessParens(node.discriminant)) {
  592. report(node.discriminant);
  593. }
  594. },
  595. ThrowStatement(node) {
  596. const throwToken = sourceCode.getFirstToken(node);
  597. if (hasExcessParensNoLineTerminator(throwToken, node.argument)) {
  598. report(node.argument);
  599. }
  600. },
  601. UnaryExpression: checkUnaryUpdate,
  602. UpdateExpression: checkUnaryUpdate,
  603. AwaitExpression: checkUnaryUpdate,
  604. VariableDeclarator(node) {
  605. if (node.init && hasExcessParens(node.init) &&
  606. precedence(node.init) >= PRECEDENCE_OF_ASSIGNMENT_EXPR &&
  607. // RegExp literal is allowed to have parens (#1589)
  608. !(node.init.type === "Literal" && node.init.regex)) {
  609. report(node.init);
  610. }
  611. },
  612. WhileStatement(node) {
  613. if (hasDoubleExcessParens(node.test) && !isCondAssignException(node)) {
  614. report(node.test);
  615. }
  616. },
  617. WithStatement(node) {
  618. if (hasDoubleExcessParens(node.object)) {
  619. report(node.object);
  620. }
  621. },
  622. YieldExpression(node) {
  623. if (node.argument) {
  624. const yieldToken = sourceCode.getFirstToken(node);
  625. if ((precedence(node.argument) >= precedence(node) &&
  626. hasExcessParensNoLineTerminator(yieldToken, node.argument)) ||
  627. hasDoubleExcessParens(node.argument)) {
  628. report(node.argument);
  629. }
  630. }
  631. },
  632. ClassDeclaration: checkClass,
  633. ClassExpression: checkClass,
  634. SpreadElement: checkSpreadOperator,
  635. SpreadProperty: checkSpreadOperator,
  636. ExperimentalSpreadProperty: checkSpreadOperator
  637. };
  638. }
  639. };