forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: no-useless-catch rule (fixes eslint#11174)
Moves no-useless-catch from an internal rule to a core rule. Adds docs and test cases.
- Loading branch information
Alex Grasley
committed
Dec 17, 2018
1 parent
733d936
commit 7433d64
Showing
8 changed files
with
249 additions
and
67 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,50 @@ | ||
# Disallow unnecessary catch clauses (no-useless-catch) | ||
|
||
A `catch` clause that only rethrows the original error is redundant, and has no effect on the runtime behavior of the program. These redundant clauses can be a source of confusion and code bloat, so it's better to disallow these unnecessary `catch` clauses. | ||
|
||
## Rule Details | ||
|
||
This rule reports `catch` clauses that only `throw` the caught error. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-catch: "error"*/ | ||
|
||
try { | ||
doSomethingThatMightThrow(); | ||
} catch (e) { | ||
throw e; | ||
} | ||
|
||
try { | ||
doSomethingThatMightThrow(); | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
cleanUp(); | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-catch: "error"*/ | ||
|
||
try { | ||
doSomethingThatMightThrow(); | ||
} catch (e) { | ||
doSomethingBeforeRethrow(); | ||
throw e; | ||
} | ||
|
||
try { | ||
doSomethingThatMightThrow(); | ||
} catch (e) { | ||
handleError(e); | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't want to be notified about unnecessary catch clauses, you can safely disable this rule. |
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,185 @@ | ||
/** | ||
* @fileoverview Tests for no-useless-throw rule | ||
* @author Teddy Katz | ||
* @author Alex Grasley | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-useless-catch"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run("no-useless-catch", rule, { | ||
valid: [ | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
console.error(err); | ||
} finally { | ||
bar(); | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
doSomethingBeforeRethrow(); | ||
throw err; | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
throw err.msg; | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
throw new Error("whoops!"); | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
throw bar; | ||
} | ||
`, | ||
` | ||
try { | ||
foo(); | ||
} catch (err) { } | ||
`, | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch ({ err }) { | ||
throw err; | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6 } | ||
}, | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch ([ err ]) { | ||
throw err; | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6 } | ||
}, | ||
{ | ||
code: ` | ||
async () => { | ||
try { | ||
await doSomething(); | ||
} catch (e) { | ||
doSomethingAfterCatch(); | ||
throw e; | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 8 } | ||
} | ||
|
||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
throw err; | ||
} | ||
`, | ||
errors: [{ | ||
message: "Unnecessary try/catch wrapper.", | ||
type: "TryStatement" | ||
}] | ||
}, | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
foo(); | ||
} | ||
`, | ||
errors: [{ | ||
message: "Unnecessary catch clause.", | ||
type: "CatchClause" | ||
}] | ||
}, | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
/* some comment */ | ||
throw err; | ||
} | ||
`, | ||
errors: [{ | ||
message: "Unnecessary try/catch wrapper.", | ||
type: "TryStatement" | ||
}] | ||
}, | ||
{ | ||
code: ` | ||
try { | ||
foo(); | ||
} catch (err) { | ||
/* some comment */ | ||
throw err; | ||
} finally { | ||
foo(); | ||
} | ||
`, | ||
errors: [{ | ||
message: "Unnecessary catch clause.", | ||
type: "CatchClause" | ||
}] | ||
}, | ||
{ | ||
code: ` | ||
async () => { | ||
try { | ||
await doSomething(); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 8 }, | ||
errors: [{ | ||
message: "Unnecessary try/catch wrapper.", | ||
type: "TryStatement" | ||
}] | ||
} | ||
] | ||
}); |
This file was deleted.
Oops, something went wrong.
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