summaryrefslogtreecommitdiff
path: root/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js')
-rw-r--r--node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js b/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js
new file mode 100644
index 00000000..2945315d
--- /dev/null
+++ b/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js
@@ -0,0 +1,50 @@
+"use strict";
+
+// this fixes a bug where converting let to var
+// doesn't change the binding's scope to function scope
+// https://github.com/babel/babel/issues/4818
+module.exports = function (mangler) {
+ mangler.program.traverse({
+ VariableDeclaration(path) {
+ if (path.node.kind !== "var") {
+ return;
+ }
+
+ const fnScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
+ const bindingIds = path.getOuterBindingIdentifierPaths();
+
+ for (const name in bindingIds) {
+ const binding = path.scope.getBinding(name); // var isn't hoisted to fnScope
+
+ if (binding.scope !== fnScope) {
+ const existingBinding = fnScope.bindings[name]; // make sure we are clear that the fnScope doesn't already have
+ // an existing binding
+
+ if (!existingBinding) {
+ // move binding to the function scope
+ // update our scopeTracker first before
+ // we mutate the scope
+ mangler.scopeTracker.moveBinding(binding, fnScope);
+ fnScope.bindings[name] = binding;
+ binding.scope = fnScope;
+ delete binding.scope.bindings[name];
+ } else {
+ // we need a new binding that's valid in both the scopes
+ // binding.scope and fnScope
+ const newName = fnScope.generateUid(binding.scope.generateUid(name)); // rename binding in the original scope
+
+ mangler.rename(binding.scope, binding, name, newName); // move binding to fnScope as newName
+ // update our scopeTracker first before
+ // we mutate the scope
+
+ mangler.scopeTracker.moveBinding(binding, fnScope);
+ fnScope.bindings[newName] = binding;
+ binding.scope = fnScope;
+ delete binding.scope.bindings[newName];
+ }
+ }
+ }
+ }
+
+ });
+}; \ No newline at end of file