From 8b4b02a5fb0e90b65db9c602c52a23a7d19466ef Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 9 Mar 2016 13:24:20 -0800 Subject: [PATCH] [hotfix T7197] Use scope.moveBindingTo I had deleted the binding and created a new one. I naively thought that the analysis will automatically run again. But now discovered the method I actually want to use: `scope.moveBindingTo` which moves the binding and all the correct analysis. The only thing that was left to do is to update `binding.kind` which I did manually. --- .../src/index.js | 6 +++--- .../test/fixtures/general/function-name/actual.js | 5 +++++ .../test/fixtures/general/function-name/expected.js | 5 +++++ .../test/fixtures/general/function-name/options.json | 7 +++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/actual.js create mode 100644 packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/expected.js create mode 100644 packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/options.json diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index 727a66e3b1..de3c63e699 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -90,9 +90,10 @@ function convertBlockScopedToVar(path, parent, scope, moveBindingsToParent = fal const parentScope = scope.getFunctionParent(); const ids = path.getBindingIdentifiers(); for (let name in ids) { - scope.removeOwnBinding(name); + let binding = scope.getOwnBinding(name); + if (binding) binding.kind = "var"; + scope.moveBindingTo(name, parentScope); } - parentScope.registerBinding("var", path); } } @@ -351,7 +352,6 @@ class BlockScoping { const binding = scope.getBinding(ref.name); if (!binding) continue; if (binding.kind === "let" || binding.kind === "const") { - scope.removeOwnBinding(ref.name); parentScope.registerBinding("var", binding.path); } } diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/actual.js new file mode 100644 index 0000000000..85d943cd3b --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/actual.js @@ -0,0 +1,5 @@ +let foo = () => { + foo = () => { }; +}; + +foo(); diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/expected.js new file mode 100644 index 0000000000..3cd5698f0f --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/expected.js @@ -0,0 +1,5 @@ +var _foo = function foo() { + _foo = function foo() {}; +}; + +_foo(); diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/options.json b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/options.json new file mode 100644 index 0000000000..38472510f3 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/function-name/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "transform-es2015-arrow-functions", + "transform-es2015-function-name", + "transform-es2015-block-scoping" + ] +}