From a9d101e3da07ba2d8ff94c5deb7d5322de3cd91f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 14 Oct 2014 08:07:03 +1100 Subject: [PATCH] support nested arrow functions - fixes #60 --- lib/6to5/transformers/_alias-functions.js | 8 +++++++- .../syntax/arrow-functions/nested/actual.js | 13 +++++++++++++ .../syntax/arrow-functions/nested/expected.js | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/syntax/arrow-functions/nested/actual.js create mode 100644 test/fixtures/syntax/arrow-functions/nested/expected.js diff --git a/lib/6to5/transformers/_alias-functions.js b/lib/6to5/transformers/_alias-functions.js index 16c251b02e..a00e4ce992 100644 --- a/lib/6to5/transformers/_alias-functions.js +++ b/lib/6to5/transformers/_alias-functions.js @@ -29,6 +29,12 @@ var go = function (getBody, node, file) { // traverse all child nodes of this function and find arguments and this traverse(node, function (node, parent) { + if (_aliasFunction === "arrows") { + if (traverse.isFunction(node) && node._aliasFunction !== "arrows") { + return false; + } + } + var getId; if (node.type === "Identifier" && node.name === "arguments") { @@ -40,7 +46,7 @@ var go = function (getBody, node, file) { } if (util.isReferenced(node, parent)) return getId(); - }, _aliasFunction === "arrows" && ["FunctionExpression", "FunctionDeclaration"]); + }); return false; }); diff --git a/test/fixtures/syntax/arrow-functions/nested/actual.js b/test/fixtures/syntax/arrow-functions/nested/actual.js new file mode 100644 index 0000000000..9387606df0 --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/nested/actual.js @@ -0,0 +1,13 @@ +module.exports = { + init() { + return new Promise((resolve, reject) => { + MongoClient.connect(config.mongodb, (err, db) => { + if (err) { + return reject(err); + } + this.db = db; + resolve(this); + }); + }); + } +}; diff --git a/test/fixtures/syntax/arrow-functions/nested/expected.js b/test/fixtures/syntax/arrow-functions/nested/expected.js new file mode 100644 index 0000000000..4c579ac3f0 --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/nested/expected.js @@ -0,0 +1,14 @@ +module.exports = { + init: function() { + var _this = this; + return new Promise(function(resolve, reject) { + MongoClient.connect(config.mongodb, function(err, db) { + if (err) { + return reject(err); + } + _this.db = db; + resolve(_this); + }); + }); + } +};