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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }
  24. case 'NewExpression': {
  25. return !isNewExpressionWithParentheses(node, sourceCode);
  26. }
  27. case 'Literal': {
  28. /* c8 ignore next */
  29. if (isDecimalIntegerNode(node)) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. default: {
  35. return true;
  36. }
  37. }
  38. }
  39. module.exports = shouldAddParenthesesToMemberExpressionObject;