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:
Daniel Tschinder
2016-10-01 19:24:12 +02:00
committed by Henry Zhu
parent f9ccee9d83
commit 5a8070a397
4 changed files with 36 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,10 @@
function wrapper(fn) {
return (...args) => {
if (someCondition) {
const val = fn(...args);
return val.test(() => {
console.log(val);
});
}
};
}

View 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;
}
};
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["transform-es2015-parameters", "transform-es2015-arrow-functions", "transform-es2015-block-scoping"]
}