From 9b82c5b99cebdbcbeec537bd822aebd406aafdc3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 11 Oct 2014 09:29:00 +1100 Subject: [PATCH] refer to parent arguments inside arrow functions - fixes #39 --- lib/6to5/transformers/arrow-functions.js | 29 +++++++++++++++++++ .../arrow-functions/arguments/actual.js | 16 ++++++++++ .../arrow-functions/arguments/expected.js | 19 ++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 test/fixtures/arrow-functions/arguments/actual.js create mode 100644 test/fixtures/arrow-functions/arguments/expected.js diff --git a/lib/6to5/transformers/arrow-functions.js b/lib/6to5/transformers/arrow-functions.js index bf91af9915..54664bcada 100644 --- a/lib/6to5/transformers/arrow-functions.js +++ b/lib/6to5/transformers/arrow-functions.js @@ -24,3 +24,32 @@ exports.ArrowFunctionExpression = function (node) { return node; } }; + +exports.FunctionDeclaration = +exports.FunctionExpression = function (node, parent, opts, generateUid) { + var hasArguments = false; + var id; + + // traverse the function and find all arrow functions + traverse(node, function (node) { + if (node.type !== "ArrowFunctionExpression") return; + + traverse(node, function (node, parent) { + if (node.type === "Identifier" && node.name === "arguments" && + parent.type !== "MemberExpression") { + hasArguments = true; + id = id || b.identifier(generateUid("arguments")); + return id; + } + }, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]); + + return false; + }, ["FunctionDeclaration", "FunctionExpression"]); + + if (hasArguments) { + util.ensureBlock(node); + node.body.body.unshift(b.variableDeclaration("var", [ + b.variableDeclarator(id, b.identifier("arguments")) + ])); + } +}; diff --git a/test/fixtures/arrow-functions/arguments/actual.js b/test/fixtures/arrow-functions/arguments/actual.js new file mode 100644 index 0000000000..223df8f4fc --- /dev/null +++ b/test/fixtures/arrow-functions/arguments/actual.js @@ -0,0 +1,16 @@ +function outer() { + var inner = () => arguments; + return [].slice.call(inner()); +} +console.log(outer(1, 2)); + +function outer() { + var inner = () => arguments; + + var another = function () { + var inner2 = () => arguments; + }; + + return [].slice.call(inner()); +} +console.log(outer(1, 2)); diff --git a/test/fixtures/arrow-functions/arguments/expected.js b/test/fixtures/arrow-functions/arguments/expected.js new file mode 100644 index 0000000000..57f74d5478 --- /dev/null +++ b/test/fixtures/arrow-functions/arguments/expected.js @@ -0,0 +1,19 @@ +function outer() { + var _arguments = arguments; + var inner = function () { return _arguments; }; + return [].slice.call(inner()); +} +console.log(outer(1, 2)); + +function outer() { + var _arguments2 = arguments; + var inner = function () { return _arguments2; }; + + var another = function () { + var _arguments3 = arguments; + var inner2 = function () { return _arguments3; }; + }; + + return [].slice.call(inner()); +} +console.log(outer(1, 2));