Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hoisting of arguments #283

Merged
merged 2 commits into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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