Skip to content

Commit 18e15d9

Browse files
authoredFeb 24, 2018
Chore: avoid useless catch clauses that just rethrow errors (#10010)
1 parent a1c3759 commit 18e15d9

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed
 

‎.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
"eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"],
2626
"eslint-plugin/test-case-property-ordering": "error",
2727
"eslint-plugin/test-case-shorthand-strings": "error",
28-
"rulesdir/multiline-comment-style": "error"
28+
"rulesdir/multiline-comment-style": "error",
29+
"rulesdir/no-useless-catch": "error"
2930
}
3031
};

‎tests/lib/testers/no-test-runners.js

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ try {
2727
]
2828
});
2929
}, /Output is incorrect\. \(' foo = bar;' == 'invalid output'\)$/);
30-
} catch (e) {
31-
throw e;
3230
} finally {
3331
it = tmpIt;
3432
describe = tmpDescribe;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @fileoverview Tests for no-useless-throw rule
3+
* @author Teddy Katz
4+
*/
5+
6+
"use strict";
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const rule = require("../../../tools/internal-rules/no-useless-catch");
13+
const RuleTester = require("../../../lib/testers/rule-tester");
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
new RuleTester({ parserOptions: { ecmaVersion: 6 } }).run("rulesdir/no-useless-catch", rule, {
20+
valid: [
21+
`
22+
try {
23+
foo();
24+
} catch (err) {
25+
console.error(err);
26+
} finally {
27+
foo;
28+
}
29+
`,
30+
`
31+
try {
32+
foo();
33+
} catch ({ err }) {
34+
throw err;
35+
}
36+
`
37+
],
38+
invalid: [
39+
{
40+
code: `
41+
try {
42+
foo();
43+
} catch (e) {
44+
throw e;
45+
}
46+
`,
47+
errors: [{ message: "Unnecessary try/catch wrapper." }]
48+
},
49+
{
50+
code: `
51+
try {
52+
foo();
53+
} catch (e) {
54+
throw e;
55+
} finally {
56+
foo();
57+
}
58+
`,
59+
errors: [{ message: "Unnecessary catch clause." }]
60+
}
61+
]
62+
});
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @fileoverview Reports useless `catch` clauses that just rethrow their error.
3+
* @author Teddy Katz
4+
*/
5+
6+
"use strict";
7+
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
description: "disallow unnecessary `catch` clauses",
12+
category: "Internal",
13+
recommended: false
14+
},
15+
16+
schema: []
17+
},
18+
19+
create(context) {
20+
return {
21+
CatchClause(node) {
22+
if (
23+
node.param.type === "Identifier" &&
24+
node.body.body.length &&
25+
node.body.body[0].type === "ThrowStatement" &&
26+
node.body.body[0].argument.type === "Identifier" &&
27+
node.body.body[0].argument.name === node.param.name
28+
) {
29+
if (node.parent.finalizer) {
30+
context.report({
31+
node,
32+
message: "Unnecessary catch clause."
33+
});
34+
} else {
35+
context.report({
36+
node,
37+
message: "Unnecessary try/catch wrapper."
38+
});
39+
}
40+
}
41+
}
42+
};
43+
}
44+
};

0 commit comments

Comments
 (0)
Please sign in to comment.