npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@putout/plugin-conditions

v5.0.0

Published

🐊Putout plugin adds support of conditions transformations

Downloads

26,485

Readme

@putout/plugin-conditions NPM version

🐊Putout adds support of conditions transformations.

Install

npm i @putout/plugin-conditions -D

Rules

Config

{
    "rules": {
        "conditions/apply-consistent-blocks": "on",
        "conditions/apply-comparison-order": "on",
        "conditions/apply-if": "on",
        "conditions/add-return": "on",
        "conditions/convert-comparison-to-boolean": "on",
        "conditions/convert-equal-to-strict-equal": "on",
        "conditions/convert-arrow-to-condition": "on",
        "conditions/evaluate": "on",
        "conditions/remove-boolean": "on",
        "conditions/remove-constant": "on",
        "conditions/remove-zero": "on",
        "conditions/remove-useless-else": "on",
        "conditions/remove-same-values-condition": "on",
        "conditions/merge-if-statements": "on"
    }
}

apply-consistent-blocks

A block statement is used to group zero or more statements. The block is delimited by a pair of braces ("curly braces") and contains a list of zero or more statements and declarations.

(c) MDN

Check out in 🐊Putout Editor:

❌ Example of incorrect code

if (a > 3) {
    m();
}

if (a > 3)
    b = 5;
else {
    b = 6;
}

if (a > 3)
    b = 5;
else {
    b = 6;
    fn();
}

✅ Example of correct code

if (a > 3)
    m();

if (a > 3)
    b = 5;
else
    b = 6;

if (a > 3) {
    b = 5;
} else {
    b = 6;
    fn();
}

apply-comparison-order

The result of evaluating an equality operator is always of type boolean based on whether the comparison is true.

(c) MDN

Checkout it 🐊Putout Editor.

❌ Example of incorrect code

3 === a;
3 < b;

✅ Example of correct code

a === 3;
b > 3;

Comparison

Linter | Rule | Fix --------|-------|------------| 🐊 Putout| conditions/apply-comparison-order| ✅ ⏣ ESLint | yoda | ½

apply-if

❌ Example of incorrect code

if (2 > 3)
    ;

alert();

✅ Example of correct code

if (2 > 3)
    alert();

add-return

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

if (a)
    false;

✅ Example of correct code

if (a)
    return false;

convert-arrow-to-condition

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

if ((a) => b) {}

✅ Example of correct code

if (a <= b) {}

convert-comparison-to-boolean

Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal.

(c) MDN

❌ Example of incorrect code

const t = 2 < 3;

✅ Example of correct code

const t = false;

convert-equal-to-strict-equal

The strict equality operator (===) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator (==), the strict equality operator always considers operands of different types to be different.

(c) MDN

❌ Example of incorrect code

if (a == b) {}

✅ Example of correct code

if (a === b) {}

evaluate

The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.

(c) MDN

❌ Example of incorrect code

const a = [];
const c = a;

if (a)
    console.log(a);

✅ Example of correct code

const a = [];
const c = a;

console.log(a);

remove-boolean

❌ Example of incorrect code

if (a === true)
    alert();

✅ Example of correct code

if (a)
    alert();

remove-constant

❌ Example of incorrect code

function hi(a) {
    if (2 < 3) {
        console.log('hello');
        console.log('world');
    }
}

✅ Example of correct code

function hi(b) {
    console.log('hello');
    console.log('world');
}

remove-zero

❌ Example of incorrect code

if (b === 0) {}

if (b !== 0) {}

✅ Example of correct code

if (!b) {}

if (b) {}

simplify

❌ Example of incorrect code

if (zone?.tooltipCallback)
    zone.tooltipCallback(e);

if (a)
    alert('hello');
else
    alert('hello');

✅ Example of correct code

zone?.tooltipCallback(e);

alert('hello');

merge-if-statements

The if statement executes a statement if a specified condition is truthy.

(c) MDN

❌ Example of incorrect code

if (a > b)
    if (b < c)
        console.log('hello');

✅ Example of correct code

if (a > b && b < c)
    console.log('hello');

remove-useless-else

You can skip the else block if your if block always executes a return statement, it makes code a lot easier to read.

(c) no else return

Remove useless else before:

  • return;
  • continue;
  • break;

❌ Example of incorrect code

if (x)
    return;
else
    console.log();

✅ Example of correct code

if (x)
    return;

console.log();

remove-same-values-condition

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

for (const [i, el] of entries(elements)) {
    if (el !== path)
        continue;
    
    if (!Number(i) && n) {
        path.parentPath.node.elements[i] = null;
        break;
    }
    
    if (el === path) {
        remove(path);
        break;
    }
}

✅ Example of correct code

for (const [i, el] of entries(elements)) {
    if (el !== path)
        continue;
    
    if (!Number(i) && n) {
        path.parentPath.node.elements[i] = null;
        break;
    }
    
    remove(path);
}

Comparison

Linter | Rule | Fix --------|-------|------------| 🐊 Putout | conditions/remove-useless-else | ✅ ⏣ ESLint | no-else-return | ✅

License

MIT