Diogo Franco 452f8f150c Always use the native (or polyfilled) Promise in transform-async-to-generator (#5536)
* Always use the native (or polyfilled) Promise in transform-async-to-generator

Fixes #5531

* Simplify scope handling to only un-shadow the Program's Promise

Only the helper needs to see the native Promise.
2017-04-06 11:17:31 -04:00

26 lines
787 B
JavaScript

import remapAsyncToGenerator from "babel-helper-remap-async-to-generator";
import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions";
export default function () {
return {
inherits: syntaxAsyncFunctions,
visitor: {
Function(path, state) {
if (!path.node.async || path.node.generator) return;
// Ensure any Promise bindings at the Program level are renamed
// so the asyncToGenerator helper only sees the native Promise
const programScope = path.scope.getProgramParent();
if (programScope.hasBinding("Promise", true)) {
programScope.rename("Promise");
}
remapAsyncToGenerator(path, state.file, {
wrapAsync: state.addHelper("asyncToGenerator"),
});
},
},
};
}