@putout/plugin-simplify-logical-expressions
v4.0.2
Published
šPutout plugin adds ability to simplify logical expressions
Downloads
5,095
Maintainers
Readme
@putout/plugin-simplify-logical-expressions
The logical NOT (
!
) operator takes truth to falsity and vice versa.(c) MDN
šPutout plugin adds ability to simplify logical expressions containing
comparisons which will always evaluate to true
or false
since it's likely indications of programmer error.
Complements @putout/plugin-apply-comparison-order
.
Merged to @putout/plugin-logical-expressions.
Install
npm i @putout/plugin-simplify-logical-expressions -D
Rule
{
"rules": {
"simplify-logical-expressions": "on"
}
}
ā Example of incorrect code
const is = !(options && !options.bidirectional);
if (!left.type === 'UnaryExpression');
const oneOf = a || a;
const same = a === a;
ā Example of correct code
const is = !options || options.bidirectional;
if (left.type !== 'UnaryExpression');
const oneOf = a;
const same = true;
The rule also simplify duplication use:
-if (a && b || a && c) {
+if (a && (b || c)) {
}
Wrong cases with instanceof
:
-!a instanceof b;
-a instanceof !b;
-!a instanceof !b;
+!(a instanceof b);
Wrong cases with in
:
-!a in b;
-a in !b;
+!(a in b);
In case of duplicates:
-a && b && a
+a && b
Comparison
Linter | Rule | Fix
--------|-------|------------|
š Putout| simplify-logical-expressions
| ā
ā£ ESLint | no-constant-binary-expression
| ā
License
MIT