项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

no-throw-literal.js 1.1 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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("../ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow throwing literals as exceptions",
  14. category: "Best Practices",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-throw-literal"
  17. },
  18. schema: []
  19. },
  20. create(context) {
  21. return {
  22. ThrowStatement(node) {
  23. if (!astUtils.couldBeError(node.argument)) {
  24. context.report({ node, message: "Expected an object to be thrown." });
  25. } else if (node.argument.type === "Identifier") {
  26. if (node.argument.name === "undefined") {
  27. context.report({ node, message: "Do not throw undefined." });
  28. }
  29. }
  30. }
  31. };
  32. }
  33. };