File tree 4 files changed +108
-3
lines changed
4 files changed +108
-3
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ module.exports = {
25
25
"eslint-plugin/report-message-format" : [ "error" , "[^a-z].*\\.$" ] ,
26
26
"eslint-plugin/test-case-property-ordering" : "error" ,
27
27
"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"
29
30
}
30
31
} ;
Original file line number Diff line number Diff line change 27
27
]
28
28
} ) ;
29
29
} , / O u t p u t i s i n c o r r e c t \. \( ' f o o = b a r ; ' = = ' i n v a l i d o u t p u t ' \) $ / ) ;
30
- } catch ( e ) {
31
- throw e ;
32
30
} finally {
33
31
it = tmpIt ;
34
32
describe = tmpDescribe ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments