From 8b4b02a5fb0e90b65db9c602c52a23a7d19466ef Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 9 Mar 2016 13:24:20 -0800 Subject: [PATCH 1/3] [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" + ] +} From 3dd80a6b14179b3adb4d170419f3e5347e209df0 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 9 Mar 2016 13:28:45 -0800 Subject: [PATCH 2/3] Update updateScopeInfo method to use moveBindingTo --- .../babel-plugin-transform-es2015-block-scoping/src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 de3c63e699..67bd13567f 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -352,7 +352,8 @@ class BlockScoping { const binding = scope.getBinding(ref.name); if (!binding) continue; if (binding.kind === "let" || binding.kind === "const") { - parentScope.registerBinding("var", binding.path); + binding.kind = 'var'; + scope.moveBindingTo(ref.name, parentScope); } } } From 07d2c15e99c16c737749096baf859e75f04a3d4b Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 9 Mar 2016 13:34:06 -0800 Subject: [PATCH 3/3] quotes --- .../babel-plugin-transform-es2015-block-scoping/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 67bd13567f..d41e2dd544 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -352,7 +352,7 @@ class BlockScoping { const binding = scope.getBinding(ref.name); if (!binding) continue; if (binding.kind === "let" || binding.kind === "const") { - binding.kind = 'var'; + binding.kind = "var"; scope.moveBindingTo(ref.name, parentScope); } }