diff --git a/packages/babel-plugin-transform-block-scoping/src/index.js b/packages/babel-plugin-transform-block-scoping/src/index.js index 3a9576a525..cf2b8f550c 100644 --- a/packages/babel-plugin-transform-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-block-scoping/src/index.js @@ -481,10 +481,18 @@ class BlockScoping { // todo: could skip this if the colliding binding is in another function if (scope.parentHasBinding(key) || scope.hasGlobal(key)) { - // The same identifier might have been bound separately in the block scope and - // the enclosing scope (e.g. loop or catch statement), so we should handle both - // individually - if (scope.hasOwnBinding(key)) { + const binding = scope.getOwnBinding(key); + if (binding) { + const parentBinding = scope.parent.getOwnBinding(key); + if ( + binding.kind === "hoisted" && + (!parentBinding || isVar(parentBinding.path.parent)) + ) { + continue; + } + // The same identifier might have been bound separately in the block scope and + // the enclosing scope (e.g. loop or catch statement), so we should handle both + // individually scope.rename(ref.name); } diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/input.js new file mode 100644 index 0000000000..ee50d75b66 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/input.js @@ -0,0 +1,13 @@ +const run = function () { + return false; +}; + +if (true) { + function run() { + return true; + } +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/output.js new file mode 100644 index 0000000000..45ef23d2be --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-const/output.js @@ -0,0 +1,13 @@ +var run = function () { + return false; +}; + +if (true) { + var _run = function () { + return true; + }; +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/input.js new file mode 100644 index 0000000000..6004c1b474 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/input.js @@ -0,0 +1,13 @@ +let run = function () { + return false; +}; + +if (true) { + function run() { + return true; + } +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/output.js new file mode 100644 index 0000000000..45ef23d2be --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-let/output.js @@ -0,0 +1,13 @@ +var run = function () { + return false; +}; + +if (true) { + var _run = function () { + return true; + }; +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/input.js new file mode 100644 index 0000000000..09899d30db --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/input.js @@ -0,0 +1,13 @@ +var run = function () { + return false; +}; + +if (true) { + function run() { + return true; + } +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/output.js new file mode 100644 index 0000000000..8e995197bd --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046-collision-var/output.js @@ -0,0 +1,13 @@ +var run = function () { + return false; +}; + +if (true) { + var run = function () { + return true; + }; +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/input.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/input.js new file mode 100644 index 0000000000..19d0ca04b0 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/input.js @@ -0,0 +1,9 @@ +if (true) { + function run() { + return true; + } +} + +function test() { + return run(); +} diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/output.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/output.js new file mode 100644 index 0000000000..80fd2e37a9 --- /dev/null +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/output.js @@ -0,0 +1,9 @@ +if (true) { + var run = function () { + return true; + }; +} + +function test() { + return run(); +}