From 5f075a9e935d1ca9c10680081904d29332db3245 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Thu, 16 Mar 2017 22:39:53 +0100 Subject: [PATCH 1/2] Fix hoisting of arguments --- packages/regenerator-transform/src/visit.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/regenerator-transform/src/visit.js b/packages/regenerator-transform/src/visit.js index d21a6ab91..cf9a802f2 100644 --- a/packages/regenerator-transform/src/visit.js +++ b/packages/regenerator-transform/src/visit.js @@ -96,8 +96,11 @@ exports.visitor = { let didRenameArguments = renameArguments(path, argsId); if (didRenameArguments) { vars = vars || t.variableDeclaration("var", []); + const argumentIdentifier = t.identifier("arguments"); + // we need to do this as otherwise arguments in arrow functions gets hoisted + argumentIdentifier._shadowedFunctionLiteral = path; vars.declarations.push(t.variableDeclarator( - argsId, t.identifier("arguments") + argsId, argumentIdentifier )); } From c3c2823476c9966a9d43950959927e19dbce1a65 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sat, 18 Mar 2017 10:43:25 +0100 Subject: [PATCH 2/2] Add test --- package.json | 2 ++ test/regression.js | 25 +++++++++++++++++++++++++ test/run.js | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 test/regression.js diff --git a/package.json b/package.json index 35086aa25..d0e4b89a0 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,8 @@ "through": "^2.3.8" }, "devDependencies": { + "babel-plugin-transform-es2015-parameters": "^6.23.0", + "babel-plugin-transform-es2015-spread": "^6.22.0", "babylon": "^6.14.1", "mocha": "^2.3.4", "promise": "^7.0.4", diff --git a/test/regression.js b/test/regression.js new file mode 100644 index 000000000..ac5ecba83 --- /dev/null +++ b/test/regression.js @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2017, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * https://raw.github.com/facebook/regenerator/master/LICENSE file. An + * additional grant of patent rights can be found in the PATENTS file in + * the same directory. + */ + +var assert = require("assert"); + +describe("regressions", function() { + it("should correctly hoist arguments", async function () { + function test(fn) { + return async (...args) => { + return fn(...args); + }; + } + const result = []; + await test((arg1, arg2) => { result.push(arg1, arg2); })(1, "foo"); + + assert.deepEqual(result, [1, "foo"]); + }); +}); diff --git a/test/run.js b/test/run.js index e225f9e29..40c55b50c 100644 --- a/test/run.js +++ b/test/run.js @@ -128,6 +128,31 @@ enqueue(convert, [ "./test/async.es5.js" ]); +function convertWithSpread(es6File, es5File, callback) { + var transformOptions = { + presets:[require("regenerator-preset")], + plugins: [ + require("babel-plugin-transform-es2015-spread"), + require("babel-plugin-transform-es2015-parameters") + ] + }; + + fs.readFile(es6File, "utf-8", function(err, es6) { + if (err) { + return callback(err); + } + + var es5 = require("babel-core").transform(es6, transformOptions).code; + + fs.writeFile(es5File, es5, callback); + }); +} + +enqueue(convertWithSpread, [ + "./test/regression.js", + "./test/regression.es5.js" +]); + enqueue(makeMochaCopyFunction("mocha.js")); enqueue(makeMochaCopyFunction("mocha.css")); @@ -137,11 +162,14 @@ if (!semver.eq(process.version, "0.11.7")) { try { require.resolve("browserify"); // Throws if missing. enqueue(bundle, [ - ["./test/runtime.js", - "./test/tests.es5.js", - "./test/tests-node4.es5.js", - "./test/non-native.es5.js", - "./test/async.es5.js"], + [ + "./test/runtime.js", + "./test/tests.es5.js", + "./test/tests-node4.es5.js", + "./test/non-native.es5.js", + "./test/async.es5.js", + "./test/regression.es5.js" + ], "./test/tests.browser.js" ]); } catch (ignored) { @@ -156,6 +184,7 @@ enqueue("mocha", [ "./test/tests-node4.es5.js", "./test/non-native.es5.js", "./test/async.es5.js", + "./test/regression.es5.js", "./test/tests.transform.js" ]);