From 62fcf98b6fe8fbcf59b26204cf8a7a993648cb00 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 26 May 2020 15:50:02 -0700 Subject: [PATCH] js: Use hasOwnProperty correctly or not at all. Signed-off-by: Anders Kaseorg --- frontend_tests/casper_lib/common.js | 2 +- frontend_tests/node_tests/typing_status.js | 3 +-- static/js/blueslip.js | 4 ++-- static/js/localstorage.js | 2 +- static/js/message_events.js | 2 +- static/js/popovers.js | 6 +----- static/js/stats/stats.js | 11 ++++------- static/js/stream_edit.js | 2 +- static/js/user_groups.js | 2 +- 9 files changed, 13 insertions(+), 21 deletions(-) diff --git a/frontend_tests/casper_lib/common.js b/frontend_tests/casper_lib/common.js index 135eba75a7..22dc2b3612 100644 --- a/frontend_tests/casper_lib/common.js +++ b/frontend_tests/casper_lib/common.js @@ -170,7 +170,7 @@ exports.check_form = function (form_selector, expected, test_name) { var values = casper.getFormValues(form_selector); var k; for (k in expected) { - if (expected.hasOwnProperty(k)) { + if (Object.prototype.hasOwnProperty.call(expected, k)) { casper.test.assertEqual(values[k], expected[k], test_name ? test_name + ": " + k : undefined); } diff --git a/frontend_tests/node_tests/typing_status.js b/frontend_tests/node_tests/typing_status.js index b0d63b6a73..86d4aaca1d 100644 --- a/frontend_tests/node_tests/typing_status.js +++ b/frontend_tests/node_tests/typing_status.js @@ -267,8 +267,7 @@ run_test('basics', () => { }; // stub functions to see how may time they are called - for (const method in call_count) { - if (!call_count.hasOwnProperty(method)) { continue; } + for (const method of Object.keys(call_count)) { typing_status.__Rewire__(method, function () { call_count[method] += 1; }); diff --git a/static/js/blueslip.js b/static/js/blueslip.js index 03bfa2364d..4aeccf616e 100644 --- a/static/js/blueslip.js +++ b/static/js/blueslip.js @@ -191,9 +191,9 @@ BlueslipError.prototype = Object.create(Error.prototype); exports.exception_msg = function blueslip_exception_msg(ex) { let message = ex.message; - if (ex.hasOwnProperty('fileName')) { + if (ex.fileName !== undefined) { message += " at " + ex.fileName; - if (ex.hasOwnProperty('lineNumber')) { + if (ex.lineNumber !== undefined) { message += ":" + ex.lineNumber; } } diff --git a/static/js/localstorage.js b/static/js/localstorage.js index e35acae999..faf4a09254 100644 --- a/static/js/localstorage.js +++ b/static/js/localstorage.js @@ -164,7 +164,7 @@ let warned_of_localstorage = false; localstorage.supported = function supports_localstorage() { try { - return window.hasOwnProperty('localStorage') && window.localStorage !== null; + return window.localStorage !== undefined && window.localStorage !== null; } catch (e) { if (!warned_of_localstorage) { blueslip.error("Client browser does not support local storage, will lose socket message on reload"); diff --git a/static/js/message_events.js b/static/js/message_events.js index 6f8e2660ee..d9949c0ebb 100644 --- a/static/js/message_events.js +++ b/static/js/message_events.js @@ -21,7 +21,7 @@ function maybe_add_narrowed_messages(messages, msg_list) { const elsewhere_messages = []; for (const elem of messages) { - if (data.messages.hasOwnProperty(elem.id)) { + if (Object.prototype.hasOwnProperty.call(data.messages, elem.id)) { util.set_match_data(elem, data.messages[elem.id]); new_messages.push(elem); } else { diff --git a/static/js/popovers.js b/static/js/popovers.js index 38b14e17ad..2ed63999b2 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -42,11 +42,7 @@ function elem_to_user_id(elem) { }; // add back all shallow properties of $.fn.popover to the new proxied version. - for (const x in popover) { - if (popover.hasOwnProperty(x)) { - $.fn.popover[x] = popover[x]; - } - } + Object.assign($.fn.popover, popover); }($.fn.popover)); function copy_email_handler(e) { diff --git a/static/js/stats/stats.js b/static/js/stats/stats.js index 8579999ed7..3de203c33d 100644 --- a/static/js/stats/stats.js +++ b/static/js/stats/stats.js @@ -337,16 +337,13 @@ function round_to_percentages(values, total) { // Last label will turn into "Other" if time_series data has a label not in labels function compute_summary_chart_data(time_series_data, num_steps, labels_) { const data = new Map(); - for (const key in time_series_data) { - if (!time_series_data.hasOwnProperty(key)) { - continue; - } - if (time_series_data[key].length < num_steps) { - num_steps = time_series_data[key].length; + for (const [key, array] of Object.entries(time_series_data)) { + if (array.length < num_steps) { + num_steps = array.length; } let sum = 0; for (let i = 1; i <= num_steps; i += 1) { - sum += time_series_data[key][time_series_data[key].length - i]; + sum += array[array.length - i]; } data.set(key, sum); } diff --git a/static/js/stream_edit.js b/static/js/stream_edit.js index 19b0d11d85..af6a79f7be 100644 --- a/static/js/stream_edit.js +++ b/static/js/stream_edit.js @@ -576,7 +576,7 @@ exports.initialize = function () { function invite_success(data) { text_box.val(''); - if (data.subscribed.hasOwnProperty(principal)) { + if (Object.prototype.hasOwnProperty.call(data.subscribed, principal)) { stream_subscription_info_elem.text(i18n.t("Subscribed successfully!")); // The rest of the work is done via the subscription -> add event we will get } else { diff --git a/static/js/user_groups.js b/static/js/user_groups.js index fa7ba24ea5..ffe590cf68 100644 --- a/static/js/user_groups.js +++ b/static/js/user_groups.js @@ -91,7 +91,7 @@ exports.initialize = function (params) { }; exports.is_user_group = function (item) { - return item.hasOwnProperty('members'); + return item.members !== undefined; }; window.user_groups = exports;