Skip to content

breaks are transpiled to return inside then #509

@CyriacBr

Description

@CyriacBr

Current Behavior

While working on my code I noticed a bug that led to my transpiled files and I saw that tsdx was incorrectly transforming my break inside switch cases to return.
I thoughts that was rather odd so I created a sample project with the following code to pinpoint the issue:

function wait() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
}

export const test = async () => {
  await wait();
  switch ('' as any) {
    case 'a':
      break;
    case 'b':
      break;
    default:
      break;
  }
  const foo = { bar: true };
  console.log('foo :', foo);
};

When running tsdx build, this is what I get:

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function wait() {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve();
    }, 1000);
  });
}

var test = function test() {
  try {
    return Promise.resolve(wait()).then(function () {
      switch ('') {
        case 'a':
          return;

        case 'b':
          return;

        default:
          return;
      }

      var foo = {
        bar: true
      };
      console.log('foo :', foo);
    });
  } catch (e) {
    return Promise.reject(e);
  }
};

exports.test = test;
//# sourceMappingURL=switch-test.cjs.development.js.map

As you can see, inside test, breaks inside then are transformed to returns, which is NOT correct. The console.log line would never be reached.

Expected behavior

breaks inside then should stay breaks.

Suggested solution(s)

No idea. The problem may come from babel or rollrup or one of their plugins.
Any idea what may cause the problem?

Your environment

Software Version(s)
TSDX 0.12.3
TypeScript ^3.7.5
Browser
npm/Yarn yarn 1.19.1
Node 10.16.3
Operating System Linux, but same behavior on windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugSomething isn't workingscope: upstreamIssue in upstream dependencytopic: async-to-promisesRelated to bugs with babel-plugin-async-to-promises

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions