🐊Putout adds support of conditions transformations.
npm i @putout/plugin-conditions -D
- ✅ add-return;
- ✅ apply-comparison-order;
- ✅ apply-consistent-blocks;
- ✅ apply-early-return;
- ✅ apply-equal;
- ✅ apply-if;
- ✅ convert-arrow-to-condition;
- ✅ convert-comparison-to-boolean;
- ✅ convert-equal-to-strict-equal;
- ✅ convert-switch-to-if;
- ✅ evaluate;
- ✅ merge-if-statements;
- ✅ merge-if-with-else;
- ✅ remove-boolean;
- ✅ remove-constant;
- ✅ remove-same-values-condition;
- ✅ remove-useless-else;
- ✅ remove-useless-loop-condition;
- ✅ remove-zero;
- ✅ reverse;
- ✅ simplify;
- ✅ wrap-with-block;
{
"rules": {
"conditions/add-return": "on",
"conditions/apply-comparison-order": "on",
"conditions/apply-consistent-blocks": "on",
"conditions/apply-equal": "on",
"conditions/apply-if": "on",
"conditions/apply-return-return": "on",
"conditions/convert-arrow-to-condition": "on",
"conditions/convert-comparison-to-boolean": "on",
"conditions/convert-equal-to-strict-equal": "on",
"conditions/convert-switch-to-if": "on",
"conditions/evaluate": "on",
"conditions/merge-if-statements": "on",
"conditions/merge-if-with-else": "on",
"conditions/remove-boolean": "on",
"conditions/remove-constant": "on",
"conditions/remove-same-values-condition": "on",
"conditions/remove-useless-else": "on",
"conditions/remove-useless-loop-condition": "on",
"conditions/remove-zero": "on",
"conditions/reverse": "on",
"conditions/simplify": "on",
"conditions/wrap-with-block": "on"
}
}Checkout in 🐊Putout Editor.
if (a)
false;if (a)
return false;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.
3 === a;
3 < b;a === 3;
b > 3;| Linter | Rule | Fix |
|---|---|---|
| 🐊 Putout | conditions/apply-comparison-order |
✅ |
| ⏣ ESLint | yoda |
½ |
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:
if (a > 3) {
m();
}
if (a > 3)
b = 5;
else {
b = 6;
}
if (a > 3)
b = 5;
else {
b = 6;
fn();
}if (a > 3)
m();
if (a > 3)
b = 5;
else
b = 6;
if (a > 3) {
b = 5;
} else {
b = 6;
fn();
}Checkout in 🐊Putout Editor.
function notBlockNoNext() {
if (a)
x();
else
b();
}function notBlockNoNext() {
if (a) {
x();
return;
}
b();
}Checkout in 🐊Putout Editor.
return a.b = c;return a.b === c;if (2 > 3)
;
alert();if (2 > 3)
alert();Checkout in 🐊Putout Editor.
if ((a) => b) {}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
const t = 2 < 3;const t = false;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
if (a == b) {}if (a === b) {}The
switchstatement evaluates anexpression, matching the expression's value against a series of case clauses, and executesstatementsafter the first case clause with a matching value, until a break statement is encountered.(c) MDN
Checkout in 🐊Putout Editor.
function report(path) {
switch(path.node.type) {
case 'ImportFinding':
return 'remove unused import: ' + path.node.path;
}
return 'remove unused variable';
}function report(path) {
if (path.node.type === 'ImportFinding')
return 'remove unused import: ' + path.node.path;
return 'remove unused variable';
}The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.
(c) MDN
const a = [];
const c = a;
if (a)
console.log(a);const a = [];
const c = a;
console.log(a);Multiple
if...elsestatements can be nested to create an else if clause(c) MDN
if (a > b)
if (b < c)
console.log('hello');if (a > b && b < c)
console.log('hello');The
ifstatement executes a statementifa specified condition is truthy.(c) MDN
Checkout in Putout Editor.
if (!matchFn)
fix(from, to, path);
else if (matchFn(options))
fix(from, to, path);if (!matchFn || matchFn(options))
fix(from, to, path);if (a === true)
alert();if (a)
alert();function hi(a) {
if (2 < 3) {
console.log('hello');
console.log('world');
}
}function hi(b) {
console.log('hello');
console.log('world');
}Checkout in 🐊Putout Editor.
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;
}
}for (const [i, el] of entries(elements)) {
if (el !== path)
continue;
if (!Number(i) && n) {
path.parentPath.node.elements[i] = null;
break;
}
remove(path);
}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;
if (x)
return;
else
console.log();if (x)
return;
console.log();| Linter | Rule | Fix |
|---|---|---|
| 🐊 Putout | conditions/remove-useless-else |
✅ |
| ⏣ ESLint | no-else-return |
✅ |
Checkout in 🐊Putout Editor.
while (currentDirPath = getParentDirectory(currentDirPath)) {
if (!currentDirPath)
break;
}while (currentDirPath = getParentDirectory(currentDirPath)) {}if (b === 0) {}
if (b !== 0) {}if (!b) {}
if (b) {}Checkout in 🐊Putout Editor.
const check = (references) => !(references > 3);
return !(nextNode.type !== 'text' || nextNode.value !== ' ');const check = (references) => references <= 3;
return nextNode.type === 'text' && nextNode.value === ' ';if (zone?.tooltipCallback)
zone.tooltipCallback(e);
if (a)
alert('hello');
else
alert('hello');zone?.tooltipCallback(e);
alert('hello');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.
if (a)
const b = 5;
if (a) {
const b = 5;
}MIT