@putout/plugin-return
v2.0.0
Published
🐊Putout plugin adds ability to transform code related to return
Maintainers
Readme
@putout/plugin-return 
The
returnstatement ends function execution and specifies a value to be returned to the function caller.(c) MDN
🐊Putout plugin adds ability to transform to new Node.js API and apply best practices.
Install
npm i putout @putout/plugin-return -DRules
- ✅ apply-early-return;
- ✅ convert-from-continue;
- ✅ convert-from-break;
- ✅ merge-with-next-sibling;
- ✅ remove-useless;
- ✅ simplify-boolean;
Config
{
"rules": {
"return/apply-early-return": "on",
"return/convert-from-continue": "on",
"return/convert-from-break": "on",
"return/merge-with-next-sibling": "on",
"return/remove-useless": "on",
"return/simplify-boolean": "on"
}
}apply-early-return
In short, an early return provides functionality so the result of a conditional statement can be returned as soon as a result is available, rather than wait until the rest of the function is run.
(c) dev.to
❌ Example of incorrect code
function get(a) {
const b = 0;
{
if (a > 0)
return 5;
return 7;
}
}✅ Example of correct code
function get(a) {
if (a > 0)
return 5;
return 7;
}convert-from-continue
The
continuestatement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.(c) MDN
SyntaxError: Illegal continue statement: no surrounding iteration statement(c) MDN
Checkout in 🐊Putout Editor.
❌ Example of incorrect code
function x() {
if (a)
continue;
return b;
}✅ Example of correct code
function x() {
if (a)
return;
return b;
}convert-from-break
The
breakstatement terminates the current loop or switch statement and transfers program control to the statement following the terminated statement.(c) MDN
SyntaxError: unlabeled break must be inside loop or switch(c) MDN
Checkout in 🐊Putout Editor.
❌ Example of incorrect code
function x() {
if (a)
break;
return false;
}✅ Example of correct code
function x() {
if (a)
return;
return false;
}merge-with-next-sibling
Checkout in 🐊Putout Editor.
❌ Example of incorrect code
function x() {
return;
{
hello: 'world';
}
return;
5;
return;
a ? 2 : 3;
}✅ Example of correct code
function x() {
return {
hello: 'world',
};
return 5;
return a ? 2 : 3;
}remove-useless
❌ Example of incorrect code
const traverse = ({push}) => {
return {
ObjectExpression(path) {
push(path);
},
};
};✅ Example of correct code
const traverse = ({push}) => ({
ObjectExpression(path) {
push(path);
},
});simplify-boolean
Check out in 🐊Putout Editor.
❌ Example of incorrect code
function isA(a, b) {
if (a.length === b.length)
return true;
return false;
}✅ Example of correct code
function isA(a, b) {
return a.length !== b.length;
}License
MIT
