packages/babel-plugin-minify-simplify/src/if-statement.js
function conditionalReturnToGuards(path) {
    const { node } = path;

    if (
      !path.inList ||
      !path.get("consequent").isBlockStatement() ||
      node.alternate
    ) {
      return;
    }

    let ret;
    let test;
    const exprs = [];
    const statements = node.consequent.body;

    for (let i = 0, statement; (statement = statements[i]); i++) {
      if (t.isExpressionStatement(statement)) {
        exprs.push(statement.expression);
      } else if (t.isIfStatement(statement)) {
        if (i < statements.length - 1) {
          // This isn't the last statement. Bail.
          return;
        }
        if (statement.alternate) {
          return;
        }
        if (!t.isReturnStatement(statement.consequent)) {
          return;
        }
        ret = statement.consequent;
        test = statement.test;
      } else {
        return;
      }
    }

    if (!test || !ret) {
      return;
    }

    exprs.push(test);

    const expr = exprs.length === 1 ? exprs[0] : t.sequenceExpression(exprs);

    const replacement = t.logicalExpression("&&", node.test, expr);

    path.replaceWith(t.ifStatement(replacement, ret, null));
  }
function conditionalReturnToGuards(path) {
    const { node } = path;

    if (
      !path.inList ||
      !path.get("consequent").isBlockStatement() ||
      node.alternate
    ) {
      return;
    }

    let ret;
    let test;
    const exprs = [];
    const statements = node.consequent.body;

    for (let i = 0, statement; (statement = statements[i]); i++) {
      if (t.isExpressionStatement(statement)) {
        exprs.push(statement.expression);
      } else if (t.isIfStatement(statement)) {
        if (i < statements.length - 1) {
          // This isn't the last statement. Bail.
          return;
        }
        if (statement.alternate) {
          return;
        }
        if (!t.isReturnStatement(statement.consequent)) {
          return;
        }
        ret = statement.consequent;
        test = statement.test;
      } else {
        return;
      }
    }

    if (!test || !ret) {
      return;
    }

    exprs.push(test);

    const expr = exprs.length === 1 ? exprs[0] : t.sequenceExpression(exprs);

    const replacement = t.logicalExpression("&&", node.test, expr);

    path.replaceWith(t.ifStatement(replacement, ret, null));
  }
function conditionalReturnToGuards ( path ) { const { node } = path ; if ( ! path . inList || ! path . get ( "consequent" ) . isBlockStatement ( ) || node . alternate ) { return ; } let ret ; let test ; const exprs = [ ] ; const statements = node . consequent . body ; for ( let i = 0 , statement ; ( statement = statements [ i ] ) ; i ++ ) { if ( t . isExpressionStatement ( statement ) ) { exprs . push ( statement . expression ) ; } else if ( t . isIfStatement ( statement ) ) { if ( i < statements . length - 1 ) { // This isn't the last statement. Bail. return ; } if ( statement . alternate ) { return ; } if ( ! t . isReturnStatement ( statement . consequent ) ) { return ; } ret = statement . consequent ; test = statement . test ; } else { return ; } } if ( ! test || ! ret ) { return ; } exprs . push ( test ) ; const expr = exprs . length === 1 ? exprs [ 0 ] : t . sequenceExpression ( exprs ) ; const replacement = t . logicalExpression ( "&&" , node . test , expr ) ; path . replaceWith ( t . ifStatement ( replacement , ret , null ) ) ; }
Make if statements with conditional returns in the body into an if statement that guards the rest of the block.
Make if statements with conditional returns in the body into an if statement that guards the rest of the block .