1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-05 13:15:46 +01:00

Replace Array.slice conversions of the arguments object with rest parameters.

This commit is contained in:
Doug Kearns
2013-09-05 20:46:21 +10:00
parent e0a0388382
commit 99bfcbf2fc
16 changed files with 82 additions and 93 deletions

View File

@@ -15,8 +15,7 @@ defineModule("addons", {
this.lazyRequire("completion", ["completion"]);
lazyRequire("template", ["template"]);
var callResult = function callResult(method) {
let args = Array.slice(arguments, 1);
var callResult = function callResult(method, ...args) {
return function (result) { result[method].apply(result, args); };
}

View File

@@ -68,8 +68,8 @@ Object.defineProperty(defineModule.loadLog, "push", {
}
});
defineModule.prefix = "";
defineModule.dump = function dump_() {
let msg = Array.map(arguments, function (msg) {
defineModule.dump = function dump_(...args) {
let msg = args.map(function (msg) {
if (loaded.util && typeof msg == "object")
msg = util.objectToString(msg);
return msg;
@@ -78,13 +78,13 @@ defineModule.dump = function dump_() {
.replace(/^./gm, JSMLoader.name + ": $&"));
}
defineModule.modules = [];
defineModule.time = function time(major, minor, func, self) {
defineModule.time = function time(major, minor, func, self, ...args) {
let time = Date.now();
if (typeof func !== "function")
func = self[func];
try {
var res = func.apply(self, Array.slice(arguments, 4));
var res = func.apply(self, args);
}
catch (e) {
loaded.util && util.reportError(e);
@@ -441,18 +441,18 @@ function curry(fn, length, self, acc) {
return fn;
// Close over function with 'this'
function close(self, fn) function () fn.apply(self, Array.slice(arguments));
function close(self, fn) function () fn.apply(self, arguments);
if (acc == null)
acc = [];
return function curried() {
let args = acc.concat(Array.slice(arguments));
return function curried(...args) {
// The curried result should preserve 'this'
if (arguments.length == 0)
if (args.length == 0)
return close(self || this, curried);
let args = acc.concat(args);
if (args.length >= length)
return fn.apply(self || this, args);
@@ -461,15 +461,14 @@ function curry(fn, length, self, acc) {
}
if (curry.bind)
var bind = function bind(meth, self) let (func = callable(meth) ? meth : self[meth])
func.bind.apply(func, Array.slice(arguments, 1));
var bind = function bind(meth, self, ...args) let (func = callable(meth) ? meth : self[meth])
func.bind.apply(func, [self].concat(args));
else
var bind = function bind(func, self) {
var bind = function bind(func, self, ...args) {
if (!callable(func))
func = self[func];
let args = Array.slice(arguments, bind.length);
return function bound() func.apply(self, args.concat(Array.slice(arguments)));
return function bound(...args2) func.apply(self, args.concat(args2));
};
/**
@@ -574,8 +573,8 @@ function isString(val) objproto.toString.call(val) == "[object String]";
*/
function callable(val) typeof val === "function" && !(val instanceof Ci.nsIDOMElement);
function call(fn) {
fn.apply(arguments[1], Array.slice(arguments, 2));
function call(fn, self, ...args) {
fn.apply(self, args);
return fn;
}
@@ -660,8 +659,8 @@ function update(target) {
func.superapply = function superapply(self, args)
let (meth = Object.getPrototypeOf(target)[k])
meth && meth.apply(self, args);
func.supercall = function supercall(self)
func.superapply(self, Array.slice(arguments, 1));
func.supercall = function supercall(self, ...args)
func.superapply(self, args);
}
}
Object.defineProperty(target, k, desc);
@@ -687,8 +686,8 @@ function update_(target) {
func.superapply = function super_apply(self, args)
let (meth = Object.getPrototypeOf(target)[k])
meth && meth.apply(self, args);
func.supercall = function super_call(self)
func.superapply(self, Array.slice(arguments, 1));
func.supercall = function super_call(self, ...args)
func.superapply(self, args);
}
}
Object.defineProperty(target, k, desc);
@@ -722,9 +721,8 @@ function update_(target) {
*
* @returns {function} The constructor for the resulting class.
*/
function Class() {
function Class(...args) {
var args = Array.slice(arguments);
if (isString(args[0]))
var name = args.shift();
var superclass = Class;
@@ -991,8 +989,8 @@ Class.prototype = {
return meth && meth.apply(self, args);
};
func.supercall = function supercall(self) {
return func.superapply(self, Array.slice(arguments, 1));
func.supercall = function supercall(self, ...args) {
return func.superapply(self, args);
}
}
}
@@ -1129,22 +1127,22 @@ var Finished = Class("Finished", ErrorBase);
* @param {Object} classProperties Properties to be applied to the class constructor.
* @returns {Class}
*/
function Module(name, prototype) {
function Module(name, prototype, ...args) {
try {
let init = callable(prototype) ? 4 : 3;
let proto = arguments[callable(prototype) ? 2 : 1];
let init = callable(prototype) ? 2 : 1;
let proto = callable(prototype) ? args[0] : prototype;
proto._metaInit_ = function () {
delete module.prototype._metaInit_;
currentModule[name.toLowerCase()] = this;
};
const module = Class.apply(Class, Array.slice(arguments, 0, init));
const module = Class.apply(Class, [name, prototype, ...args.slice(0, init)]);
let instance = module();
module.className = name.toLowerCase();
instance.INIT = update(Object.create(Module.INIT),
arguments[init] || {});
args[init] || {});
currentModule[module.className] = instance;
defineModule.modules.push(instance);
@@ -1201,13 +1199,9 @@ Module.INIT = {
*
* @returns {function} The constructor for the new Struct.
*/
function Struct() {
if (!/^[A-Z]/.test(arguments[0]))
var args = Array.slice(arguments, 0);
else {
var className = arguments[0];
args = Array.slice(arguments, 1);
}
function Struct(...args) {
if (/^[A-Z]/.test(args[0]))
var className = args.shift();
const Struct = Class(className || "Struct", StructBase, {
length: args.length,
@@ -1601,9 +1595,9 @@ var array = Class("array", Array, {
},
array: ary,
toString: function () this.array.toString(),
concat: function () this.__noSuchMethod__("concat", Array.slice(arguments)),
filter: function () this.__noSuchMethod__("filter", Array.slice(arguments)),
map: function () this.__noSuchMethod__("map", Array.slice(arguments))
concat: function (...args) this.__noSuchMethod__("concat", args),
filter: function (...args) this.__noSuchMethod__("filter", args),
map: function (...args) this.__noSuchMethod__("map", args)
};
}
}, {
@@ -1736,8 +1730,8 @@ var array = Class("array", Array, {
/* Make Minefield not explode, because Minefield exploding is not fun. */
let iterProto = Iter.prototype;
Object.keys(iter).forEach(function (k) {
iterProto[k] = function () {
let res = iter[k].apply(iter, [this].concat(Array.slice(arguments)));
iterProto[k] = function (...args) {
let res = iter[k].apply(iter, [this].concat(args));
if (isinstance(res, ["Iterator", "Generator"]))
return Iter(res);
return res;
@@ -1746,8 +1740,8 @@ Object.keys(iter).forEach(function (k) {
Object.keys(array).forEach(function (k) {
if (!(k in iterProto))
iterProto[k] = function () {
let res = array[k].apply(array, [this.toArray()].concat(Array.slice(arguments)));
iterProto[k] = function (...args) {
let res = array[k].apply(array, [this.toArray()].concat(args));
if (isinstance(res, ["Iterator", "Generator"]))
return Iter(res);
if (isArray(res))

View File

@@ -1571,7 +1571,7 @@ var Commands = Module("commands", {
return dactyl.userEval(completer);
});
if (callable(result))
return result.apply(this, Array.slice(arguments));
return result.apply(this, arguments);
else
return context.completions = result;
};

View File

@@ -705,8 +705,8 @@ var CompletionContext = Class("CompletionContext", {
* for the new context. If a string is provided, it is
* interpreted as a method to access on *self*.
*/
fork: function fork(name, offset, self, completer) {
return this.forkapply(name, offset, self, completer, Array.slice(arguments, fork.length));
fork: function fork(name, offset, self, completer, ...args) {
return this.forkapply(name, offset, self, completer, args);
},
forkapply: function forkapply(name, offset, self, completer, args) {
@@ -731,7 +731,7 @@ var CompletionContext = Class("CompletionContext", {
return context;
},
split: function split(name, obj, fn) {
split: function split(name, obj, fn, ...args) {
let context = this.fork(name);
let alias = (prop) => {
context.__defineGetter__(prop, () => this[prop]);
@@ -746,7 +746,7 @@ var CompletionContext = Class("CompletionContext", {
context.hasItems = true;
this.hasItems = false;
if (fn)
return fn.apply(obj || this, [context].concat(Array.slice(arguments, split.length)));
return fn.apply(obj || this, [context].concat(args));
return context;
},
@@ -903,10 +903,10 @@ var Completion = Module("completion", {
get options() modules.options,
// FIXME
_runCompleter: function _runCompleter(name, filter, maxItems) {
_runCompleter: function _runCompleter(name, filter, maxItems, ...args) {
let context = modules.CompletionContext(filter);
context.maxItems = maxItems;
let res = context.fork.apply(context, ["run", 0, this, name].concat(Array.slice(arguments, 3)));
let res = context.fork.apply(context, ["run", 0, this, name].concat(args));
if (res) {
if (Components.stack.caller.name === "runCompleter") // FIXME
return { items: res.map(function m(i) ({ item: i })) };
@@ -917,14 +917,14 @@ var Completion = Module("completion", {
},
runCompleter: function runCompleter(name, filter, maxItems) {
return this._runCompleter.apply(this, Array.slice(arguments))
return this._runCompleter.apply(this, arguments)
.items.map(function m(i) i.item);
},
listCompleter: function listCompleter(name, filter, maxItems) {
listCompleter: function listCompleter(name, filter, maxItems, ...args) {
let context = modules.CompletionContext(filter || "");
context.maxItems = maxItems;
context.fork.apply(context, ["list", 0, this, name].concat(Array.slice(arguments, 3)));
context.fork.apply(context, ["list", 0, this, name].concat(args));
context = context.contexts["/list"];
context.wait(null, true);
@@ -987,9 +987,9 @@ var Completion = Module("completion", {
}, this);
},
addUrlCompleter: function addUrlCompleter(opt) {
let completer = Completion.UrlCompleter.apply(null, Array.slice(arguments));
completer.args = Array.slice(arguments, completer.length);
addUrlCompleter: function addUrlCompleter(opt, ...args) {
let completer = Completion.UrlCompleter.apply(null, [opt, ...args]);
completer.args = args;
this.urlCompleters[opt] = completer;
},

View File

@@ -65,7 +65,7 @@ var Group = Class("Group", {
get builtin() this.modules.contexts.builtinGroups.indexOf(this) >= 0,
}, {
compileFilter: function (patterns, default_) {
compileFilter: function (patterns, default_ = false) {
if (arguments.length < 2)
default_ = false;

View File

@@ -855,8 +855,7 @@ var JavaScript = Module("javascript", {
mappings: function initMappings(dactyl, modules, window) {
const { mappings, modes } = modules;
function bind() mappings.add.apply(mappings,
[[modes.REPL]].concat(Array.slice(arguments)))
function bind(...args) mappings.add.apply(mappings, [[modes.REPL]].concat(args))
bind(["<Return>"], "Accept the current input",
function ({ self }) { self.accept(); });

View File

@@ -65,14 +65,13 @@ var Modules = function Modules(window) {
*
* @returns {function} The constructor for the resulting module.
*/
function Module(name) {
let args = Array.slice(arguments);
function Module(name, ...args) {
var base = ModuleBase;
if (callable(args[1]))
base = args.splice(1, 1)[0];
if (callable(args[0]))
base = args.shift();
let [, prototype, classProperties, moduleInit] = args;
let [prototype, classProperties, moduleInit] = args;
prototype._metaInit_ = function () {
delete module.prototype._metaInit_;
Class.replaceProperty(modules, module.className, this);

View File

@@ -98,7 +98,7 @@ var Messages = Module("messages", {
file = io.File(file);
function properties(base, iter_, prop) iter(function _properties() {
function key() [base, obj.identifier || obj.name].concat(Array.slice(arguments)).join(".").replace(/[\\:=]/g, "\\$&");
function key(...args) [base, obj.identifier || obj.name].concat(args).join(".").replace(/[\\:=]/g, "\\$&");
prop = prop || "description";
for (var obj in iter_) {

View File

@@ -1622,11 +1622,11 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @param {function} func The function to call
* @param {object} self The 'this' object for the function.
*/
trapErrors: function trapErrors(func, self) {
trapErrors: function trapErrors(func, self, ...args) {
try {
if (!callable(func))
func = self[func];
return func.apply(self || this, Array.slice(arguments, 2));
return func.apply(self || this, args);
}
catch (e) {
this.reportError(e);
@@ -1708,9 +1708,9 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @param {object} self The 'this' object of the method.
* @param ... Arguments to pass to *meth*.
*/
withProperErrors: function withProperErrors(meth, self) {
withProperErrors: function withProperErrors(meth, self, ...args) {
try {
return (callable(meth) ? meth : self[meth]).apply(self, Array.slice(arguments, withProperErrors.length));
return (callable(meth) ? meth : self[meth]).apply(self, args);
}
catch (e) {
throw e.stack ? e : Error(e);