Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle block-level function declaration (#10046) #11801

Merged
merged 5 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}