πPutout plugin adds support of conditions transformations
npm install @putout/plugin-conditions[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-conditions.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-conditions "npm"
πPutout adds support of conditions transformations.
```
npm i @putout/plugin-conditions -D
- β
add-return;
- β
apply-comparison-order;
- β
apply-consistent-blocks;
- β
apply-equal;
- β
apply-if;
- β
convert-comparison-to-boolean;
- β
convert-equal-to-strict-equal;
- β
convert-arrow-to-condition;
- β
evaluate;
- β
merge-if-statements;
- β
merge-if-with-else;
- β
reverse;
- β
remove-boolean;
- β
remove-constant;
- β
remove-same-values-condition;
- β
remove-useless-else;
- β
remove-useless-loop-condition;
- β
remove-zero;
- β
simplify;
- β
wrap-with-block;
`json`
{
"rules": {
"conditions/apply-consistent-blocks": "on",
"conditions/apply-comparison-order": "on",
"conditions/apply-equal": "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/reverse": "on",
"conditions/remove-boolean": "on",
"conditions/remove-constant": "on",
"conditions/remove-zero": "on",
"conditions/remove-useless-else": "on",
"conditions/remove-useless-loop-condition": "on",
"conditions/remove-same-values-condition": "on",
"conditions/merge-if-statements": "on",
"conditions/merge-if-with-else": "on",
"conditions/simplify": "on",
"conditions/wrap-with-block": "on"
}
}
> 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:
`js
if (a > 3) {
m();
}
if (a > 3)
b = 5;
else {
b = 6;
}
if (a > 3)
b = 5;
else {
b = 6;
fn();
}
`
`js
if (a > 3)
m();
if (a > 3)
b = 5;
else
b = 6;
if (a > 3) {
b = 5;
} else {
b = 6;
fn();
}
`
> 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.
`js`
3 === a;
3 < b;
`js`
a === 3;
b > 3;
Linter | Rule | Fix
--------|-------|------------|
π Putout| conditions/apply-comparison-order| β
β£ ESLint | yoda | Β½
Checkout in πPutout Editor.
`js`
return a.b = c;
`js`
return a.b === c;
`js
if (2 > 3)
;
alert();
`
`js`
if (2 > 3)
alert();
Checkout in πPutout Editor.
`js`
if (a)
false;
`js`
if (a)
return false;
Checkout in πPutout Editor.
`js`
if ((a) => b) {}
`js`
if (a <= b) {}
> 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
`js`
const t = 2 < 3;
`js`
const t = false;
> 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
`js`
if (a == b) {}
`js`
if (a === b) {}
> The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.
>
> (c) MDN
`js
const a = [];
const c = a;
if (a)
console.log(a);
`
`js
const a = [];
const c = a;
console.log(a);
`
Checkout in πPutout Editor.
`js
const check = (references) => !(references > 3);
return !(nextNode.type !== 'text' || nextNode.value !== ' ');
`
`js
const check = (references) => references <= 3;
return nextNode.type === 'text' && nextNode.value === ' ';
`
`js`
if (a === true)
alert();
`js`
if (a)
alert();
`js`
function hi(a) {
if (2 < 3) {
console.log('hello');
console.log('world');
}
}
`js`
function hi(b) {
console.log('hello');
console.log('world');
}
`js
if (b === 0) {}
if (b !== 0) {}
`
`js
if (!b) {}
if (b) {}
`
`js
if (zone?.tooltipCallback)
zone.tooltipCallback(e);
if (a)
alert('hello');
else
alert('hello');
`
`js
zone?.tooltipCallback(e);
alert('hello');
`
> Multiple if...else statements can be nested to create an else if clause
>
> (c) MDN
`js`
if (a > b)
if (b < c)
console.log('hello');
`js`
if (a > b && b < c)
console.log('hello');
> The if statement executes a statement if a specified condition is truthy.
>
> (c) MDN
Checkout in Putout Editor.
`js`
if (!matchFn)
fix(from, to, path);
else if (matchFn(options))
fix(from, to, path);
`js`
if (!matchFn || matchFn(options))
fix(from, to, path);
> 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
- ;
`js`
if (x)
return;
else
console.log();
`js
if (x)
return;
console.log();
`
Linter | Rule | Fix
--------|-------|------------|
π Putout | conditions/remove-useless-else | β
β£ ESLint | no-else-return | β
Checkout in πPutout Editor.
`js`
while (currentDirPath = getParentDirectory(currentDirPath)) {
if (!currentDirPath)
break;
}
`js`
while (currentDirPath = getParentDirectory(currentDirPath)) {}
Checkout in πPutout Editor.
`js`
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;
}
}
`js`
for (const [i, el] of entries(elements)) {
if (el !== path)
continue;
if (!Number(i) && n) {
path.parentPath.node.elements[i] = null;
break;
}
remove(path);
}
> If you use a declaration instead of a statement, it would be a SyntaxError. For example, a let declaration is not a statement, so you can't use it in its bare form as the body of an if statement.
>
> (c) MDN
Checkout in πPutout Editor.
`js
const a = 5;
if (a) {}
`
`js``
if (a) {
const a = 5;
}
MIT