@putout/plugin-conditions 
πPutout adds support of conditions transformations.
Install
npm i @putout/plugin-conditions -DRules
- β 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;
Config
{
"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"
}
}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-equal
Checkout in πPutout Editor.
β Example of incorrect code
return a.b = c;β Example of correct code
return a.b === c;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 aBooleanresult. 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);reverse
Checkout in πPutout Editor.
β Example of incorrect code
const check = (references) => !(references > 3);
return !(nextNode.type !== 'text' || nextNode.value !== ' ');β Example of correct code
const check = (references) => references <= 3;
return nextNode.type === 'text' && nextNode.value === ' ';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
Multiple
if...elsestatements can be nested to create an else if clause(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');merge-if-with-else
The
ifstatement executes a statementifa specified condition is truthy.(c) MDN
Checkout in Putout Editor.
β Example of incorrect code
if (!matchFn)
fix(from, to, path);
else if (matchFn(options))
fix(from, to, path);β Example of correct code
if (!matchFn || matchFn(options))
fix(from, to, path);remove-useless-else
You can skip the
elseblock if yourifblock always executes areturnstatement, 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();Comparison
| Linter | Rule | Fix |
|---|---|---|
| π Putout | conditions/remove-useless-else |
β |
| β£ ESLint | no-else-return |
β |
remove-useless-loop-condition
Checkout in πPutout Editor.
β Example of incorrect code
while (currentDirPath = getParentDirectory(currentDirPath)) {
if (!currentDirPath)
break;
}β Example of correct code
while (currentDirPath = getParentDirectory(currentDirPath)) {}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);
}wrap-with-block
If you use a declaration instead of a statement, it would be a
SyntaxError. For example, aletdeclaration 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.
β Example of incorrect code
const a = 5;
if (a) {}β Example of correct code
if (a) {
const a = 5;
}License
MIT