]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-with | |
8f9d1d4d DC |
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 | ||
eb39fafa DC |
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 | ||
8f9d1d4d DC |
20 | ::: incorrect |
21 | ||
eb39fafa DC |
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 | ||
8f9d1d4d DC |
30 | ::: |
31 | ||
eb39fafa DC |
32 | Examples of **correct** code for this rule: |
33 | ||
8f9d1d4d DC |
34 | ::: correct |
35 | ||
eb39fafa DC |
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 | ||
8f9d1d4d DC |
43 | ::: |
44 | ||
eb39fafa DC |
45 | ## When Not To Use It |
46 | ||
47 | If you intentionally use `with` statements then you can disable this rule. |