add file alias declarations so we can alias certain long function calls - resolves #62
This commit is contained in:
parent
5f00f74aba
commit
1f61e7675b
3
lib/6to5/declarations.js
Normal file
3
lib/6to5/declarations.js
Normal file
@ -0,0 +1,3 @@
|
||||
var b = require("recast").types.builders;
|
||||
|
||||
exports.slice = b.memberExpression(b.identifier("Array"), b.memberExpression(b.identifier("prototype"), b.identifier("slice"), false), false)
|
||||
@ -2,14 +2,17 @@ module.exports = File;
|
||||
|
||||
var SHEBANG_REGEX = /^\#\!.*/;
|
||||
|
||||
var transform = require("./transform");
|
||||
var util = require("./util");
|
||||
var _ = require("lodash");
|
||||
var declarations = require("./declarations");
|
||||
var transform = require("./transform");
|
||||
var util = require("./util");
|
||||
var b = require("recast").types.builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.uids = {};
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.ast = {};
|
||||
this.declarations = {};
|
||||
this.uids = {};
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.ast = {};
|
||||
}
|
||||
|
||||
File.normaliseOptions = function (opts) {
|
||||
@ -46,6 +49,18 @@ File.prototype.parseShebang = function (code) {
|
||||
return code;
|
||||
};
|
||||
|
||||
File.prototype.addDeclaration = function (name) {
|
||||
var declar = this.declarations[name];
|
||||
if (declar) return declar.uid;
|
||||
|
||||
var uid = b.identifier(this.generateUid(name));
|
||||
this.declarations[name] = {
|
||||
uid: uid,
|
||||
node: declarations[name]
|
||||
};
|
||||
return uid;
|
||||
};
|
||||
|
||||
File.prototype.parse = function (code) {
|
||||
var self = this;
|
||||
|
||||
@ -66,6 +81,14 @@ File.prototype.transform = function (ast) {
|
||||
transformer.transform(self);
|
||||
});
|
||||
|
||||
var body = ast.program.body;
|
||||
|
||||
_.each(this.declarations, function (declar) {
|
||||
body.unshift(b.variableDeclaration("var", [
|
||||
b.variableDeclarator(declar.uid, declar.node)
|
||||
]));
|
||||
});
|
||||
|
||||
var result = util.generate(ast, opts);
|
||||
|
||||
if (this.shebang) {
|
||||
|
||||
@ -1 +1 @@
|
||||
var VARIABLE_NAME = Array.prototype.slice.call(arguments, SLICE_ARG);
|
||||
var VARIABLE_NAME = SLICE_KEY.call(arguments, SLICE_ARG);
|
||||
|
||||
@ -1 +1 @@
|
||||
var VARIABLE_NAME = Array.prototype.slice.call(arguments);
|
||||
var VARIABLE_NAME = SLICE_KEY.call(arguments);
|
||||
|
||||
@ -1 +1 @@
|
||||
Array.prototype.slice.call(arguments);
|
||||
SLICE_KEY.call(arguments);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
|
||||
exports.Function = function (node) {
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.rest) return;
|
||||
|
||||
var rest = node.rest;
|
||||
@ -12,6 +12,7 @@ exports.Function = function (node) {
|
||||
|
||||
util.ensureBlock(node);
|
||||
node.body.body.unshift(util.template(templateName, {
|
||||
SLICE_KEY: file.addDeclaration("slice"),
|
||||
VARIABLE_NAME: rest,
|
||||
SLICE_ARG: b.literal(node.params.length)
|
||||
}));
|
||||
|
||||
@ -21,7 +21,7 @@ exports.ArrayExpression = function (node) {
|
||||
return concat;
|
||||
};
|
||||
|
||||
exports.CallExpression = function (node) {
|
||||
exports.CallExpression = function (node, parent, file) {
|
||||
var args = node.arguments;
|
||||
|
||||
if (args.length && _.last(args).type === "SpreadElement") {
|
||||
@ -34,7 +34,9 @@ exports.CallExpression = function (node) {
|
||||
|
||||
if (args.length) {
|
||||
if (spreadLiteral.name === "arguments") {
|
||||
spreadLiteral = util.template("arguments-slice");
|
||||
spreadLiteral = util.template("arguments-slice", {
|
||||
SLICE_KEY: file.addDeclaration("slice")
|
||||
});
|
||||
}
|
||||
|
||||
var concat = util.template("array-concat");
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var t = function (f) {
|
||||
var items = Array.prototype.slice.call(arguments, 1);
|
||||
var items = _slice.call(arguments, 1);
|
||||
};
|
||||
|
||||
function t(f) {
|
||||
var items = Array.prototype.slice.call(arguments, 1);
|
||||
var items = _slice.call(arguments, 1);
|
||||
}
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var t = function () {
|
||||
var items = Array.prototype.slice.call(arguments);
|
||||
var items = _slice.call(arguments);
|
||||
};
|
||||
|
||||
function t() {
|
||||
var items = Array.prototype.slice.call(arguments);
|
||||
var items = _slice.call(arguments);
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
function foo() {
|
||||
return bar.apply(null, ["test"].concat(Array.prototype.slice.call(arguments)));
|
||||
return bar.apply(null, ["test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
function bar(one, two, three) {
|
||||
return [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user