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.

57 lines
1.7 KiB

4 years ago
  1. /**
  2. * @fileoverview Reports useless `catch` clauses that just rethrow their error.
  3. * @author Teddy Katz
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow unnecessary `catch` clauses",
  14. category: "Best Practices",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-useless-catch"
  17. },
  18. schema: [],
  19. messages: {
  20. unnecessaryCatchClause: "Unnecessary catch clause.",
  21. unnecessaryCatch: "Unnecessary try/catch wrapper."
  22. }
  23. },
  24. create(context) {
  25. return {
  26. CatchClause(node) {
  27. if (
  28. node.param &&
  29. node.param.type === "Identifier" &&
  30. node.body.body.length &&
  31. node.body.body[0].type === "ThrowStatement" &&
  32. node.body.body[0].argument.type === "Identifier" &&
  33. node.body.body[0].argument.name === node.param.name
  34. ) {
  35. if (node.parent.finalizer) {
  36. context.report({
  37. node,
  38. messageId: "unnecessaryCatchClause"
  39. });
  40. } else {
  41. context.report({
  42. node: node.parent,
  43. messageId: "unnecessaryCatch"
  44. });
  45. }
  46. }
  47. }
  48. };
  49. }
  50. };