should-add-parentheses-to-member-expression-object.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const isNewExpressionWithParentheses = require('./is-new-expression-with-parentheses.js');
  3. const {isDecimalIntegerNode} = require('./numeric.js');
  4. /**
  5. Check if parentheses should to be added to a `node` when it's used as an `object` of `MemberExpression`.
  6. @param {Node} node - The AST node to check.
  7. @param {SourceCode} sourceCode - The source code object.
  8. @returns {boolean}
  9. */
  10. function shouldAddParenthesesToMemberExpressionObject(node, sourceCode) {
  11. switch (node.type) {
  12. // This is not a full list. Some other nodes like `FunctionDeclaration` don't need parentheses,
  13. // but it's not possible to be in the place we are checking at this point.
  14. case 'Identifier':
  15. case 'MemberExpression':
  16. case 'CallExpression':
  17. case 'ChainExpression':
  18. case 'TemplateLiteral':
  19. case 'ThisExpression':
  20. case 'ArrayExpression':
  21. case 'FunctionExpression':
  22. return false;
  23. case 'NewExpression':
  24. return !isNewExpressionWithParentheses(node, sourceCode);
  25. case 'Literal': {
  26. /* istanbul ignore next */
  27. if (isDecimalIntegerNode(node)) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. default:
  33. return true;
  34. }
  35. }
  36. module.exports = shouldAddParenthesesToMemberExpressionObject;