hardcode aliasArguments

This commit is contained in:
Sebastian McKenzie 2014-10-13 03:27:27 +11:00
parent fae60291b3
commit aa7c9488b0
2 changed files with 21 additions and 39 deletions

View File

@ -2,7 +2,7 @@ var traverse = require("../traverse");
var util = require("../util"); var util = require("../util");
var b = require("ast-types").builders; var b = require("ast-types").builders;
exports.ArrowFunctionExpression = function (node) { exports.ArrowFunctionExpression = function (node, parent, file) {
util.ensureBlock(node); util.ensureBlock(node);
node.expression = false; node.expression = false;
@ -18,16 +18,33 @@ exports.ArrowFunctionExpression = function (node) {
}; };
exports.FunctionDeclaration = exports.FunctionDeclaration =
exports.FunctionExpression = function (node, parent, opts, generateUid) { exports.FunctionExpression = function (node, parent, file) {
var argumentsId; 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) { traverse(node, function (node) {
if (node.type !== "ArrowFunctionExpression") return; if (node.type !== "ArrowFunctionExpression") return;
// traverse all child nodes of this arrow function and find a sole arguments // traverse all child nodes of this arrow function and find a sole arguments
// identifier // 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; return false;
}, ["FunctionDeclaration", "FunctionExpression"]); }, ["FunctionDeclaration", "FunctionExpression"]);

View File

@ -59,19 +59,6 @@ exports.canCompile = function (filename) {
return _.contains([".js", ".es6", ".jsx"], ext); 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) { exports.sourceMapToComment = function (map) {
var json = JSON.stringify(map); var json = JSON.stringify(map);
var base64 = new Buffer(json).toString("base64"); 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 { try {
exports.templates = require("../../templates.json"); exports.templates = require("../../templates.json");
} catch (err) { } catch (err) {