πPutout plugin adds ability to transform code related to return
npm install @putout/plugin-return[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-return.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-return "npm"
> The return statement 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.
```
npm i putout @putout/plugin-return -D
- β
apply-early-return;
- β
convert-from-continue;
- β
convert-from-break;
- β
merge-with-next-sibling;
- β
remove-useless;
- β
simplify-boolean;
`json`
{
"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"
}
}
> 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
`js`
function get(a) {
const b = 0;
{
if (a > 0)
return 5;
return 7;
}
}
`js`
function get(a) {
if (a > 0)
return 5;
return 7;
}
> The continue statement 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.
`ts`
function x() {
if (a)
continue;
return b;
}
`js`
function x() {
if (a)
return;
return b;
}
> The break statement 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.
`ts`
function x() {
if (a)
break;
return false;
}
`js`
function x() {
if (a)
return;
return false;
}
Checkout in πPutout Editor.
`js`
function x() {
return;
{
hello: 'world';
}
return;
5;
return;
a ? 2 : 3;
}
`js`
function x() {
return {
hello: 'world',
};
return 5;
return a ? 2 : 3;
}
`js`
const traverse = ({push}) => {
return {
ObjectExpression(path) {
push(path);
},
};
};
`js`
const traverse = ({push}) => ({
ObjectExpression(path) {
push(path);
},
});
Check out in πPutout Editor.
`js`
function isA(a, b) {
if (a.length === b.length)
return true;
return false;
}
`js``
function isA(a, b) {
return a.length !== b.length;
}
MIT