Socket
Socket
Sign inDemoInstall

@putout/plugin-logical-expressions

Package Overview
Dependencies
1
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @putout/plugin-logical-expressions

🐊Putout plugin adds ability to transform logical expressions


Version published
Weekly downloads
3.7K
increased by4.3%
Maintainers
1
Install size
23.7 MB
Created
Weekly downloads
 

Readme

Source

@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

Rule

{
    "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

LinterRuleFix
🐊 Putoutlogical-expressions
ESLintno-constant-binary-expression

License

MIT

Keywords

FAQs

Last updated on 28 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc