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

            // Need to be careful of side-effects.
            if (!t.isIdentifier(node.discriminant)) {
              return;
            }

            if (!node.cases.length) {
              return;
            }

            const consTestPairs = [];
            let fallThru = [];
            let defaultRet;
            for (const switchCase of node.cases) {
              if (switchCase.consequent.length > 1) {
                return;
              }

              const cons = switchCase.consequent[0];

              // default case
              if (!switchCase.test) {
                if (!t.isReturnStatement(cons)) {
                  return;
                }
                defaultRet = cons;
                continue;
              }

              if (!switchCase.consequent.length) {
                fallThru.push(switchCase.test);
                continue;
              }

              // TODO: can we void it?
              if (!t.isReturnStatement(cons)) {
                return;
              }

              let test = t.binaryExpression(
                "===",
                node.discriminant,
                switchCase.test
              );

              if (fallThru.length && !defaultRet) {
                test = fallThru.reduceRight(
                  (right, test) =>
                    t.logicalExpression(
                      "||",
                      t.binaryExpression("===", node.discriminant, test),
                      right
                    ),
                  test
                );
              }
              fallThru = [];

              consTestPairs.push([test, cons.argument || VOID_0]);
            }

            // Bail if we have any remaining fallthrough
            if (fallThru.length) {
              return;
            }

            // We need the default to be there to make sure there is an oppurtinity
            // not to return.
            if (!defaultRet) {
              if (path.inList) {
                const nextPath = path.getSibling(path.key + 1);
                if (nextPath.isReturnStatement()) {
                  defaultRet = nextPath.node;
                  nextPath.remove();
                } else if (
                  !nextPath.node &&
                  path.parentPath.parentPath.isFunction()
                ) {
                  // If this is the last statement in a function we just fake a void return.
                  defaultRet = t.returnStatement(VOID_0);
                } else {
                  return;
                }
              } else {
                return;
              }
            }

            const cond = consTestPairs.reduceRight(
              (alt, [test, cons]) => t.conditionalExpression(test, cons, alt),
              defaultRet.argument || VOID_0
            );

            path.replaceWith(t.returnStatement(cond));

            // Maybe now we can merge with some previous switch statement.
            if (path.inList) {
              const prev = path.getSibling(path.key - 1);
              if (prev.isSwitchStatement()) {
                prev.visit();
              }
            }
          }
function(path) {
            const { node } = path;

            // Need to be careful of side-effects.
            if (!t.isIdentifier(node.discriminant)) {
              return;
            }

            if (!node.cases.length) {
              return;
            }

            const consTestPairs = [];
            let fallThru = [];
            let defaultRet;
            for (const switchCase of node.cases) {
              if (switchCase.consequent.length > 1) {
                return;
              }

              const cons = switchCase.consequent[0];

              // default case
              if (!switchCase.test) {
                if (!t.isReturnStatement(cons)) {
                  return;
                }
                defaultRet = cons;
                continue;
              }

              if (!switchCase.consequent.length) {
                fallThru.push(switchCase.test);
                continue;
              }

              // TODO: can we void it?
              if (!t.isReturnStatement(cons)) {
                return;
              }

              let test = t.binaryExpression(
                "===",
                node.discriminant,
                switchCase.test
              );

              if (fallThru.length && !defaultRet) {
                test = fallThru.reduceRight(
                  (right, test) =>
                    t.logicalExpression(
                      "||",
                      t.binaryExpression("===", node.discriminant, test),
                      right
                    ),
                  test
                );
              }
              fallThru = [];

              consTestPairs.push([test, cons.argument || VOID_0]);
            }

            // Bail if we have any remaining fallthrough
            if (fallThru.length) {
              return;
            }

            // We need the default to be there to make sure there is an oppurtinity
            // not to return.
            if (!defaultRet) {
              if (path.inList) {
                const nextPath = path.getSibling(path.key + 1);
                if (nextPath.isReturnStatement()) {
                  defaultRet = nextPath.node;
                  nextPath.remove();
                } else if (
                  !nextPath.node &&
                  path.parentPath.parentPath.isFunction()
                ) {
                  // If this is the last statement in a function we just fake a void return.
                  defaultRet = t.returnStatement(VOID_0);
                } else {
                  return;
                }
              } else {
                return;
              }
            }

            const cond = consTestPairs.reduceRight(
              (alt, [test, cons]) => t.conditionalExpression(test, cons, alt),
              defaultRet.argument || VOID_0
            );

            path.replaceWith(t.returnStatement(cond));

            // Maybe now we can merge with some previous switch statement.
            if (path.inList) {
              const prev = path.getSibling(path.key - 1);
              if (prev.isSwitchStatement()) {
                prev.visit();
              }
            }
          }
function ( path ) { const { node } = path ; // Need to be careful of side-effects. if ( ! t . isIdentifier ( node . discriminant ) ) { return ; } if ( ! node . cases . length ) { return ; } const consTestPairs = [ ] ; let fallThru = [ ] ; let defaultRet ; for ( const switchCase of node . cases ) { if ( switchCase . consequent . length > 1 ) { return ; } const cons = switchCase . consequent [ 0 ] ; // default case if ( ! switchCase . test ) { if ( ! t . isReturnStatement ( cons ) ) { return ; } defaultRet = cons ; continue ; } if ( ! switchCase . consequent . length ) { fallThru . push ( switchCase . test ) ; continue ; } // TODO: can we void it? if ( ! t . isReturnStatement ( cons ) ) { return ; } let test = t . binaryExpression ( "===" , node . discriminant , switchCase . test ) ; if ( fallThru . length && ! defaultRet ) { test = fallThru . reduceRight ( ( right , test ) => t . logicalExpression ( "||" , t . binaryExpression ( "===" , node . discriminant , test ) , right ) , test ) ; } fallThru = [ ] ; consTestPairs . push ( [ test , cons . argument || VOID_0 ] ) ; } // Bail if we have any remaining fallthrough if ( fallThru . length ) { return ; } // We need the default to be there to make sure there is an oppurtinity // not to return. if ( ! defaultRet ) { if ( path . inList ) { const nextPath = path . getSibling ( path . key + 1 ) ; if ( nextPath . isReturnStatement ( ) ) { defaultRet = nextPath . node ; nextPath . remove ( ) ; } else if ( ! nextPath . node && path . parentPath . parentPath . isFunction ( ) ) { // If this is the last statement in a function we just fake a void return. defaultRet = t . returnStatement ( VOID_0 ) ; } else { return ; } } else { return ; } } const cond = consTestPairs . reduceRight ( ( alt , [ test , cons ] ) => t . conditionalExpression ( test , cons , alt ) , defaultRet . argument || VOID_0 ) ; path . replaceWith ( t . returnStatement ( cond ) ) ; // Maybe now we can merge with some previous switch statement. if ( path . inList ) { const prev = path . getSibling ( path . key - 1 ) ; if ( prev . isSwitchStatement ( ) ) { prev . visit ( ) ; } } }
Convert switch statements with all returns in their cases to return conditional.
Convert switch statements with all returns in their cases to return conditional .