is-new-expression-with-parentheses.js 970 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. const {isOpeningParenToken, isClosingParenToken} = require('@eslint-community/eslint-utils');
  3. /**
  4. Determine if a constructor function is newed-up with parens.
  5. @param {Node} node - The `NewExpression` node to be checked.
  6. @param {SourceCode} sourceCode - The source code object.
  7. @returns {boolean} True if the constructor is called with parens.
  8. Copied from https://github.com/eslint/eslint/blob/cc4871369645c3409dc56ded7a555af8a9f63d51/lib/rules/no-extra-parens.js#L252
  9. */
  10. function isNewExpressionWithParentheses(node, sourceCode) {
  11. if (node.arguments.length > 0) {
  12. return true;
  13. }
  14. const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2);
  15. // The expression should end with its own parens, for example, `new new Foo()` is not a new expression with parens.
  16. return isOpeningParenToken(penultimateToken)
  17. && isClosingParenToken(lastToken)
  18. && node.callee.range[1] < node.range[1];
  19. }
  20. module.exports = isNewExpressionWithParentheses;