Forward bound shadowed function when hoisting identifiers (#4635)
This change ensures that when hoisting an identifier we do not hoist it up to the first no shadowed function, but rather up to the bound shadowed function
This commit is contained in:
committed by
Henry Zhu
parent
f9ccee9d83
commit
5a8070a397
@@ -17,7 +17,7 @@ const superVisitor = {
|
||||
|
||||
export default new Plugin({
|
||||
name: "internal.shadowFunctions",
|
||||
|
||||
|
||||
visitor: {
|
||||
ThisExpression(path) {
|
||||
remap(path, "this");
|
||||
@@ -104,6 +104,10 @@ function remap(path, key) {
|
||||
} else {
|
||||
const init = key === "this" ? t.thisExpression() : t.identifier(key);
|
||||
|
||||
// Forward the shadowed function, so that the identifiers do not get hoisted
|
||||
// up to the first non shadow function but rather up to the bound shadow function
|
||||
if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction;
|
||||
|
||||
fnPath.scope.push({ id, init });
|
||||
}
|
||||
|
||||
|
||||
10
packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js
vendored
Normal file
10
packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
function wrapper(fn) {
|
||||
return (...args) => {
|
||||
if (someCondition) {
|
||||
const val = fn(...args);
|
||||
return val.test(() => {
|
||||
console.log(val);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
18
packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js
vendored
Normal file
18
packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
function wrapper(fn) {
|
||||
return function () {
|
||||
var _arguments = arguments;
|
||||
|
||||
if (someCondition) {
|
||||
var _ret = function () {
|
||||
var val = fn(..._arguments);
|
||||
return {
|
||||
v: val.test(function () {
|
||||
console.log(val);
|
||||
})
|
||||
};
|
||||
}();
|
||||
|
||||
if (typeof _ret === "object") return _ret.v;
|
||||
}
|
||||
};
|
||||
}
|
||||
3
packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json
vendored
Normal file
3
packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-es2015-parameters", "transform-es2015-arrow-functions", "transform-es2015-block-scoping"]
|
||||
}
|
||||
Reference in New Issue
Block a user