]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-catch-shadow | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
eb39fafa DC |
6 | |
7 | This rule was **deprecated** in ESLint v5.1.0. | |
8 | ||
9 | In IE 8 and earlier, the catch clause parameter can overwrite the value of a variable in the outer scope, if that variable has the same name as the catch clause parameter. | |
10 | ||
11 | ```js | |
12 | var err = "x"; | |
13 | ||
14 | try { | |
15 | throw "problem"; | |
16 | } catch (err) { | |
17 | ||
18 | } | |
19 | ||
20 | console.log(err) // err is 'problem', not 'x' | |
21 | ``` | |
22 | ||
23 | ## Rule Details | |
24 | ||
25 | This rule is aimed at preventing unexpected behavior in your program that may arise from a bug in IE 8 and earlier, in which the catch clause parameter can leak into outer scopes. This rule will warn whenever it encounters a catch clause parameter that has the same name as a variable in an outer scope. | |
26 | ||
27 | Examples of **incorrect** code for this rule: | |
28 | ||
8f9d1d4d DC |
29 | ::: incorrect |
30 | ||
eb39fafa DC |
31 | ```js |
32 | /*eslint no-catch-shadow: "error"*/ | |
33 | ||
34 | var err = "x"; | |
35 | ||
36 | try { | |
37 | throw "problem"; | |
38 | } catch (err) { | |
39 | ||
40 | } | |
41 | ||
42 | function err() { | |
43 | // ... | |
44 | }; | |
45 | ||
46 | try { | |
47 | throw "problem"; | |
48 | } catch (err) { | |
49 | ||
50 | } | |
51 | ``` | |
52 | ||
8f9d1d4d DC |
53 | ::: |
54 | ||
eb39fafa DC |
55 | Examples of **correct** code for this rule: |
56 | ||
8f9d1d4d DC |
57 | ::: correct |
58 | ||
eb39fafa DC |
59 | ```js |
60 | /*eslint no-catch-shadow: "error"*/ | |
61 | ||
62 | var err = "x"; | |
63 | ||
64 | try { | |
65 | throw "problem"; | |
66 | } catch (e) { | |
67 | ||
68 | } | |
69 | ||
70 | function err() { | |
71 | // ... | |
72 | }; | |
73 | ||
74 | try { | |
75 | throw "problem"; | |
76 | } catch (e) { | |
77 | ||
78 | } | |
79 | ``` | |
80 | ||
8f9d1d4d DC |
81 | ::: |
82 | ||
eb39fafa DC |
83 | ## When Not To Use It |
84 | ||
85 | If you do not need to support IE 8 and earlier, you should turn this rule off. |