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

This commit is contained in:
vitorveiga 2020-12-15 17:22:15 +00:00 committed by GitHub
parent ecbbd9da48
commit 90fb8d275e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 4 deletions

View File

@ -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);
}

View File

@ -0,0 +1,13 @@
const run = function () {
return false;
};
if (true) {
function run() {
return true;
}
}
function test() {
return run();
}

View File

@ -0,0 +1,13 @@
var run = function () {
return false;
};
if (true) {
var _run = function () {
return true;
};
}
function test() {
return run();
}

View File

@ -0,0 +1,13 @@
let run = function () {
return false;
};
if (true) {
function run() {
return true;
}
}
function test() {
return run();
}

View File

@ -0,0 +1,13 @@
var run = function () {
return false;
};
if (true) {
var _run = function () {
return true;
};
}
function test() {
return run();
}

View File

@ -0,0 +1,13 @@
var run = function () {
return false;
};
if (true) {
function run() {
return true;
}
}
function test() {
return run();
}

View File

@ -0,0 +1,13 @@
var run = function () {
return false;
};
if (true) {
var run = function () {
return true;
};
}
function test() {
return run();
}

View File

@ -0,0 +1,9 @@
if (true) {
function run() {
return true;
}
}
function test() {
return run();
}

View File

@ -0,0 +1,9 @@
if (true) {
var run = function () {
return true;
};
}
function test() {
return run();
}