diff --git a/src/babel/traversal/path/hoister.js b/src/babel/traversal/path/hoister.js index e47c971051..d4477bd1b2 100644 --- a/src/babel/traversal/path/hoister.js +++ b/src/babel/traversal/path/hoister.js @@ -12,6 +12,10 @@ var referenceVisitor = { if (this.isReferenced()) { var bindingInfo = scope.getBinding(node.name); + // this binding isn't accessible from the parent scope so we can safely ignore it + // eg. it's in a closure etc + if (bindingInfo !== state.scope.getBinding(node.name)) return; + if (bindingInfo && bindingInfo.constant) { state.bindings[node.name] = bindingInfo; } else { @@ -25,9 +29,10 @@ var referenceVisitor = { }; export default class PathHoister { - constructor(path) { + constructor(path, scope) { this.foundIncompatible = false; this.bindings = {}; + this.scope = scope; this.scopes = []; this.path = path; } diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 6ff0b903bf..31be117438 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -644,8 +644,8 @@ export default class TraversalPath { * Description */ - hoist() { - var hoister = new PathHoister(this); + hoist(scope = this.scope) { + var hoister = new PathHoister(this, scope); return hoister.run(); } diff --git a/src/babel/traversal/scope.js b/src/babel/traversal/scope.js index 0f09ddc0a0..b71bf0cd33 100644 --- a/src/babel/traversal/scope.js +++ b/src/babel/traversal/scope.js @@ -75,6 +75,13 @@ export default class Scope { return parent; } + var cached = path.getData("scope"); + if (cached && cached.parent === parent) { + return cached; + } else { + path.setData("scope", this); + } + this.parent = parent; this.file = parent ? parent.file : file;