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.

no-throw-literal.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview Rule to restrict what can be thrown as an exception.
  3. * @author Dieter Oberkofler
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow throwing literals as exceptions",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-throw-literal"
  18. },
  19. schema: []
  20. },
  21. create(context) {
  22. return {
  23. ThrowStatement(node) {
  24. if (!astUtils.couldBeError(node.argument)) {
  25. context.report({ node, message: "Expected an object to be thrown." });
  26. } else if (node.argument.type === "Identifier") {
  27. if (node.argument.name === "undefined") {
  28. context.report({ node, message: "Do not throw undefined." });
  29. }
  30. }
  31. }
  32. };
  33. }
  34. };