]>
Commit | Line | Data |
---|---|---|
1 | --- | |
2 | title: no-with | |
3 | rule_type: suggestion | |
4 | further_reading: | |
5 | - https://web.archive.org/web/20200717110117/https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ | |
6 | --- | |
7 | ||
8 | ||
9 | ||
10 | The `with` statement is potentially problematic because it adds members of an object to the current scope, making it impossible to tell what a variable inside the block actually refers to. | |
11 | ||
12 | ## Rule Details | |
13 | ||
14 | This rule disallows `with` statements. | |
15 | ||
16 | If ESLint parses code in strict mode, the parser (instead of this rule) reports the error. | |
17 | ||
18 | Examples of **incorrect** code for this rule: | |
19 | ||
20 | ::: incorrect | |
21 | ||
22 | ```js | |
23 | /*eslint no-with: "error"*/ | |
24 | ||
25 | with (point) { | |
26 | r = Math.sqrt(x * x + y * y); // is r a member of point? | |
27 | } | |
28 | ``` | |
29 | ||
30 | ::: | |
31 | ||
32 | Examples of **correct** code for this rule: | |
33 | ||
34 | ::: correct | |
35 | ||
36 | ```js | |
37 | /*eslint no-with: "error"*/ | |
38 | /*eslint-env es6*/ | |
39 | ||
40 | const r = ({x, y}) => Math.sqrt(x * x + y * y); | |
41 | ``` | |
42 | ||
43 | ::: | |
44 | ||
45 | ## When Not To Use It | |
46 | ||
47 | If you intentionally use `with` statements then you can disable this rule. |