🐊Putout plugin adds ability to simplify logical expressions
npm install @putout/plugin-simplify-logical-expressions[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-simplify-logical-expressions.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-simplify-logical-expressions "npm"
> 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.
```
npm i @putout/plugin-simplify-logical-expressions -D
`json`
{
"rules": {
"simplify-logical-expressions": "on"
}
}
`js
const is = !(options && !options.bidirectional);
if (!left.type === 'UnaryExpression');
const oneOf = a || a;
const same = a === a;
`
`js
const is = !options || options.bidirectional;
if (left.type !== 'UnaryExpression');
const oneOf = a;
const same = true;
`
The rule also simplify duplication use:
`diff`
-if (a && b || a && c) {
+if (a && (b || c)) {
}
Wrong cases with instanceof:
`diff`
-!a instanceof b;
-a instanceof !b;
-!a instanceof !b;
+!(a instanceof b);
Wrong cases with in:
`diff`
-!a in b;
-a in !b;
+!(a in b);
In case of duplicates:
`diff`
-a && b && a
+a && b
Linter | Rule | Fix
--------|-------|------------|
🐊 Putout| simplify-logical-expressions| ✅
⏣ ESLint | no-constant-binary-expression` | ❌
MIT