Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-extra-parens.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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("../util/ast-utils.js");
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "disallow unnecessary parentheses",
  15. category: "Possible Errors",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-extra-parens"
  18. },
  19. fixable: "code",
  20. schema: {
  21. anyOf: [
  22. {
  23. type: "array",
  24. items: [
  25. {
  26. enum: ["functions"]
  27. }
  28. ],
  29. minItems: 0,
  30. maxItems: 1
  31. },
  32. {
  33. type: "array",
  34. items: [
  35. {
  36. enum: ["all"]
  37. },
  38. {
  39. type: "object",
  40. properties: {
  41. conditionalAssign: { type: "boolean" },
  42. nestedBinaryExpressions: { type: "boolean" },
  43. returnAssign: { type: "boolean" },
  44. ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
  45. enforceForArrowConditionals: { type: "boolean" }
  46. },
  47. additionalProperties: false
  48. }
  49. ],
  50. minItems: 0,
  51. maxItems: 2
  52. }
  53. ]
  54. },
  55. messages: {
  56. unexpected: "Unnecessary parentheses around expression."
  57. }
  58. },
  59. create(context) {
  60. const sourceCode = context.getSourceCode();
  61. const tokensToIgnore = new WeakSet();
  62. const isParenthesised = astUtils.isParenthesised.bind(astUtils, sourceCode);
  63. const precedence = astUtils.getPrecedence;
  64. const ALL_NODES = context.options[0] !== "functions";
  65. const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false;
  66. const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false;
  67. const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false;
  68. const IGNORE_JSX = ALL_NODES && context.options[1] && context.options[1].ignoreJSX;
  69. const IGNORE_ARROW_CONDITIONALS = ALL_NODES && context.options[1] &&
  70. context.options[1].enforceForArrowConditionals === false;
  71. const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
  72. const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
  73. /**
  74. * Determines if this rule should be enforced for a node given the current configuration.
  75. * @param {ASTNode} node - The node to be checked.
  76. * @returns {boolean} True if the rule should be enforced for this node.
  77. * @private
  78. */
  79. function ruleApplies(node) {
  80. if (node.type === "JSXElement" || node.type === "JSXFragment") {
  81. const isSingleLine = node.loc.start.line === node.loc.end.line;
  82. switch (IGNORE_JSX) {
  83. // Exclude this JSX element from linting
  84. case "all":
  85. return false;
  86. // Exclude this JSX element if it is multi-line element
  87. case "multi-line":
  88. return isSingleLine;
  89. // Exclude this JSX element if it is single-line element
  90. case "single-line":
  91. return !isSingleLine;
  92. // Nothing special to be done for JSX elements
  93. case "none":
  94. break;
  95. // no default
  96. }
  97. }
  98. return ALL_NODES || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
  99. }
  100. /**
  101. * Determines if a node is surrounded by parentheses twice.
  102. * @param {ASTNode} node - The node to be checked.
  103. * @returns {boolean} True if the node is doubly parenthesised.
  104. * @private
  105. */
  106. function isParenthesisedTwice(node) {
  107. const previousToken = sourceCode.getTokenBefore(node, 1),
  108. nextToken = sourceCode.getTokenAfter(node, 1);
  109. return isParenthesised(node) && previousToken && nextToken &&
  110. astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
  111. astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
  112. }
  113. /**
  114. * Determines if a node is surrounded by (potentially) invalid parentheses.
  115. * @param {ASTNode} node - The node to be checked.
  116. * @returns {boolean} True if the node is incorrectly parenthesised.
  117. * @private
  118. */
  119. function hasExcessParens(node) {
  120. return ruleApplies(node) && isParenthesised(node);
  121. }
  122. /**
  123. * Determines if a node that is expected to be parenthesised is surrounded by
  124. * (potentially) invalid extra parentheses.
  125. * @param {ASTNode} node - The node to be checked.
  126. * @returns {boolean} True if the node is has an unexpected extra pair of parentheses.
  127. * @private
  128. */
  129. function hasDoubleExcessParens(node) {
  130. return ruleApplies(node) && isParenthesisedTwice(node);
  131. }
  132. /**
  133. * Determines if a node test expression is allowed to have a parenthesised assignment
  134. * @param {ASTNode} node - The node to be checked.
  135. * @returns {boolean} True if the assignment can be parenthesised.
  136. * @private
  137. */
  138. function isCondAssignException(node) {
  139. return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression";
  140. }
  141. /**
  142. * Determines if a node is in a return statement
  143. * @param {ASTNode} node - The node to be checked.
  144. * @returns {boolean} True if the node is in a return statement.
  145. * @private
  146. */
  147. function isInReturnStatement(node) {
  148. for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
  149. if (
  150. currentNode.type === "ReturnStatement" ||
  151. (currentNode.type === "ArrowFunctionExpression" && currentNode.body.type !== "BlockStatement")
  152. ) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. /**
  159. * Determines if a constructor function is newed-up with parens
  160. * @param {ASTNode} newExpression - The NewExpression node to be checked.
  161. * @returns {boolean} True if the constructor is called with parens.
  162. * @private
  163. */
  164. function isNewExpressionWithParens(newExpression) {
  165. const lastToken = sourceCode.getLastToken(newExpression);
  166. const penultimateToken = sourceCode.getTokenBefore(lastToken);
  167. return newExpression.arguments.length > 0 || astUtils.isOpeningParenToken(penultimateToken) && astUtils.isClosingParenToken(lastToken);
  168. }
  169. /**
  170. * Determines if a node is or contains an assignment expression
  171. * @param {ASTNode} node - The node to be checked.
  172. * @returns {boolean} True if the node is or contains an assignment expression.
  173. * @private
  174. */
  175. function containsAssignment(node) {
  176. if (node.type === "AssignmentExpression") {
  177. return true;
  178. }
  179. if (node.type === "ConditionalExpression" &&
  180. (node.consequent.type === "AssignmentExpression" || node.alternate.type === "AssignmentExpression")) {
  181. return true;
  182. }
  183. if ((node.left && node.left.type === "AssignmentExpression") ||
  184. (node.right && node.right.type === "AssignmentExpression")) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. /**
  190. * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment
  191. * @param {ASTNode} node - The node to be checked.
  192. * @returns {boolean} True if the assignment can be parenthesised.
  193. * @private
  194. */
  195. function isReturnAssignException(node) {
  196. if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) {
  197. return false;
  198. }
  199. if (node.type === "ReturnStatement") {
  200. return node.argument && containsAssignment(node.argument);
  201. }
  202. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  203. return containsAssignment(node.body);
  204. }
  205. return containsAssignment(node);
  206. }
  207. /**
  208. * Determines if a node following a [no LineTerminator here] restriction is
  209. * surrounded by (potentially) invalid extra parentheses.
  210. * @param {Token} token - The token preceding the [no LineTerminator here] restriction.
  211. * @param {ASTNode} node - The node to be checked.
  212. * @returns {boolean} True if the node is incorrectly parenthesised.
  213. * @private
  214. */
  215. function hasExcessParensNoLineTerminator(token, node) {
  216. if (token.loc.end.line === node.loc.start.line) {
  217. return hasExcessParens(node);
  218. }
  219. return hasDoubleExcessParens(node);
  220. }
  221. /**
  222. * Determines whether a node should be preceded by an additional space when removing parens
  223. * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
  224. * @returns {boolean} `true` if a space should be inserted before the node
  225. * @private
  226. */
  227. function requiresLeadingSpace(node) {
  228. const leftParenToken = sourceCode.getTokenBefore(node);
  229. const tokenBeforeLeftParen = sourceCode.getTokenBefore(node, 1);
  230. const firstToken = sourceCode.getFirstToken(node);
  231. return tokenBeforeLeftParen &&
  232. tokenBeforeLeftParen.range[1] === leftParenToken.range[0] &&
  233. leftParenToken.range[1] === firstToken.range[0] &&
  234. !astUtils.canTokensBeAdjacent(tokenBeforeLeftParen, firstToken);
  235. }
  236. /**
  237. * Determines whether a node should be followed by an additional space when removing parens
  238. * @param {ASTNode} node node to evaluate; must be surrounded by parentheses
  239. * @returns {boolean} `true` if a space should be inserted after the node
  240. * @private
  241. */
  242. function requiresTrailingSpace(node) {
  243. const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 });
  244. const rightParenToken = nextTwoTokens[0];
  245. const tokenAfterRightParen = nextTwoTokens[1];
  246. const tokenBeforeRightParen = sourceCode.getLastToken(node);
  247. return rightParenToken && tokenAfterRightParen &&
  248. !sourceCode.isSpaceBetweenTokens(rightParenToken, tokenAfterRightParen) &&
  249. !astUtils.canTokensBeAdjacent(tokenBeforeRightParen, tokenAfterRightParen);
  250. }
  251. /**
  252. * Determines if a given expression node is an IIFE
  253. * @param {ASTNode} node The node to check
  254. * @returns {boolean} `true` if the given node is an IIFE
  255. */
  256. function isIIFE(node) {
  257. return node.type === "CallExpression" && node.callee.type === "FunctionExpression";
  258. }
  259. /**
  260. * Report the node
  261. * @param {ASTNode} node node to evaluate
  262. * @returns {void}
  263. * @private
  264. */
  265. function report(node) {
  266. const leftParenToken = sourceCode.getTokenBefore(node);
  267. const rightParenToken = sourceCode.getTokenAfter(node);
  268. if (!isParenthesisedTwice(node)) {
  269. if (tokensToIgnore.has(sourceCode.getFirstToken(node))) {
  270. return;
  271. }
  272. if (isIIFE(node) && !isParenthesised(node.callee)) {
  273. return;
  274. }
  275. }
  276. context.report({
  277. node,
  278. loc: leftParenToken.loc.start,
  279. messageId: "unexpected",
  280. fix(fixer) {
  281. const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]);
  282. return fixer.replaceTextRange([
  283. leftParenToken.range[0],
  284. rightParenToken.range[1]
  285. ], (requiresLeadingSpace(node) ? " " : "") + parenthesizedSource + (requiresTrailingSpace(node) ? " " : ""));
  286. }
  287. });
  288. }
  289. /**
  290. * Evaluate Unary update
  291. * @param {ASTNode} node node to evaluate
  292. * @returns {void}
  293. * @private
  294. */
  295. function checkUnaryUpdate(node) {
  296. if (node.type === "UnaryExpression" && node.argument.type === "BinaryExpression" && node.argument.operator === "**") {
  297. return;
  298. }
  299. if (hasExcessParens(node.argument) && precedence(node.argument) >= precedence(node)) {
  300. report(node.argument);
  301. }
  302. }
  303. /**
  304. * Check if a member expression contains a call expression
  305. * @param {ASTNode} node MemberExpression node to evaluate
  306. * @returns {boolean} true if found, false if not
  307. */
  308. function doesMemberExpressionContainCallExpression(node) {
  309. let currentNode = node.object;
  310. let currentNodeType = node.object.type;
  311. while (currentNodeType === "MemberExpression") {
  312. currentNode = currentNode.object;
  313. currentNodeType = currentNode.type;
  314. }
  315. return currentNodeType === "CallExpression";
  316. }
  317. /**
  318. * Evaluate a new call
  319. * @param {ASTNode} node node to evaluate
  320. * @returns {void}
  321. * @private
  322. */
  323. function checkCallNew(node) {
  324. const callee = node.callee;
  325. if (hasExcessParens(callee) && precedence(callee) >= precedence(node)) {
  326. const hasNewParensException = callee.type === "NewExpression" && !isNewExpressionWithParens(callee);
  327. if (
  328. hasDoubleExcessParens(callee) ||
  329. !isIIFE(node) && !hasNewParensException && !(
  330. /*
  331. * Allow extra parens around a new expression if
  332. * there are intervening parentheses.
  333. */
  334. (callee.type === "MemberExpression" && 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. * If `let` is followed by a `[` token, it's a property access on the `let` value: `for ((let[foo]) of bar);`
  500. * Removing it will cause the property access to be parsed as a destructuring declaration of `foo` instead.
  501. */
  502. astUtils.isOpeningBracketToken(
  503. sourceCode.getTokenAfter(firstLeftToken, astUtils.isNotClosingParenToken)
  504. ))
  505. )
  506. ) {
  507. tokensToIgnore.add(firstLeftToken);
  508. }
  509. }
  510. if (!(node.type === "ForOfStatement" && node.right.type === "SequenceExpression") && hasExcessParens(node.right)) {
  511. report(node.right);
  512. }
  513. if (hasExcessParens(node.left)) {
  514. report(node.left);
  515. }
  516. },
  517. ForStatement(node) {
  518. if (node.init && hasExcessParens(node.init)) {
  519. report(node.init);
  520. }
  521. if (node.test && hasExcessParens(node.test) && !isCondAssignException(node)) {
  522. report(node.test);
  523. }
  524. if (node.update && hasExcessParens(node.update)) {
  525. report(node.update);
  526. }
  527. },
  528. IfStatement(node) {
  529. if (hasDoubleExcessParens(node.test) && !isCondAssignException(node)) {
  530. report(node.test);
  531. }
  532. },
  533. LogicalExpression: checkBinaryLogical,
  534. MemberExpression(node) {
  535. const nodeObjHasExcessParens = hasExcessParens(node.object);
  536. if (
  537. nodeObjHasExcessParens &&
  538. precedence(node.object) >= precedence(node) &&
  539. (
  540. node.computed ||
  541. !(
  542. astUtils.isDecimalInteger(node.object) ||
  543. // RegExp literal is allowed to have parens (#1589)
  544. (node.object.type === "Literal" && node.object.regex)
  545. )
  546. )
  547. ) {
  548. report(node.object);
  549. }
  550. if (nodeObjHasExcessParens &&
  551. node.object.type === "CallExpression" &&
  552. node.parent.type !== "NewExpression") {
  553. report(node.object);
  554. }
  555. if (node.computed && hasExcessParens(node.property)) {
  556. report(node.property);
  557. }
  558. },
  559. NewExpression: checkCallNew,
  560. ObjectExpression(node) {
  561. node.properties
  562. .filter(property => {
  563. const value = property.value;
  564. return value && hasExcessParens(value) && precedence(value) >= PRECEDENCE_OF_ASSIGNMENT_EXPR;
  565. }).forEach(property => report(property.value));
  566. },
  567. ReturnStatement(node) {
  568. const returnToken = sourceCode.getFirstToken(node);
  569. if (isReturnAssignException(node)) {
  570. return;
  571. }
  572. if (node.argument &&
  573. hasExcessParensNoLineTerminator(returnToken, node.argument) &&
  574. // RegExp literal is allowed to have parens (#1589)
  575. !(node.argument.type === "Literal" && node.argument.regex)) {
  576. report(node.argument);
  577. }
  578. },
  579. SequenceExpression(node) {
  580. node.expressions
  581. .filter(e => hasExcessParens(e) && precedence(e) >= precedence(node))
  582. .forEach(report);
  583. },
  584. SwitchCase(node) {
  585. if (node.test && hasExcessParens(node.test)) {
  586. report(node.test);
  587. }
  588. },
  589. SwitchStatement(node) {
  590. if (hasDoubleExcessParens(node.discriminant)) {
  591. report(node.discriminant);
  592. }
  593. },
  594. ThrowStatement(node) {
  595. const throwToken = sourceCode.getFirstToken(node);
  596. if (hasExcessParensNoLineTerminator(throwToken, node.argument)) {
  597. report(node.argument);
  598. }
  599. },
  600. UnaryExpression: checkUnaryUpdate,
  601. UpdateExpression: checkUnaryUpdate,
  602. AwaitExpression: checkUnaryUpdate,
  603. VariableDeclarator(node) {
  604. if (node.init && hasExcessParens(node.init) &&
  605. precedence(node.init) >= PRECEDENCE_OF_ASSIGNMENT_EXPR &&
  606. // RegExp literal is allowed to have parens (#1589)
  607. !(node.init.type === "Literal" && node.init.regex)) {
  608. report(node.init);
  609. }
  610. },
  611. WhileStatement(node) {
  612. if (hasDoubleExcessParens(node.test) && !isCondAssignException(node)) {
  613. report(node.test);
  614. }
  615. },
  616. WithStatement(node) {
  617. if (hasDoubleExcessParens(node.object)) {
  618. report(node.object);
  619. }
  620. },
  621. YieldExpression(node) {
  622. if (node.argument) {
  623. const yieldToken = sourceCode.getFirstToken(node);
  624. if ((precedence(node.argument) >= precedence(node) &&
  625. hasExcessParensNoLineTerminator(yieldToken, node.argument)) ||
  626. hasDoubleExcessParens(node.argument)) {
  627. report(node.argument);
  628. }
  629. }
  630. },
  631. ClassDeclaration: checkClass,
  632. ClassExpression: checkClass,
  633. SpreadElement: checkSpreadOperator,
  634. SpreadProperty: checkSpreadOperator,
  635. ExperimentalSpreadProperty: checkSpreadOperator
  636. };
  637. }
  638. };