Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: avoid useless catch clauses that just rethrow errors (#10010)
- Loading branch information
1 parent
a1c3759
commit 18e15d9
Showing
4 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |