no-extra-boolean-cast
禁止不必要的布尔转换
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
此规则报告的一些问题可通过 --fix
命令行选项自动修复
在 if
语句的测试等上下文中,表达式的结果已经被强制转换为布尔值,不需要通过双重否定 (!!
) 或 Boolean
调用转换为布尔值。例如,这些 if
语句是等效的:
¥In contexts such as an if
statement’s test where the result of the expression will already be coerced to a Boolean, casting to a Boolean via double negation (!!
) or a Boolean
call is unnecessary. For example, these if
statements are equivalent:
if (!!foo) {
// ...
}
if (Boolean(foo)) {
// ...
}
if (foo) {
// ...
}
规则详情
¥Rule Details
此规则不允许不必要的布尔类型转换。
¥This rule disallows unnecessary boolean casts.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-extra-boolean-cast: "error"*/
var foo = !!!bar;
var foo = !!bar ? baz : bat;
var foo = Boolean(!!bar);
var foo = new Boolean(!!bar);
if (!!foo) {
// ...
}
if (Boolean(foo)) {
// ...
}
while (!!foo) {
// ...
}
do {
// ...
} while (Boolean(foo));
for (; !!foo; ) {
// ...
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-extra-boolean-cast: "error"*/
var foo = !!bar;
var foo = Boolean(bar);
function qux() {
return !!bar;
}
var foo = bar ? !!baz : !!bat;
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"enforceForInnerExpressions"
当设置为true
时,除了检查默认上下文之外,还会检查其结果在布尔上下文中使用的表达式中是否存在额外的布尔转换。请参阅下面的示例。默认值为false
,这意味着默认情况下此规则不会警告内部表达式内的额外布尔值转换。¥
"enforceForInnerExpressions"
when set totrue
, in addition to checking default contexts, checks whether extra boolean casts are present in expressions whose result is used in a boolean context. See examples below. Default isfalse
, meaning that this rule by default does not warn about extra booleans cast inside inner expressions.
已弃用:对象属性 enforceForLogicalOperands
已弃用 (eslint#18222)。请使用 enforceForInnerExpressions
代替。
¥Deprecated: The object property enforceForLogicalOperands
is deprecated (eslint#18222). Please use enforceForInnerExpressions
instead.
enforceForInnerExpressions
此规则的错误代码示例("enforceForInnerExpressions"
选项设置为 true
):
¥Examples of incorrect code for this rule with "enforceForInnerExpressions"
option set to true
:
/*eslint no-extra-boolean-cast: ["error", {"enforceForInnerExpressions": true}]*/
if (!!foo || bar) {
//...
}
while (!!foo && bar) {
//...
}
if ((!!foo || bar) && !!baz) {
//...
}
var foo = new Boolean(!!bar || baz);
foo && Boolean(bar) ? baz : bat;
const ternaryBranches = Boolean(bar ? !!baz : bat);
const nullishCoalescingOperator = Boolean(bar ?? Boolean(baz));
const commaOperator = Boolean((bar, baz, !!bat));
// another comma operator example
for (let i = 0; console.log(i), Boolean(i < 10); i++) {
// ...
}
此规则的正确代码示例("enforceForInnerExpressions"
选项设置为 true
):
¥Examples of correct code for this rule with "enforceForInnerExpressions"
option set to true
:
/*eslint no-extra-boolean-cast: ["error", {"enforceForInnerExpressions": true}]*/
// Note that `||` and `&&` alone aren't a boolean context for either operand
// since the resultant value need not be a boolean without casting.
var foo = !!bar || baz;
if (foo || bar) {
//...
}
while (foo && bar) {
//...
}
if ((foo || bar) && baz) {
//...
}
var foo = new Boolean(bar || baz);
foo && bar ? baz : bat;
const ternaryBranches = Boolean(bar ? baz : bat);
const nullishCoalescingOperator = Boolean(bar ?? baz);
const commaOperator = Boolean((bar, baz, bat));
// another comma operator example
for (let i = 0; console.log(i), i < 10; i++) {
// ...
}
// comma operator in non-final position
Boolean((Boolean(bar), baz, bat));
版本
此规则是在 ESLint v0.4.0 中引入。