@putout/plugin-return

1.1.2Β β€’Β PublicΒ β€’Β Published

@putout/plugin-return NPM version

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.

Install

npm i putout @putout/plugin-return -D

Rules

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 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.

❌ 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 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.

❌ 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

/@putout/plugin-return/

    Package Sidebar

    Install

    npm i @putout/plugin-return

    Weekly Downloads

    2,863

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    14.8 kB

    Total Files

    11

    Last publish

    Collaborators

    • coderaiser