noUselessTernary
Ce contenu n’est pas encore disponible dans votre langue.
Diagnostic Category: lint/complexity/noUselessTernary
Since: v1.5.0
Sources:
- Same as:
no-unneeded-ternary
Disallow ternary operators when simpler alternatives exist.
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
Section titled ExamplesInvalid
Section titled Invalidcode-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;
│ -----------
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 │
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 │
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
Section titled ValidResources
Section titled ResourcesLogical NOT: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT