should-add-parentheses-to-new-expression-callee.js 1013 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. // Copied from https://github.com/eslint/eslint/blob/aa87329d919f569404ca573b439934552006572f/lib/rules/no-extra-parens.js#L448
  3. /**
  4. Check if a member expression contains a call expression.
  5. @param {ASTNode} node - The `MemberExpression` node to evaluate.
  6. @returns {boolean} true if found, false if not.
  7. */
  8. function doesMemberExpressionContainCallExpression(node) {
  9. let currentNode = node.object;
  10. let currentNodeType = node.object.type;
  11. while (currentNodeType === 'MemberExpression') {
  12. currentNode = currentNode.object;
  13. currentNodeType = currentNode.type;
  14. }
  15. return currentNodeType === 'CallExpression';
  16. }
  17. /**
  18. Check if parentheses should be added to a `node` when it's used as `callee` of `NewExpression`.
  19. @param {Node} node - The AST node to check.
  20. @returns {boolean}
  21. */
  22. function shouldAddParenthesesToNewExpressionCallee(node) {
  23. return node.type === 'MemberExpression' && doesMemberExpressionContainCallExpression(node);
  24. }
  25. module.exports = shouldAddParenthesesToNewExpressionCallee;