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