Skip to content

Commit

Permalink
Merge pull request #283 from danez/fix-async-arrow-rest
Browse files Browse the repository at this point in the history
Fix hoisting of arguments
  • Loading branch information
benjamn committed Mar 18, 2017
2 parents 50b722e + c3c2823 commit 2d9cf46
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion packages/regenerator-transform/src/visit.js
Expand Up @@ -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
));
}

Expand Down
25 changes: 25 additions & 0 deletions 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"]);
});
});
39 changes: 34 additions & 5 deletions test/run.js
Expand Up @@ -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"));

Expand All @@ -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) {
Expand All @@ -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"
]);

Expand Down

0 comments on commit 2d9cf46

Please sign in to comment.