From 59d55d1e065bfabc105b97dd5664e186dc69f4a0 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 11 Feb 2020 16:35:16 -0800 Subject: [PATCH] js: Use modern spread arguments syntax. Signed-off-by: Anders Kaseorg --- .../node_tests/composebox_typeahead.js | 5 ++- frontend_tests/node_tests/input_pill.js | 4 +- frontend_tests/node_tests/presence.js | 8 ++-- frontend_tests/node_tests/settings_bots.js | 6 +-- frontend_tests/node_tests/settings_org.js | 4 +- frontend_tests/zjsunit/zjquery.js | 40 ++++++++----------- static/js/archive.js | 8 ++-- static/js/blueslip.js | 10 ++--- static/js/filter.js | 3 +- static/js/list_render.js | 5 ++- static/js/message_viewport.js | 6 +-- static/js/popovers.js | 4 +- static/js/setup.js | 8 ++-- static/js/stats/stats.js | 2 +- static/js/subs.js | 10 ++--- static/js/templates.js | 32 +++++++-------- static/js/unread.js | 13 ++---- static/shared/js/typeahead.js | 10 ++--- 18 files changed, 84 insertions(+), 94 deletions(-) diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index 0829977782..5db61eb03e 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -308,11 +308,12 @@ run_test('content_typeahead_selected', () => { }; let caret_called1 = false; let caret_called2 = false; - fake_this.$element.caret = function (arg1, arg2) { - if (arguments.length === 0) { // .caret() used in split_at_cursor + fake_this.$element.caret = function (...args) { + if (args.length === 0) { // .caret() used in split_at_cursor caret_called1 = true; return fake_this.query.length; } + const [arg1, arg2] = args; // .caret() used in setTimeout assert.equal(arg1, arg2); caret_called2 = true; diff --git a/frontend_tests/node_tests/input_pill.js b/frontend_tests/node_tests/input_pill.js index 3391044434..51c5cde3d1 100644 --- a/frontend_tests/node_tests/input_pill.js +++ b/frontend_tests/node_tests/input_pill.js @@ -513,7 +513,7 @@ run_test('exit button on pill', () => { }; const exit_click_handler = container.get_on_handler('click', '.exit'); - exit_click_handler.apply(exit_button_stub, [e]); + exit_click_handler.call(exit_button_stub, e); assert(next_pill_focused); @@ -548,7 +548,7 @@ run_test('misc things', () => { }, }; - animation_end_handler.apply(input_stub); + animation_end_handler.call(input_stub); assert(shake_class_removed); // bad data diff --git a/frontend_tests/node_tests/presence.js b/frontend_tests/node_tests/presence.js index 3c086bf1d6..84eb27735d 100644 --- a/frontend_tests/node_tests/presence.js +++ b/frontend_tests/node_tests/presence.js @@ -124,10 +124,10 @@ run_test('status_from_timestamp', () => { pushable: true, }; let called = false; - blueslip.error = function () { - assert.equal(arguments[0], 'Unexpected status'); - assert.deepEqual(arguments[1].presence_object, info.random_client); - assert.equal(arguments[2], undefined); + blueslip.error = function (msg, more_info, stack) { + assert.equal(msg, 'Unexpected status'); + assert.deepEqual(more_info.presence_object, info.random_client); + assert.equal(stack, undefined); called = true; }; status = status_from_timestamp( diff --git a/frontend_tests/node_tests/settings_bots.js b/frontend_tests/node_tests/settings_bots.js index 618a0bef26..84fd23d6b3 100644 --- a/frontend_tests/node_tests/settings_bots.js +++ b/frontend_tests/node_tests/settings_bots.js @@ -79,7 +79,7 @@ function test_create_bot_type_input_box_toggle(f) { const GENERIC_BOT_TYPE = '1'; $('#create_bot_type :selected').val(EMBEDDED_BOT_TYPE); - f.apply(); + f(); assert(!create_payload_url.hasClass('required')); assert(!payload_url_inputbox.visible()); assert($('#select_service_name').hasClass('required')); @@ -87,13 +87,13 @@ function test_create_bot_type_input_box_toggle(f) { assert(config_inputbox.visible()); $('#create_bot_type :selected').val(OUTGOING_WEBHOOK_BOT_TYPE); - f.apply(); + f(); assert(create_payload_url.hasClass('required')); assert(payload_url_inputbox.visible()); assert(!config_inputbox.visible()); $('#create_bot_type :selected').val(GENERIC_BOT_TYPE); - f.apply(); + f(); assert(!create_payload_url.hasClass('required')); assert(!payload_url_inputbox.visible()); assert(!config_inputbox.visible()); diff --git a/frontend_tests/node_tests/settings_org.js b/frontend_tests/node_tests/settings_org.js index 5e0c604f8f..67986853ef 100644 --- a/frontend_tests/node_tests/settings_org.js +++ b/frontend_tests/node_tests/settings_org.js @@ -429,7 +429,7 @@ function test_change_allow_subdomains(change_allow_subdomains) { parents_obj.set_find_results('.domain', domain_obj); elem_obj.prop('checked', allow); - change_allow_subdomains.apply(elem_obj, [ev]); + change_allow_subdomains.call(elem_obj, ev); success_callback(); assert.equal(info.val(), @@ -440,7 +440,7 @@ function test_change_allow_subdomains(change_allow_subdomains) { allow = false; elem_obj.prop('checked', allow); - change_allow_subdomains.apply(elem_obj, [ev]); + change_allow_subdomains.call(elem_obj, ev); success_callback(); assert.equal(info.val(), 'translated: Update successful: Subdomains no longer allowed for example.com'); diff --git a/frontend_tests/zjsunit/zjquery.js b/frontend_tests/zjsunit/zjquery.js index 852a19f2a7..7878ba34c8 100644 --- a/frontend_tests/zjsunit/zjquery.js +++ b/frontend_tests/zjsunit/zjquery.js @@ -54,10 +54,8 @@ exports.make_event_store = (selector) => { return handler; }, - off: function () { - const event_name = arguments[0]; - - if (arguments.length === 1) { + off: function (event_name, ...args) { + if (args.length === 0) { on_functions.delete(event_name); return; } @@ -69,15 +67,12 @@ exports.make_event_store = (selector) => { throw Error('zjquery does not support this call sequence'); }, - on: function () { + on: function (event_name, ...args) { // parameters will either be // (event_name, handler) or // (event_name, sel, handler) - const event_name = arguments[0]; - let handler; - - if (arguments.length === 2) { - handler = arguments[1]; + if (args.length === 1) { + const [handler] = args; if (on_functions.has(event_name)) { console.info('\nEither the app or the test can be at fault here..'); console.info('(sometimes you just want to call $.clear_all_elements();)\n'); @@ -88,12 +83,11 @@ exports.make_event_store = (selector) => { return; } - if (arguments.length !== 3) { + if (args.length !== 2) { throw Error('wrong number of arguments passed in'); } - const sel = arguments[1]; - handler = arguments[2]; + const [sel, handler] = args; assert.equal(typeof sel, 'string', 'String selectors expected here.'); assert.equal(typeof handler, 'function', 'An handler function expected here.'); @@ -267,12 +261,12 @@ exports.make_new_elem = function (selector, opts) { event_store.generic_event('keyup', arg); return self; }, - off: function () { - event_store.off.apply(undefined, arguments); + off: function (...args) { + event_store.off(...args); return self; }, - on: function () { - event_store.on.apply(undefined, arguments); + on: function (...args) { + event_store.on(...args); return self; }, parent: function () { @@ -346,18 +340,18 @@ exports.make_new_elem = function (selector, opts) { event_store.trigger(ev); return self; }, - val: function () { - if (arguments.length === 0) { + val: function (...args) { + if (args.length === 0) { return value || ''; } - value = arguments[0]; + [value] = args; return self; }, - css: function () { - if (arguments.length === 0) { + css: function (...args) { + if (args.length === 0) { return css || {}; } - css = arguments[0]; + [css] = args; return self; }, visible: function () { diff --git a/static/js/archive.js b/static/js/archive.js index 0b8a140845..29fa1076ac 100644 --- a/static/js/archive.js +++ b/static/js/archive.js @@ -118,11 +118,11 @@ function scroll_finish() { } $(function () { - $.fn.safeOuterHeight = function () { - return $(this).outerHeight.apply(this, arguments) || 0; + $.fn.safeOuterHeight = function (...args) { + return this.outerHeight(...args) || 0; }; - $.fn.safeOuterWidth = function () { - return $(this).outerWidth.apply(this, arguments) || 0; + $.fn.safeOuterWidth = function (...args) { + return this.outerWidth(...args) || 0; }; $('.app').scroll(_.throttle(function () { scroll_finish(); diff --git a/static/js/blueslip.js b/static/js/blueslip.js index 0ec039223d..19b8f1ee50 100644 --- a/static/js/blueslip.js +++ b/static/js/blueslip.js @@ -216,22 +216,22 @@ function build_arg_list(msg, more_info) { exports.debug = function blueslip_debug(msg, more_info) { const args = build_arg_list(msg, more_info); - logger.debug.apply(logger, args); + logger.debug(...args); }; exports.log = function blueslip_log(msg, more_info) { const args = build_arg_list(msg, more_info); - logger.log.apply(logger, args); + logger.log(...args); }; exports.info = function blueslip_info(msg, more_info) { const args = build_arg_list(msg, more_info); - logger.info.apply(logger, args); + logger.info(...args); }; exports.warn = function blueslip_warn(msg, more_info) { const args = build_arg_list(msg, more_info); - logger.warn.apply(logger, args); + logger.warn(...args); if (page_params.debug_mode) { console.trace(); } @@ -242,7 +242,7 @@ exports.error = function blueslip_error(msg, more_info, stack) { stack = Error().stack; } const args = build_arg_list(msg, more_info); - logger.error.apply(logger, args); + logger.error(...args); report_error(msg, stack, {more_info: more_info}); if (page_params.debug_mode) { diff --git a/static/js/filter.js b/static/js/filter.js index ece63e48bc..5bd18e2ca9 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -507,7 +507,7 @@ Filter.prototype = { return sorted_terms; }, - can_bucket_by: function () { + can_bucket_by: function (...wanted_term_types) { // TODO: in ES6 use spread operator // // Examples call: @@ -520,7 +520,6 @@ Filter.prototype = { // a predicate to a larger list of candidate ids. // // (It's for optimization, basically.) - const wanted_term_types = [].slice.call(arguments); const all_term_types = this.sorted_term_types(); const term_types = all_term_types.slice(0, wanted_term_types.length); diff --git a/static/js/list_render.js b/static/js/list_render.js index 8ca0c9002c..b9898b443e 100644 --- a/static/js/list_render.js +++ b/static/js/list_render.js @@ -145,13 +145,14 @@ exports.create = function ($container, list, opts) { // reset the data associated with a list. This is so that instead of // initializing a new progressive list render instance, you can just // update the data of an existing one. - data: function (data) { + data: function (...args) { // if no args are provided then just return the existing data. // this interface is similar to how many jQuery functions operate, // where a call to the method without data returns the existing data. - if (typeof data === "undefined" && arguments.length === 0) { + if (args.length === 0) { return meta.list; } + const [data] = args; if (Array.isArray(data)) { meta.list = data; diff --git a/static/js/message_viewport.js b/static/js/message_viewport.js index efe6de108c..7914d31603 100644 --- a/static/js/message_viewport.js +++ b/static/js/message_viewport.js @@ -252,10 +252,10 @@ function make_dimen_wrapper(dimen_name, dimen_func) { return dimen_func.call(exports.message_pane); }, }); - return function viewport_dimension_wrapper() { - if (arguments.length !== 0) { + return function viewport_dimension_wrapper(...args) { + if (args.length !== 0) { dimensions[dimen_name].reset(); - return dimen_func.apply(exports.message_pane, arguments); + return dimen_func.apply(exports.message_pane, args); } return dimensions[dimen_name].get(); }; diff --git a/static/js/popovers.js b/static/js/popovers.js index 39daefecd6..ede0017e4d 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -28,9 +28,9 @@ function elem_to_user_id(elem) { // ones that no longer have valid parents in the DOM. (function (popover) { - $.fn.popover = function () { + $.fn.popover = function (...args) { // apply the jQuery object as `this`, and popover function arguments. - popover.apply(this, arguments); + popover.apply(this, args); // if there is a valid "popover" key in the jQuery data object then // push it to the array. diff --git a/static/js/setup.js b/static/js/setup.js index 8214c5adfc..be2322bc1f 100644 --- a/static/js/setup.js +++ b/static/js/setup.js @@ -21,12 +21,12 @@ $(function () { // will no longer cast to a Number but rather NaN. For this, we create the // `safeOuterHeight` and `safeOuterWidth` functions to safely return a result // (or 0). - $.fn.safeOuterHeight = function () { - return $(this).outerHeight.apply(this, arguments) || 0; + $.fn.safeOuterHeight = function (...args) { + return this.outerHeight(...args) || 0; }; - $.fn.safeOuterWidth = function () { - return $(this).outerWidth.apply(this, arguments) || 0; + $.fn.safeOuterWidth = function (...args) { + return this.outerWidth(...args) || 0; }; // For some reason, jQuery wants this to be attached to an element. diff --git a/static/js/stats/stats.js b/static/js/stats/stats.js index 2971e579e4..831ca85cb7 100644 --- a/static/js/stats/stats.js +++ b/static/js/stats/stats.js @@ -472,7 +472,7 @@ function populate_messages_sent_by_client(data) { $('#id_messages_sent_by_client > div').removeClass("spinner"); const data_ = plot_data[user_button][time_button]; layout.height = layout.margin.b + data_.trace.x.length * 30; - layout.xaxis.range = [0, Math.max.apply(null, data_.trace.x) * 1.3]; + layout.xaxis.range = [0, Math.max(...data_.trace.x) * 1.3]; Plotly.newPlot('id_messages_sent_by_client', [data_.trace, data_.trace_annotations], layout, diff --git a/static/js/subs.js b/static/js/subs.js index fdd8b8a9a3..c3cfd6cb58 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -437,11 +437,11 @@ exports.filter_table = function (query) { ui.reset_scrollbar($("#subscription_overlay .streams-list")); - const all_stream_ids = [].concat( - buckets.name, - buckets.desc, - buckets.other - ); + const all_stream_ids = [ + ...buckets.name, + ...buckets.desc, + ...buckets.other, + ]; for (const stream_id of all_stream_ids) { ui.get_content_element($('#subscriptions_table .streams-list')).append(widgets[stream_id]); diff --git a/static/js/templates.js b/static/js/templates.js index 8204b16299..1327fa58e2 100644 --- a/static/js/templates.js +++ b/static/js/templates.js @@ -16,31 +16,31 @@ Handlebars.registerHelper('plural', function (condition, one, other) { Handlebars.registerHelper({ eq: function (a, b) { return a === b; }, - and: function () { - // last argument is Handlebars options - if (arguments.length < 2) { + and: function (...args) { + args.pop(); // Handlebars options + if (args.length === 0) { return true; } - let i; - for (i = 0; i < arguments.length - 2; i += 1) { - if (!arguments[i] || Handlebars.Utils.isEmpty(arguments[i])) { - return arguments[i]; + const last = args.pop(); + for (const arg of args) { + if (!arg || Handlebars.Utils.isEmpty(arg)) { + return arg; } } - return arguments[i]; + return last; }, - or: function () { - // last argument is Handlebars options - if (arguments.length < 2) { + or: function (...args) { + args.pop(); // Handlebars options + if (args.length === 0) { return false; } - let i; - for (i = 0; i < arguments.length - 2; i += 1) { - if (arguments[i] && !Handlebars.Utils.isEmpty(arguments[i])) { - return arguments[i]; + const last = args.pop(); + for (const arg of args) { + if (arg && !Handlebars.Utils.isEmpty(arg)) { + return arg; } } - return arguments[i]; + return last; }, not: function (a) { return !a || Handlebars.Utils.isEmpty(a); }, }); diff --git a/static/js/unread.js b/static/js/unread.js index eb20586fd8..87c73e5f91 100644 --- a/static/js/unread.js +++ b/static/js/unread.js @@ -157,15 +157,12 @@ exports.unread_pm_counter = (function () { }; self.get_msg_ids = function () { - const lists = []; + const ids = []; for (const id_set of bucketer.values()) { - const members = Array.from(id_set); - lists.push(members); + ids.push(...Array.from(id_set)); } - const ids = [].concat.apply([], lists); - return util.sorted_ids(ids); }; @@ -333,16 +330,14 @@ exports.unread_topic_counter = (function () { return []; } - const topic_lists = []; + const ids = []; const sub = stream_data.get_sub_by_id(stream_id); for (const [topic, msgs] of per_stream_bucketer) { if (sub && !muting.is_topic_muted(stream_id, topic)) { - topic_lists.push(Array.from(msgs)); + ids.push(...Array.from(msgs)); } } - const ids = [].concat.apply([], topic_lists); - return util.sorted_ids(ids); }; diff --git a/static/shared/js/typeahead.js b/static/shared/js/typeahead.js index e99755c2e9..32ea0f08f9 100644 --- a/static/shared/js/typeahead.js +++ b/static/shared/js/typeahead.js @@ -170,9 +170,9 @@ exports.sort_emojis = function (objs, query) { (x) => x.emoji_name ); - return [].concat( - popular_emoji_matches, - triage_results.matches, - triage_results.rest - ); + return [ + ...popular_emoji_matches, + ...triage_results.matches, + ...triage_results.rest, + ]; };