Skip to content

Commit

Permalink
Chore: avoid useless catch clauses that just rethrow errors (#10010)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 24, 2018
1 parent a1c3759 commit 18e15d9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -25,6 +25,7 @@ module.exports = {
"eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"],
"eslint-plugin/test-case-property-ordering": "error",
"eslint-plugin/test-case-shorthand-strings": "error",
"rulesdir/multiline-comment-style": "error"
"rulesdir/multiline-comment-style": "error",
"rulesdir/no-useless-catch": "error"
}
};
2 changes: 0 additions & 2 deletions tests/lib/testers/no-test-runners.js
Expand Up @@ -27,8 +27,6 @@ try {
]
});
}, /Output is incorrect\. \(' foo = bar;' == 'invalid output'\)$/);
} catch (e) {
throw e;
} finally {
it = tmpIt;
describe = tmpDescribe;
Expand Down
62 changes: 62 additions & 0 deletions tests/tools/internal-rules/no-useless-catch.js
@@ -0,0 +1,62 @@
/**
* @fileoverview Tests for no-useless-throw rule
* @author Teddy Katz
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../tools/internal-rules/no-useless-catch");
const RuleTester = require("../../../lib/testers/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

new RuleTester({ parserOptions: { ecmaVersion: 6 } }).run("rulesdir/no-useless-catch", rule, {
valid: [
`
try {
foo();
} catch (err) {
console.error(err);
} finally {
foo;
}
`,
`
try {
foo();
} catch ({ err }) {
throw err;
}
`
],
invalid: [
{
code: `
try {
foo();
} catch (e) {
throw e;
}
`,
errors: [{ message: "Unnecessary try/catch wrapper." }]
},
{
code: `
try {
foo();
} catch (e) {
throw e;
} finally {
foo();
}
`,
errors: [{ message: "Unnecessary catch clause." }]
}
]
});
44 changes: 44 additions & 0 deletions tools/internal-rules/no-useless-catch.js
@@ -0,0 +1,44 @@
/**
* @fileoverview Reports useless `catch` clauses that just rethrow their error.
* @author Teddy Katz
*/

"use strict";

module.exports = {
meta: {
docs: {
description: "disallow unnecessary `catch` clauses",
category: "Internal",
recommended: false
},

schema: []
},

create(context) {
return {
CatchClause(node) {
if (
node.param.type === "Identifier" &&
node.body.body.length &&
node.body.body[0].type === "ThrowStatement" &&
node.body.body[0].argument.type === "Identifier" &&
node.body.body[0].argument.name === node.param.name
) {
if (node.parent.finalizer) {
context.report({
node,
message: "Unnecessary catch clause."
});
} else {
context.report({
node,
message: "Unnecessary try/catch wrapper."
});
}
}
}
};
}
};

0 comments on commit 18e15d9

Please sign in to comment.