noUselessTernary
¥Summary
-
规则生效日期:
v1.5.0¥Rule available since:
v1.5.0 -
诊断类别:
lint/complexity/noUselessTernary¥Diagnostic Category:
lint/complexity/noUselessTernary -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-unneeded-ternary相同¥Same as
no-unneeded-ternary
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessTernary": "error" } } }}¥Description
当存在更简单的替代方案时,禁止使用三元运算符。
¥Disallow ternary operators when simpler alternatives exist.
在 JavaScript 中,使用条件表达式在两个布尔值之间进行选择,而不是使用逻辑非(!)或双非(!!)将测试转换为布尔值,这是一个常见的错误。
¥It’s a common mistake in JavaScript to use a conditional expression to select between two
boolean values instead of using the logical NOT (!) or double NOT (!!) to convert the test to a boolean.
¥Examples
¥Invalid
var a = x ? true : true;code-block.js:1:9 lint/complexity/noUselessTernary FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unnecessary use of boolean literals in conditional expression.
> 1 │ var a = x ? true : true;
│ ^^^^^^^^^^^^^^^
2 │
ℹ Simplify your code by directly assigning the result without using a ternary operator.
ℹ If your goal is negation, you may use the logical NOT (!) or double NOT (!!) operator for clearer and concise code.
Check for more details about NOT operator.
ℹ Unsafe fix: Remove the conditional expression with
1 │ var·a·=·x·?·true·:·true;
│ -----------
var a = foo === 1 ? false : true;code-block.js:1:9 lint/complexity/noUselessTernary FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unnecessary use of boolean literals in conditional expression.
> 1 │ var a = foo === 1 ? false : true;
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Simplify your code by directly assigning the result without using a ternary operator.
ℹ If your goal is negation, you may use the logical NOT (!) or double NOT (!!) operator for clearer and concise code.
Check for more details about NOT operator.
ℹ Unsafe fix: Remove the conditional expression with
1 │ - var·a·=·foo·===·1·?·false·:·true;
1 │ + var·a·=·foo·!==1;
2 2 │
var a = foo + 1 ? false : true;code-block.js:1:9 lint/complexity/noUselessTernary FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unnecessary use of boolean literals in conditional expression.
> 1 │ var a = foo + 1 ? false : true;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Simplify your code by directly assigning the result without using a ternary operator.
ℹ If your goal is negation, you may use the logical NOT (!) or double NOT (!!) operator for clearer and concise code.
Check for more details about NOT operator.
ℹ Unsafe fix: Remove the conditional expression with
1 │ - var·a·=·foo·+·1·?·false·:·true;
1 │ + var·a·=·!(foo·+·1·);
2 2 │
var a = foo + 1 ? true : false;code-block.js:1:9 lint/complexity/noUselessTernary FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unnecessary use of boolean literals in conditional expression.
> 1 │ var a = foo + 1 ? true : false;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Simplify your code by directly assigning the result without using a ternary operator.
ℹ If your goal is negation, you may use the logical NOT (!) or double NOT (!!) operator for clearer and concise code.
Check for more details about NOT operator.
ℹ Unsafe fix: Remove the conditional expression with
1 │ - var·a·=·foo·+·1·?·true·:·false;
1 │ + var·a·=·!!(foo·+·1·);
2 2 │
¥Valid
var a = x === 2 ? 'Yes' : 'No';var a = x === 2 ? 'Yes' : false;¥Resources
逻辑错误:https://web.nodejs.cn/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
¥Logical NOT: https://web.nodejs.cn/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号