Disallow shorthand type conversions
Some problems reported by this rule are automatically fixable by the --fix command line option
Some problems reported by this rule are manually fixable by editor suggestions
This rule is currently frozen and is not accepting feature requests.
In JavaScript, there are a lot of different ways to convert value types. Some of them might be hard to read and understand.
Such as:
const b = !!foo;
const b1 = ~foo.indexOf(".");
const n = +foo;
const n1 = -(-foo);
const n2 = foo - 0;
const n3 = 1 * foo;
const s = "" + foo;
foo += ``;
Those can be replaced with the following code:
const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;
const n = Number(foo);
const n1 = Number(foo);
const n2 = Number(foo);
const n3 = Number(foo);
const s = String(foo);
foo = String(foo);
This rule is aimed to flag shorter notations for the type conversion, then suggest a more self-explanatory notation.
This rule has three main options and one override option to allow some coercions as required.
"boolean" (true by default) - When this is true, this rule warns shorter type conversions for boolean type."number" (true by default) - When this is true, this rule warns shorter type conversions for number type."string" (true by default) - When this is true, this rule warns shorter type conversions for string type."disallowTemplateShorthand" (false by default) - When this is true, this rule warns string type conversions using ${expression} form."allow" (empty by default) - Each entry in this array can be one of ~, !!, +, - -, -, or * that are to be allowed.Note that operator + in allow list would allow +foo (number coercion) as well as "" + foo (string coercion).
Examples of incorrect code for the default { "boolean": true } option:
/*eslint no-implicit-coercion: "error"*/
const b = ;
const b1 = ;
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
Examples of correct code for the default { "boolean": true } option:
/*eslint no-implicit-coercion: "error"*/
const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;
const n = ~foo; // This is a just bitwise not.
Examples of incorrect code for the default { "number": true } option:
/*eslint no-implicit-coercion: "error"*/
const n = ;
const n1 = ;
const n2 = ;
const n3 = ;
Examples of correct code for the default { "number": true } option:
/*eslint no-implicit-coercion: "error"*/
const n = Number(foo);
const n1 = parseFloat(foo);
const n2 = parseInt(foo, 10);
const n3 = foo * 1/4; // `* 1` is allowed when followed by the `/` operator
Examples of incorrect code for the default { "string": true } option:
/*eslint no-implicit-coercion: "error"*/
const s = ;
const s1 = ;
;
;
Examples of correct code for the default { "string": true } option:
/*eslint no-implicit-coercion: "error"*/
const s = String(foo);
foo = String(foo);
This option is not affected by the string option.
Examples of incorrect code for the { "disallowTemplateShorthand": true } option:
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
const s = ;
Examples of correct code for the { "disallowTemplateShorthand": true } option:
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
const s = String(foo);
const s1 = `a${foo}`;
const s2 = `${foo}b`;
const s3 = `${foo}${bar}`;
const s4 = tag`${foo}`;
Examples of correct code for the default { "disallowTemplateShorthand": false } option:
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/
const s = `${foo}`;
Using allow list, we can override and allow specific operators.
Examples of correct code for the sample { "allow": ["!!", "~"] } option:
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/
const b = !!foo;
const b1 = ~foo.indexOf(".");
If you don’t want to be notified about shorter notations for the type conversion, you can safely disable this rule.
This rule was introduced in ESLint v1.0.0-rc-2.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-implicit-coercion