rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

unexpected output with externalHelpers: true #81

Open anchengjian opened 2 years ago

anchengjian commented 2 years ago

Input:

async function AAA () {}
async function BBB () {}

babel.config.js

module.exports = function (api) {
  api.cache(false)
  return {
    presets: ['@babel/preset-env'],
  }
}

Test Code:

const code = `async function AAA () {}
async function BBB () {}`
const babelcore = require("@babel/core");
const res = babelcore.transformSync(code, {
    filename: '/a/b/c',
    plugins: [
      [ "babel-plugin-transform-async-to-promises", { externalHelpers: true } ],
    ],
});
console.log(res.code)

Output:

"use strict";
var _helpers = require("babel-plugin-transform-async-to-promises/helpers");
var BBB = function BBB() {
  return _await();  // <--------- There is no _helpers here
};
var AAA = function AAA() {
  return (0, _helpers._await)();
};
anchengjian commented 2 years ago

And, there is also a problem with the order of function declarations

dmail commented 2 years ago

The output is correct without @babel/preset-env (I guess the issue is caused by the transformation to commonjs).