]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow the type conversion with shorter notations. (no-implicit-coercion) |
2 | ||
3 | In JavaScript, there are a lot of different ways to convert value types. | |
4 | Some of them might be hard to read and understand. | |
5 | ||
6 | Such as: | |
7 | ||
8 | ```js | |
9 | var b = !!foo; | |
10 | var b = ~foo.indexOf("."); | |
11 | var n = +foo; | |
12 | var n = 1 * foo; | |
13 | var s = "" + foo; | |
14 | foo += ``; | |
15 | ``` | |
16 | ||
17 | Those can be replaced with the following code: | |
18 | ||
19 | ```js | |
20 | var b = Boolean(foo); | |
21 | var b = foo.indexOf(".") !== -1; | |
22 | var n = Number(foo); | |
23 | var n = Number(foo); | |
24 | var s = String(foo); | |
25 | foo = String(foo); | |
26 | ``` | |
27 | ||
28 | ## Rule Details | |
29 | ||
30 | This rule is aimed to flag shorter notations for the type conversion, then suggest a more self-explanatory notation. | |
31 | ||
32 | ## Options | |
33 | ||
34 | This rule has three main options and one override option to allow some coercions as required. | |
35 | ||
5422a9cc TL |
36 | - `"boolean"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `boolean` type. |
37 | - `"number"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `number` type. | |
38 | - `"string"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `string` type. | |
39 | - `"disallowTemplateShorthand"` (`false` by default) - When this is `true`, this rule warns `string` type conversions using `${expression}` form. | |
40 | - `"allow"` (`empty` by default) - Each entry in this array can be one of `~`, `!!`, `+` or `*` that are to be allowed. | |
eb39fafa DC |
41 | |
42 | Note that operator `+` in `allow` list would allow `+foo` (number coercion) as well as `"" + foo` (string coercion). | |
43 | ||
44 | ### boolean | |
45 | ||
46 | Examples of **incorrect** code for the default `{ "boolean": true }` option: | |
47 | ||
48 | ```js | |
49 | /*eslint no-implicit-coercion: "error"*/ | |
50 | ||
51 | var b = !!foo; | |
52 | var b = ~foo.indexOf("."); | |
53 | // bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling. | |
54 | ``` | |
55 | ||
56 | Examples of **correct** code for the default `{ "boolean": true }` option: | |
57 | ||
58 | ```js | |
59 | /*eslint no-implicit-coercion: "error"*/ | |
60 | ||
61 | var b = Boolean(foo); | |
62 | var b = foo.indexOf(".") !== -1; | |
63 | ||
64 | var n = ~foo; // This is a just bitwise not. | |
65 | ``` | |
66 | ||
67 | ### number | |
68 | ||
69 | Examples of **incorrect** code for the default `{ "number": true }` option: | |
70 | ||
71 | ```js | |
72 | /*eslint no-implicit-coercion: "error"*/ | |
73 | ||
74 | var n = +foo; | |
75 | var n = 1 * foo; | |
76 | ``` | |
77 | ||
78 | Examples of **correct** code for the default `{ "number": true }` option: | |
79 | ||
80 | ```js | |
81 | /*eslint no-implicit-coercion: "error"*/ | |
82 | ||
83 | var n = Number(foo); | |
84 | var n = parseFloat(foo); | |
85 | var n = parseInt(foo, 10); | |
86 | ``` | |
87 | ||
88 | ### string | |
89 | ||
90 | Examples of **incorrect** code for the default `{ "string": true }` option: | |
91 | ||
92 | ```js | |
93 | /*eslint no-implicit-coercion: "error"*/ | |
94 | ||
95 | var s = "" + foo; | |
96 | var s = `` + foo; | |
97 | foo += ""; | |
98 | foo += ``; | |
99 | ``` | |
100 | ||
101 | Examples of **correct** code for the default `{ "string": true }` option: | |
102 | ||
103 | ```js | |
104 | /*eslint no-implicit-coercion: "error"*/ | |
105 | ||
106 | var s = String(foo); | |
107 | foo = String(foo); | |
108 | ``` | |
109 | ||
5422a9cc TL |
110 | ### disallowTemplateShorthand |
111 | ||
112 | This option is **not** affected by the `string` option. | |
113 | ||
114 | Examples of **incorrect** code for the `{ "disallowTemplateShorthand": true }` option: | |
115 | ||
116 | ```js | |
117 | /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/ | |
118 | ||
119 | var s = `${foo}`; | |
120 | ``` | |
121 | ||
122 | Examples of **correct** code for the `{ "disallowTemplateShorthand": true }` option: | |
123 | ||
124 | ```js | |
125 | /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/ | |
126 | ||
127 | var s = String(foo); | |
128 | ||
129 | var s = `a${foo}`; | |
130 | ||
131 | var s = `${foo}b`; | |
132 | ||
133 | var s = `${foo}${bar}`; | |
134 | ||
135 | var s = tag`${foo}`; | |
136 | ``` | |
137 | ||
138 | Examples of **correct** code for the default `{ "disallowTemplateShorthand": false }` option: | |
139 | ||
140 | ```js | |
141 | /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/ | |
142 | ||
143 | var s = `${foo}`; | |
144 | ``` | |
145 | ||
eb39fafa DC |
146 | ### allow |
147 | ||
148 | Using `allow` list, we can override and allow specific operators. | |
149 | ||
150 | Examples of **correct** code for the sample `{ "allow": ["!!", "~"] }` option: | |
151 | ||
152 | ```js | |
153 | /*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/ | |
154 | ||
155 | var b = !!foo; | |
156 | var b = ~foo.indexOf("."); | |
157 | ``` | |
158 | ||
159 | ## When Not To Use It | |
160 | ||
161 | If you don't want to be notified about shorter notations for the type conversion, you can safely disable this rule. |