fix: handle block-level function declaration (#11801)
This commit is contained in:
parent
ecbbd9da48
commit
90fb8d275e
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
const run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
function run() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
var run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
var _run = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
let run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
function run() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
var run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
var _run = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
var run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
function run() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
var run = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
if (true) {
|
||||
var run = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
9
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/input.js
vendored
Normal file
9
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/input.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
if (true) {
|
||||
function run() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
9
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/output.js
vendored
Normal file
9
packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10046/output.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
if (true) {
|
||||
var run = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function test() {
|
||||
return run();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user