UNPKG

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