Skip to content

Latest commit

 

History

History
 
 

plugin-conditions

@putout/plugin-conditions NPM version

🐊Putout adds support of conditions transformations.

Install

npm i @putout/plugin-conditions -D

Rules

{
    "rules": {
        "conditions/apply-comparison-order": "on",
        "conditions/apply-if": "on",
        "conditions/convert-comparison-to-boolean": "on",
        "conditions/convert-equal-to-strict-equal": "on",
        "conditions/evaluate": "on",
        "conditions/remove-boolean": "on",
        "conditions/remove-constant": "on"
    }
}

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();

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');
}

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');

License

MIT