Skip to content

Commit

Permalink
fix: handle block-level function declaration (#11801)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorveiga committed Dec 15, 2020
1 parent ecbbd9d commit 90fb8d2
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/babel-plugin-transform-block-scoping/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const run = function () {
return false;
};

if (true) {
function run() {
return true;
}
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var run = function () {
return false;
};

if (true) {
var _run = function () {
return true;
};
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let run = function () {
return false;
};

if (true) {
function run() {
return true;
}
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var run = function () {
return false;
};

if (true) {
var _run = function () {
return true;
};
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var run = function () {
return false;
};

if (true) {
function run() {
return true;
}
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var run = function () {
return false;
};

if (true) {
var run = function () {
return true;
};
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (true) {
function run() {
return true;
}
}

function test() {
return run();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if (true) {
var run = function () {
return true;
};
}

function test() {
return run();
}

0 comments on commit 90fb8d2

Please sign in to comment.