Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect async transform #388

Closed
breezewish opened this issue Sep 16, 2020 · 1 comment · Fixed by #390
Closed

Incorrect async transform #388

breezewish opened this issue Sep 16, 2020 · 1 comment · Fixed by #390

Comments

@breezewish
Copy link

breezewish commented Sep 16, 2020

// esbuild --bundle index.ts --outfile=out.js --target=es2015

const myFunc = function (c) {
  return {
    foo: async (message, options = {}) => {
      if (message.a !== "1") {
        throw new Error("foo");
      }
      console.log("Pass!");
    },
  };
};

myFunc({}).foo({ a: "1" });

The snippet above will throw an error instead of printing "Pass" after esbuild transformation.

The transformed result is:

(() => {
  var __async = (__this, __arguments, generator) => {
    return new Promise((resolve, reject) => {
      var fulfilled = (value) => {
        try {
          step(generator.next(value));
        } catch (e) {
          reject(e);
        }
      };
      var rejected = (value) => {
        try {
          step(generator.throw(value));
        } catch (e) {
          reject(e);
        }
      };
      var step = (result) => {
        return result.done ? resolve(result.value) : Promise.resolve(result.value).then(fulfilled, rejected);
      };
      step((generator = generator.apply(__this, __arguments)).next());
    });
  };

  // src/index.ts
  const myFunc = function(c) {
    return {
      foo: (_0) => __async(this, arguments, function* (message, options = {}) {
        //                       ^^^^^^^^^
        // Notice that `arguments` here is incorrect, since arrow function does
        // not provide arguments.
        if (message.a !== "1") {
          throw new Error("foo");
        }
        console.log("Pass!");
      })
    };
  };
  myFunc({}).foo({a: "1"});
})();
@rtsao
Copy link
Contributor

rtsao commented Sep 16, 2020

It looks like this regression was introduced in v0.6.32 via d121b38

Which makes sense because removing options = {} from your example fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants