@putout/plugin-logical-expressions
🐊Putout plugin adds ability to transform logical expressions
Last updated 2 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-logical-expressions 
SYNC missed versions from official npm registry.

@putout/plugin-logical-expressions NPM version

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.

Install

npm i @putout/plugin-logical-expressions -D

Rules

  • βœ… [convert-bitwise-to-logical][#convert-bitwise-to-logical];
  • βœ… [remove-boolean][#remove-boolean];
  • βœ… [remove-duplicates][#remove-duplicates];
  • βœ… [simplify][#simplify];

Config

{
    "rules": {
        "logical-expressions/simplify": "on",
        "logical-expressions/remove-boolean": "on",
        "logical-expressions/remove-duplicates": "on",
        "logical-expressions/convert-bitwise-to-logical": "on"
    }
}

simplify

❌ Example of incorrect code

const is = !(options && !options.bidirectional);

if (!left.type === 'UnaryExpression') {}

const oneOf = a || a;
const same = a === a;

a() && b;

βœ… Example of correct code

const is = !options || options.bidirectional;

if (left.type !== 'UnaryExpression') {}

const oneOf = a;
const same = true;

a();

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

remove-boolean

A boolean is a logical data type that can have only the values true or false.

(c) MDN

❌ Example of incorrect code

const t = true && false;

βœ… Example of correct code

const t = false;

remove-duplicates

❌ Example of incorrect code

const t = a && b && a;

βœ… Example of correct code

const t = a && b;

convert-bitwise-to-logical

The bitwise OR operator (|) returns a 1 in each bit position for which the corresponding bits of either or both operands are 1s.

The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones).

(c) MDN

Convert bitwise to logical operator, when one of operands is not a number, since mostly likely it is an error.

❌ Example of incorrect code

a | !b;

if (!(a !== b))
    fn();

βœ… Example of correct code

a || !b;

if (a === b)
    fn();

Comparison

Linter Rule Fix
🐊 Putout logical-expressions βœ…
⏣ ESLint no-constant-binary-expression ❌

License

MIT

Current Tags

  • 6.0.0                                ...           latest (6 months ago)

13 Versions

  • 6.0.0                                ...           6 months ago
  • 5.0.0                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 3.2.0                                ...           a year ago
  • 3.1.0                                ...           a year ago
  • 3.0.2                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.1.0                                ...           a year ago
  • 2.0.0                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (8)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE