From 5a8070a39701d843e4fe347f2ba2eeb986a1458a Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sat, 1 Oct 2016 19:24:12 +0200 Subject: [PATCH] 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 --- .../internal-plugins/shadow-functions.js | 6 +++++- .../misc/regression-2364/actual.js | 10 ++++++++++ .../misc/regression-2364/expected.js | 18 ++++++++++++++++++ .../misc/regression-2364/options.json | 3 +++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js create mode 100644 packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js create mode 100644 packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json diff --git a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js index 818ee5cf0f..4eb08783f2 100644 --- a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js +++ b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js @@ -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 }); } diff --git a/packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js new file mode 100644 index 0000000000..e995e0d234 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js @@ -0,0 +1,10 @@ +function wrapper(fn) { + return (...args) => { + if (someCondition) { + const val = fn(...args); + return val.test(() => { + console.log(val); + }); + } + }; +} diff --git a/packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js new file mode 100644 index 0000000000..ae32dbb4d3 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/expected.js @@ -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; + } + }; +} \ No newline at end of file diff --git a/packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json new file mode 100644 index 0000000000..822601997a --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/misc/regression-2364/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-es2015-parameters", "transform-es2015-arrow-functions", "transform-es2015-block-scoping"] +}