noConstantCondition
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/noConstantCondition¥Diagnostic Category:
lint/correctness/noConstantCondition -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
¥Same as
no-constant-condition
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noConstantCondition": "error" } } }}¥Description
禁止在条件中使用常量表达式
¥Disallow constant expressions in conditions
¥Examples
¥Invalid
if (false) { doSomethingUnfinished();}code-block.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ if (false) {
│ ^^^^^
2 │ doSomethingUnfinished();
3 │ }
if (Boolean(1)) { doSomethingAlways();}code-block.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ if (Boolean(1)) {
│ ^^^^^^^^^^
2 │ doSomethingAlways();
3 │ }
if (undefined) { doSomethingUnfinished();}code-block.js:1:5 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ if (undefined) {
│ ^^^^^^^^^
2 │ doSomethingUnfinished();
3 │ }
for (;-2;) { doSomethingForever();}code-block.js:1:7 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ for (;-2;) {
│ ^^
2 │ doSomethingForever();
3 │ }
while (typeof x) { doSomethingForever();}code-block.js:1:8 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ while (typeof x) {
│ ^^^^^^^^
2 │ doSomethingForever();
3 │ }
var result = 0 ? a : b;code-block.js:1:14 lint/correctness/noConstantCondition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected constant condition.
> 1 │ var result = 0 ? a : b;
│ ^
2 │
¥Valid
if (x === 0) { doSomething();}
for (;;) { doSomethingForever();}
while (typeof x === "undefined") { doSomething();}
do { doSomething();} while (x);
var result = x !== 0 ? a : b;
// Exceptionwhile (true) { if (x) { break; } x = f();}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号