2 title: no-implicit-coercion
9 In JavaScript, there are a lot of different ways to convert value types.
10 Some of them might be hard to read and understand.
16 var b = ~foo.indexOf(".");
23 Those can be replaced with the following code:
27 var b = foo.indexOf(".") !== -1;
36 This rule is aimed to flag shorter notations for the type conversion, then suggest a more self-explanatory notation.
40 This rule has three main options and one override option to allow some coercions as required.
42 * `"boolean"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `boolean` type.
43 * `"number"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `number` type.
44 * `"string"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `string` type.
45 * `"disallowTemplateShorthand"` (`false` by default) - When this is `true`, this rule warns `string` type conversions using `${expression}` form.
46 * `"allow"` (`empty` by default) - Each entry in this array can be one of `~`, `!!`, `+` or `*` that are to be allowed.
48 Note that operator `+` in `allow` list would allow `+foo` (number coercion) as well as `"" + foo` (string coercion).
52 Examples of **incorrect** code for the default `{ "boolean": true }` option:
57 /*eslint no-implicit-coercion: "error"*/
60 var b = ~foo.indexOf(".");
61 // bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
66 Examples of **correct** code for the default `{ "boolean": true }` option:
71 /*eslint no-implicit-coercion: "error"*/
74 var b = foo.indexOf(".") !== -1;
76 var n = ~foo; // This is a just bitwise not.
83 Examples of **incorrect** code for the default `{ "number": true }` option:
88 /*eslint no-implicit-coercion: "error"*/
96 Examples of **correct** code for the default `{ "number": true }` option:
101 /*eslint no-implicit-coercion: "error"*/
104 var n = parseFloat(foo);
105 var n = parseInt(foo, 10);
112 Examples of **incorrect** code for the default `{ "string": true }` option:
117 /*eslint no-implicit-coercion: "error"*/
127 Examples of **correct** code for the default `{ "string": true }` option:
132 /*eslint no-implicit-coercion: "error"*/
140 ### disallowTemplateShorthand
142 This option is **not** affected by the `string` option.
144 Examples of **incorrect** code for the `{ "disallowTemplateShorthand": true }` option:
149 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
156 Examples of **correct** code for the `{ "disallowTemplateShorthand": true }` option:
161 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
169 var s = `${foo}${bar}`;
176 Examples of **correct** code for the default `{ "disallowTemplateShorthand": false }` option:
181 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/
190 Using `allow` list, we can override and allow specific operators.
192 Examples of **correct** code for the sample `{ "allow": ["!!", "~"] }` option:
197 /*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/
200 var b = ~foo.indexOf(".");
205 ## When Not To Use It
207 If you don't want to be notified about shorter notations for the type conversion, you can safely disable this rule.