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"*/
const foo = !!!bar;
const foo1 = !!bar ? baz : bat;
const foo2 = Boolean(!!bar);
const foo3 = 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"*/
const foo = !!bar;
const foo1 = Boolean(bar);
function qux() {
return !!bar;
}
foo = bar ? !!baz : !!bat;
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
"enforceForInnerExpressions"设置为true时,除了检查默认上下文外,还会检查在布尔上下文中使用其结果的表达式中是否存在额外的布尔类型转换。见下方示例。默认值为false,这意味着默认情况下,该规则不会提示内层表达式中额外的布尔类型转换。
已弃用: 对象属性 enforceForLogicalOperands 已弃用 (eslint#18222)。请改用 enforceForInnerExpressions。
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) {
//...
}
const 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.
const foo = !!bar || baz;
if (foo || bar) {
//...
}
while (foo && bar) {
//...
}
if ((foo || bar) && baz) {
//...
}
const foo1 = 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 中引入。