Repository: babel/babel
File: packages/babel-plugin-transform-regenerator/src/regenerator/emit.ts (TypeScript)

Code before:
        const tryEntry = new leap.TryEntry(
          self.getUnmarkedCurrentLoc(),
          catchEntry,
          finallyEntry,
        );

        self.tryEntries.push(tryEntry);
        self.updateContextPrevLoc(tryEntry.firstLoc);

        self.leapManager.withEntry(tryEntry, () => {
          self.explodeStatement(path.get("block"));

          if (catchLoc) {
            if (finallyLoc) {
              // If we have both a catch block and a finally block, then
              // because we emit the catch block first, we need to jump over
              // it to the finally block.
              self.jump(finallyLoc);
            } else {
              // If there is no finally block, then we need to jump over the
              // catch block to the fall-through location.
              self.jump(after);
            }

            self.updateContextPrevLoc(self.mark(catchLoc));

            const bodyPath = path.get("handler.body");
            const safeParam = self.makeTempVar();
            if (
              process.env.BABEL_8_BREAKING ||
              util.newHelpersAvailable(this.pluginPass)
            ) {
              this.emitAssign(safeParam, self.contextProperty("v"));
            } else {
              self.clearPendingException(tryEntry.firstLoc, safeParam);
            }

            bodyPath.traverse(catchParamVisitor, {
              getSafeParam: () => t.cloneNode(safeParam),
              catchParamName:
                // @ts-expect-error Assuming `handler.param` is `t.Identifier`
                handler.param.name,
            });

            self.leapManager.withEntry(catchEntry, function () {
              self.explodeStatement(bodyPath);
            });
          }

          if (finallyLoc) {
            self.updateContextPrevLoc(self.mark(finallyLoc));

Review comment (suggestion):
Avoid generating redundant `return` for the following code.
```js
function* gen(delegate) {
  try {
    return yield* delegate;
  } catch (err) {
    return err;
  }
}
```

Code after:
          finallyLoc && new leap.FinallyEntry(finallyLoc, after);

        const tryEntry = new leap.TryEntry(
          self.getUnmarkedCurrentLoc(),
          catchEntry,
          finallyEntry,
        );

        self.tryEntries.push(tryEntry);
        self.updateContextPrevLoc(tryEntry.firstLoc);

        self.leapManager.withEntry(tryEntry, () => {
          self.explodeStatement(path.get("block"));

          if (catchLoc) {
            const body = path.node.block.body;
            if (finallyLoc) {
              // If we have both a catch block and a finally block, then
              // because we emit the catch block first, we need to jump over
              // it to the finally block.
              self.jump(finallyLoc);
            } else if (
              body.length &&
              body[body.length - 1].type === "ReturnStatement"
            ) {
              after = null;
            } else {
              // If there is no finally block, then we need to jump over the
              // catch block to the fall-through location.
              self.jump(after);
            }

            self.updateContextPrevLoc(self.mark(catchLoc));

            const bodyPath = path.get("handler.body");
            const safeParam = self.makeTempVar();
            if (
              process.env.BABEL_8_BREAKING ||
              util.newHelpersAvailable(this.pluginPass)
            ) {
              this.emitAssign(safeParam, self.contextProperty("v"));
            } else {
              self.clearPendingException(tryEntry.firstLoc, safeParam);
            }

            bodyPath.traverse(catchParamVisitor, {
              getSafeParam: () => t.cloneNode(safeParam),
              catchParamName:
                // @ts-expect-error Assuming `handler.param` is `t.Identifier`
                handler.param.name,
            });