From aa7c9488b0ef5ce3420f4500b6269344b65a1dee Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 13 Oct 2014 03:27:27 +1100 Subject: [PATCH] hardcode aliasArguments --- lib/6to5/transformers/arrow-functions.js | 25 ++++++++++++++--- lib/6to5/util.js | 35 ------------------------ 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/lib/6to5/transformers/arrow-functions.js b/lib/6to5/transformers/arrow-functions.js index 8fc1443172..1ad17d27f7 100644 --- a/lib/6to5/transformers/arrow-functions.js +++ b/lib/6to5/transformers/arrow-functions.js @@ -2,7 +2,7 @@ var traverse = require("../traverse"); var util = require("../util"); var b = require("ast-types").builders; -exports.ArrowFunctionExpression = function (node) { +exports.ArrowFunctionExpression = function (node, parent, file) { util.ensureBlock(node); node.expression = false; @@ -18,16 +18,33 @@ exports.ArrowFunctionExpression = function (node) { }; exports.FunctionDeclaration = -exports.FunctionExpression = function (node, parent, opts, generateUid) { +exports.FunctionExpression = function (node, parent, file) { var argumentsId; - // traverse the function and find all arrow functions + var isArgumentIdentifier = function (node) { + return node.type === "Identifier" && node.name === "arguments"; + }; + + var getId = function () { + return argumentsId = argumentsId || b.identifier(file.generateUid("arguments")); + }; + + // traverse the function and find all arrow functions so we can alias + // arguments if neccesary traverse(node, function (node) { if (node.type !== "ArrowFunctionExpression") return; // traverse all child nodes of this arrow function and find a sole arguments // identifier - argumentsId = util.aliasArguments(generateUid, node, argumentsId); + traverse(node, function (node, parent) { + if (isArgumentIdentifier(node) && parent.type !== "MemberExpression") { + return getId(); + } else if (node.type === "MemberExpression" && isArgumentIdentifier(node.object)) { + node.object = getId(); + } else { + return; + } + }, traverse.FUNCTION_TYPES); return false; }, ["FunctionDeclaration", "FunctionExpression"]); diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 4bd471c250..702840a495 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -59,19 +59,6 @@ exports.canCompile = function (filename) { return _.contains([".js", ".es6", ".jsx"], ext); }; -exports.buildUidGenerator = function () { - var ids = {}; - - return function (name) { - var i = ids[name] || 1; - - var id = "_" + name; - if (i > 1) id += i; - ids[name] = i + 1; - return id; - }; -}; - exports.sourceMapToComment = function (map) { var json = JSON.stringify(map); var base64 = new Buffer(json).toString("base64"); @@ -212,28 +199,6 @@ exports.parse = function (opts, code, callback) { } }; -exports.aliasArguments = function (generateUid, node, id) { - var isArgumentIdentifier = function (node) { - return node.type === "Identifier" && node.name === "arguments"; - }; - - var getId = function () { - return id = id || b.identifier(generateUid("arguments")); - }; - - traverse(node, function (node, parent) { - if (isArgumentIdentifier(node) && parent.type !== "MemberExpression") { - return getId(); - } else if (node.type === "MemberExpression" && isArgumentIdentifier(node.object)) { - node.object = getId(); - } else { - return; - } - }, traverse.FUNCTION_TYPES); - - return id; -}; - try { exports.templates = require("../../templates.json"); } catch (err) {