no-implicit-coercion

禁止速记类型转换

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

在 JavaScript 中,有很多不同的方法可以转换值类型。其中一些可能难以阅读和理解。

¥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:

var b = !!foo;
var b = ~foo.indexOf(".");
var n = +foo;
var n = -(-foo);
var n = foo - 0;
var n = 1 * foo;
var s = "" + foo;
foo += ``;

这些可以替换为以下代码:

¥Those can be replaced with the following code:

var b = Boolean(foo);
var b = foo.indexOf(".") !== -1;
var n = Number(foo);
var n = Number(foo);
var n = Number(foo);
var n = Number(foo);
var s = String(foo);
foo = String(foo);

规则详情

¥Rule Details

该规则旨在为类型转换标记较短的符号,然后提出更不言自明的符号。

¥This rule is aimed to flag shorter notations for the type conversion, then suggest a more self-explanatory notation.

选项

¥Options

此规则具有三个主要选项和一个覆盖选项以允许根据需要进行一些强制。

¥This rule has three main options and one override option to allow some coercions as required.

  • "boolean"(默认为 true) - 当这是 true 时,此规则警告 boolean 类型的较短类型转换。

    ¥"boolean" (true by default) - When this is true, this rule warns shorter type conversions for boolean type.

  • "number"(默认为 true) - 当这是 true 时,此规则警告 number 类型的较短类型转换。

    ¥"number" (true by default) - When this is true, this rule warns shorter type conversions for number type.

  • "string"(默认为 true) - 当这是 true 时,此规则警告 string 类型的较短类型转换。

    ¥"string" (true by default) - When this is true, this rule warns shorter type conversions for string type.

  • "disallowTemplateShorthand"(默认为 false) - 当这是 true 时,此规则会警告使用 ${expression} 形式的 string 类型转换。

    ¥"disallowTemplateShorthand" (false by default) - When this is true, this rule warns string type conversions using ${expression} form.

  • "allow"(默认为 empty) - 该数组中的每个条目可以是允许的 ~!!+- --* 之一。

    ¥"allow" (empty by default) - Each entry in this array can be one of ~, !!, +, - -, -, or * that are to be allowed.

请注意,allow 列表中的运算符 + 将允许 +foo(数字强制)以及 "" + foo(字符串强制)。

¥Note that operator + in allow list would allow +foo (number coercion) as well as "" + foo (string coercion).

boolean

默认 { "boolean": true } 选项的错误代码示例:

¥Examples of incorrect code for the default { "boolean": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var b = !!foo;
var b = ~foo.indexOf(".");
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.

默认 { "boolean": true } 选项的正确代码示例:

¥Examples of correct code for the default { "boolean": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var b = Boolean(foo);
var b = foo.indexOf(".") !== -1;

var n = ~foo; // This is a just bitwise not.

number

默认 { "number": true } 选项的错误代码示例:

¥Examples of incorrect code for the default { "number": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var n = +foo;
var n = -(-foo);
var n = foo - 0;
var n = 1 * foo;

默认 { "number": true } 选项的正确代码示例:

¥Examples of correct code for the default { "number": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var n = Number(foo);
var n = parseFloat(foo);
var n = parseInt(foo, 10);

var n = foo * 1/4; // `* 1` is allowed when followed by the `/` operator

string

默认 { "string": true } 选项的错误代码示例:

¥Examples of incorrect code for the default { "string": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var s = "" + foo;
var s = `` + foo;
foo += "";
foo += ``;

默认 { "string": true } 选项的正确代码示例:

¥Examples of correct code for the default { "string": true } option:

在线运行
/*eslint no-implicit-coercion: "error"*/

var s = String(foo);
foo = String(foo);

disallowTemplateShorthand

该选项不受 string 选项的影响。

¥This option is not affected by the string option.

{ "disallowTemplateShorthand": true } 选项的错误代码示例:

¥Examples of incorrect code for the { "disallowTemplateShorthand": true } option:

在线运行
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

var s = `${foo}`;

{ "disallowTemplateShorthand": true } 选项的正确代码示例:

¥Examples of correct code for the { "disallowTemplateShorthand": true } option:

在线运行
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

var s = String(foo);

var s = `a${foo}`;

var s = `${foo}b`;

var s = `${foo}${bar}`;

var s = tag`${foo}`;

默认 { "disallowTemplateShorthand": false } 选项的正确代码示例:

¥Examples of correct code for the default { "disallowTemplateShorthand": false } option:

在线运行
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/

var s = `${foo}`;

allow

使用 allow 列表,我们可以覆盖并允许特定的运算符。

¥Using allow list, we can override and allow specific operators.

示例 { "allow": ["!!", "~"] } 选项的正确代码示例:

¥Examples of correct code for the sample { "allow": ["!!", "~"] } option:

在线运行
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/

var b = !!foo;
var b = ~foo.indexOf(".");

何时不使用

¥When Not To Use It

如果你不想收到有关类型转换的较短符号的通知,你可以安全地禁用此规则。

¥If you don’t want to be notified about shorter notations for the type conversion, you can safely disable this rule.

版本

此规则是在 ESLint v1.0.0-rc-2 中引入。

资源

ESLint 中文网
粤ICP备13048890号