From 634c750558e5ce149b879b66888d5ffd41865cab Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Fri, 5 May 2017 01:01:35 -0700 Subject: [PATCH] Ensure helpers that reference globals continue to reference the globals properly. --- packages/babel-core/src/transformation/file/index.js | 8 +++++++- packages/babel-helpers/src/index.js | 9 +++++++++ .../src/index.js | 7 ------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index ecff41c24b..17815cc000 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -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; diff --git a/packages/babel-helpers/src/index.js b/packages/babel-helpers/src/index.js index dbde7a6b95..0f7e05a4ac 100644 --- a/packages/babel-helpers/src/index.js +++ b/packages/babel-helpers/src/index.js @@ -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, }; }; } diff --git a/packages/babel-plugin-transform-async-to-generator/src/index.js b/packages/babel-plugin-transform-async-to-generator/src/index.js index b562491405..8c90c12efe 100644 --- a/packages/babel-plugin-transform-async-to-generator/src/index.js +++ b/packages/babel-plugin-transform-async-to-generator/src/index.js @@ -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"), });