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" + ] +}