From de21f2ef77e293ee3d2ab1dac1a5027d0ff68494 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sun, 6 Mar 2016 17:54:47 -0800 Subject: [PATCH] Resolve 'arguments' for rest args relative to direct parent. --- .../internal-plugins/shadow-functions.js | 8 +++++ .../deeply-nested-asyncs/actual.js | 30 ++++++++++--------- .../deeply-nested-asyncs/expected.js | 13 +++++++- .../fixtures/async-to-generator/options.json | 6 +++- packages/babel-traverse/src/path/ancestry.js | 6 +++- 5 files changed, 46 insertions(+), 17 deletions(-) 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 ccd753cc55..5a5681d67c 100644 --- a/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js +++ b/packages/babel-core/src/transformation/internal-plugins/shadow-functions.js @@ -29,6 +29,7 @@ function remap(path, key, create) { if (!shouldShadow(path, shadowPath)) return; let shadowFunction = path.node._shadowedFunctionLiteral; + let currentFunction; let passedShadowFunction = false; @@ -56,6 +57,13 @@ function remap(path, key, create) { return false; }); + if (shadowFunction && fnPath.isProgram() && !shadowFunction.isProgram()){ + // If the shadow wasn't found, take the closest function as a backup. + // This is a bit of a hack, but it will allow the parameter transforms to work properly + // without introducing yet another shadow-controlling flag. + fnPath = path.findParent((p) => p.isProgram() || p.isFunction()); + } + // no point in realiasing if we're in this function if (fnPath === currentFunction) return; diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/actual.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/actual.js index 7d51e3957f..54b9fa5000 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/actual.js +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/actual.js @@ -1,14 +1,16 @@ -async function s(x) { - let t = async (y, a) => { - let r = async (z, b) => { - await z; - return this.x; - } - await r(); - - return this.g(r); - } - - await t(); - return this.h(t); -} +async function s(x, ...args) { + let t = async (y, a) => { + let r = async (z, b, ...innerArgs) => { + await z; + console.log(this, innerArgs, arguments); + return this.x; + } + await r(); + + console.log(this, args, arguments); + return this.g(r); + } + + await t(); + return this.h(t); +} diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/expected.js b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/expected.js index 691a0b30d1..94c5622431 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/expected.js +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/deeply-nested-asyncs/expected.js @@ -1,12 +1,22 @@ let s = (() => { var ref = babelHelpers.asyncToGenerator(function* (x) { - var _this = this; + var _this = this, + _arguments = arguments; + + for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + args[_key - 1] = arguments[_key]; + } let t = (() => { var ref = babelHelpers.asyncToGenerator(function* (y, a) { let r = (() => { var ref = babelHelpers.asyncToGenerator(function* (z, b) { + for (var _len2 = arguments.length, innerArgs = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { + innerArgs[_key2 - 2] = arguments[_key2]; + } + yield z; + console.log(_this, innerArgs, _arguments); return _this.x; }); return function r(_x4, _x5) { @@ -15,6 +25,7 @@ let s = (() => { })(); yield r(); + console.log(_this, args, _arguments); return _this.g(r); }); return function t(_x2, _x3) { diff --git a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/options.json b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/options.json index 54b64ad0fb..78b845844f 100644 --- a/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/options.json +++ b/packages/babel-plugin-transform-async-to-generator/test/fixtures/async-to-generator/options.json @@ -1,3 +1,7 @@ { - "plugins": ["external-helpers", "transform-async-to-generator"] + "plugins": [ + "external-helpers", + "transform-es2015-parameters", + "transform-async-to-generator" + ] } diff --git a/packages/babel-traverse/src/path/ancestry.js b/packages/babel-traverse/src/path/ancestry.js index f98934c6b7..e7c3217093 100644 --- a/packages/babel-traverse/src/path/ancestry.js +++ b/packages/babel-traverse/src/path/ancestry.js @@ -213,7 +213,11 @@ export function inType() { * - _forceShadow - If truthy, this specific identifier will be bound in the closest * Function that is not flagged "shadow", or the Program. * - _shadowedFunctionLiteral - When set to a NodePath, this specific identifier will be bound - * to this NodePath/Node or the Program. + * to this NodePath/Node or the Program. If this path is not found relative to the + * starting location path, the closest function will be used. + * + * Please Note, these flags are for private internal use only and should be avoided. + * Only "shadow" is a public property that other transforms may manipulate. */ export function inShadow(key?) {