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.

new-parens.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @fileoverview Rule to flag when using constructor without parentheses
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. //------------------------------------------------------------------------------
  14. // Rule Definition
  15. //------------------------------------------------------------------------------
  16. module.exports = {
  17. meta: {
  18. type: "layout",
  19. docs: {
  20. description: "require parentheses when invoking a constructor with no arguments",
  21. category: "Stylistic Issues",
  22. recommended: false,
  23. url: "https://eslint.org/docs/rules/new-parens"
  24. },
  25. schema: [],
  26. fixable: "code"
  27. },
  28. create(context) {
  29. const sourceCode = context.getSourceCode();
  30. return {
  31. NewExpression(node) {
  32. if (node.arguments.length !== 0) {
  33. return; // shortcut: if there are arguments, there have to be parens
  34. }
  35. const lastToken = sourceCode.getLastToken(node);
  36. const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
  37. const hasParens = hasLastParen && astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken));
  38. if (!hasParens) {
  39. context.report({
  40. node,
  41. message: "Missing '()' invoking a constructor.",
  42. fix: fixer => fixer.insertTextAfter(node, "()")
  43. });
  44. }
  45. }
  46. };
  47. }
  48. };