Ensure helpers that reference globals continue to reference the globals properly.

This commit is contained in:
Logan Smyth
2017-05-05 01:01:35 -07:00
parent 158e9fbfd7
commit 634c750558
3 changed files with 16 additions and 8 deletions

View File

@@ -246,7 +246,13 @@ export default class File extends Store {
name,
));
const { nodes } = getHelper(name, uid, ownBindingNames);
const { nodes, globals } = getHelper(name, uid, ownBindingNames);
globals.forEach(name => {
if (this.path.scope.hasBinding(name, true /* noGlobals */)) {
this.path.scope.rename(name);
}
});
nodes.forEach(node => {
node._compact = true;

View File

@@ -18,6 +18,7 @@ function makePath(path) {
* the helper is whatever context it is needed in.
*/
function getHelperMetadata(file) {
const globals = new Set();
const localBindingNames = new Set();
let exportName;
@@ -65,6 +66,12 @@ function getHelperMetadata(file) {
localBindingNames.add(name);
});
},
ReferencedIdentifier(child) {
const name = child.node.name;
const binding = child.scope.getBinding(name);
if (!binding) globals.add(name);
},
AssignmentExpression(child) {
const left = child.get("left");
@@ -91,6 +98,7 @@ function getHelperMetadata(file) {
exportBindingAssignments.reverse();
return {
globals: Array.from(globals),
localBindingNames: Array.from(localBindingNames),
exportBindingAssignments,
exportPath,
@@ -187,6 +195,7 @@ function loadHelper(name) {
return {
nodes: file.program.body,
globals: metadata.globals,
};
};
}

View File

@@ -9,13 +9,6 @@ export default function() {
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"),
});