]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-catch-shadow.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-catch-shadow.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-catch-shadow
8f9d1d4d
DC
3rule_type: suggestion
4---
5
eb39fafa
DC
6
7This rule was **deprecated** in ESLint v5.1.0.
8
9In 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
12var err = "x";
13
14try {
15 throw "problem";
16} catch (err) {
17
18}
19
20console.log(err) // err is 'problem', not 'x'
21```
22
23## Rule Details
24
25This 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
27Examples of **incorrect** code for this rule:
28
8f9d1d4d
DC
29::: incorrect
30
eb39fafa
DC
31```js
32/*eslint no-catch-shadow: "error"*/
33
34var err = "x";
35
36try {
37 throw "problem";
38} catch (err) {
39
40}
41
42function err() {
43 // ...
44};
45
46try {
47 throw "problem";
48} catch (err) {
49
50}
51```
52
8f9d1d4d
DC
53:::
54
eb39fafa
DC
55Examples of **correct** code for this rule:
56
8f9d1d4d
DC
57::: correct
58
eb39fafa
DC
59```js
60/*eslint no-catch-shadow: "error"*/
61
62var err = "x";
63
64try {
65 throw "problem";
66} catch (e) {
67
68}
69
70function err() {
71 // ...
72};
73
74try {
75 throw "problem";
76} catch (e) {
77
78}
79```
80
8f9d1d4d
DC
81:::
82
eb39fafa
DC
83## When Not To Use It
84
85If you do not need to support IE 8 and earlier, you should turn this rule off.