eslint: Fix unicorn/no-lonely-if.

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v24.0.0/docs/rules/no-lonely-if.md

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-12-22 02:26:39 -08:00
committed by Tim Abbott
parent 6acbcec39d
commit 2f80415756
23 changed files with 223 additions and 247 deletions

View File

@@ -46,11 +46,9 @@ exports.initialize = function () {
// Later we check whether after MS_DELAY the user is still
// long touching the same message as it can be possible that
// user touched another message within MS_DELAY period.
if (meta.touchdown === true && !meta.invalid) {
if (id === meta.current_target) {
if (meta.touchdown === true && !meta.invalid && id === meta.current_target) {
$(this).trigger("longtap");
}
}
}, MS_DELAY);
});

View File

@@ -19,14 +19,16 @@ exports.smart_insert = function (textarea, syntax) {
const before_str = textarea.val().slice(0, pos);
const after_str = textarea.val().slice(pos);
if (pos > 0) {
if (
pos > 0 &&
// If there isn't space either at the end of the content
// before the insert or (unlikely) at the start of the syntax,
// add one.
if (!is_space(before_str.slice(-1)) && !is_space(syntax[0])) {
!is_space(before_str.slice(-1)) &&
!is_space(syntax[0])
) {
syntax = " " + syntax;
}
}
// If there isn't whitespace either at the end of the syntax or the
// start of the content after the syntax, add one.

View File

@@ -227,13 +227,14 @@ function handle_keydown(e) {
function handle_keyup(e) {
const code = e.keyCode || e.which;
if (code === 13 || (code === 9 && !e.shiftKey)) {
if (
// Enter key or Tab key
if (nextFocus) {
(code === 13 || (code === 9 && !e.shiftKey)) &&
nextFocus
) {
nextFocus.trigger("focus");
nextFocus = false;
}
}
}
exports.split_at_cursor = function (query, input) {

View File

@@ -473,8 +473,7 @@ exports.drafts_handle_events = function (e, event_key) {
const focused_draft_id = row_with_focus().data("draft-id");
// Allows user to delete drafts with Backspace
if (event_key === "backspace" || event_key === "delete") {
if (focused_draft_id !== undefined) {
if ((event_key === "backspace" || event_key === "delete") && focused_draft_id !== undefined) {
const draft_row = row_with_focus();
const next_draft_row = row_after_focus();
const prev_draft_row = row_before_focus();
@@ -497,7 +496,6 @@ exports.drafts_handle_events = function (e, event_key) {
remove_draft(draft_row);
}
}
// This handles when pressing Enter while looking at drafts.
// It restores draft that is focused.

View File

@@ -512,9 +512,12 @@ exports.navigate = function (event_name, e) {
function process_keypress(e) {
const is_filter_focused = $(".emoji-popover-filter").is(":focus");
const pressed_key = e.which;
if (!is_filter_focused && pressed_key !== 58) {
if (
!is_filter_focused &&
// ':' => 58, is a hotkey for toggling reactions popover.
if ((pressed_key >= 32 && pressed_key <= 126) || pressed_key === 8) {
pressed_key !== 58 &&
((pressed_key >= 32 && pressed_key <= 126) || pressed_key === 8)
) {
// Handle only printable characters or Backspace.
e.preventDefault();
e.stopPropagation();
@@ -536,7 +539,6 @@ function process_keypress(e) {
change_focus_to_filter();
filter_emojis();
}
}
}
exports.emoji_select_tab = function (elt) {

View File

@@ -58,16 +58,18 @@ exports.first_visible_message = function (bar) {
// Important: This will break if we ever have things that are
// not message rows inside a recipient_row block.
message = message.next(".message_row");
if (message.length > 0 && result) {
if (
message.length > 0 &&
result &&
// Before returning a result, we check whether the next
// message's top is actually below the bottom of the
// floating recipient bar; this is different from the
// bottom of our current message because there may be a
// between-messages date separator row in between.
if (top_offset(message) < frb_bottom - date_bar_height_offset) {
top_offset(message) < frb_bottom - date_bar_height_offset
) {
result = message;
}
}
if (result) {
return result;
}

View File

@@ -150,8 +150,7 @@ function do_hashchange_overlay(old_hash) {
//
// In most situations we skip by this logic and load
// the new overlay.
if (coming_from_overlay) {
if (base === old_base) {
if (coming_from_overlay && base === old_base) {
if (base === "streams") {
subs.change_state(section);
return;
@@ -181,7 +180,6 @@ function do_hashchange_overlay(old_hash) {
// changes.
return;
}
}
// It's not super likely that an overlay is already open,
// but you can jump from /settings to /streams by using

View File

@@ -559,19 +559,15 @@ exports.process_hotkey = function (e, hotkey) {
return false;
}
if (event_name === "up_arrow") {
if (list_util.inside_list(e)) {
if (event_name === "up_arrow" && list_util.inside_list(e)) {
list_util.go_up(e);
return true;
}
}
if (event_name === "down_arrow") {
if (list_util.inside_list(e)) {
if (event_name === "down_arrow" && list_util.inside_list(e)) {
list_util.go_down(e);
return true;
}
}
if (menu_dropdown_hotkeys.has(event_name)) {
if (popovers.actions_popped()) {
@@ -618,12 +614,10 @@ exports.process_hotkey = function (e, hotkey) {
// The next two sections date back to 00445c84 and are Mac/Chrome-specific,
// and they should possibly be eliminated in favor of keeping standard
// browser behavior.
if (event_name === "backspace") {
if ($("#compose-send-button").is(":focus")) {
if (event_name === "backspace" && $("#compose-send-button").is(":focus")) {
// Ignore Backspace; don't navigate back a page.
return true;
}
}
if (event_name === "narrow_to_compose_target") {
narrow.to_compose_target();

View File

@@ -282,11 +282,9 @@ exports.create = function (opts) {
// should switch to focus the last pill in the list.
// the rest of the events then will be taken care of in the function
// below that handles events on the ".pill" class.
if (char === KEY.LEFT_ARROW) {
if (window.getSelection().anchorOffset === 0) {
if (char === KEY.LEFT_ARROW && window.getSelection().anchorOffset === 0) {
store.$parent.find(".pill").last().trigger("focus");
}
}
// Typing of the comma is prevented if the last field doesn't validate,
// as well as when the new pill is created.

View File

@@ -35,15 +35,15 @@ const ls = {
let data = localStorage.getItem(key);
data = ls.parseJSON(data);
if (data) {
if (data.__valid) {
if (
data &&
data.__valid &&
// JSON forms of data with `Infinity` turns into `null`,
// so if null then it hasn't expired since nothing was specified.
if (!ls.isExpired(data.expires) || data.expires === null) {
(!ls.isExpired(data.expires) || data.expires === null)
) {
return data;
}
}
}
return undefined;
},

View File

@@ -168,15 +168,17 @@ exports.update_messages = function update_messages(events) {
// The event.message_ids received from the server are not in sorted order.
event_messages.sort((a, b) => a.id - b.id);
if (going_forward_change && stream_name && compose_stream_name) {
if (stream_name.toLowerCase() === compose_stream_name.toLowerCase()) {
if (orig_topic === compose_state.topic()) {
if (
going_forward_change &&
stream_name &&
compose_stream_name &&
stream_name.toLowerCase() === compose_stream_name.toLowerCase() &&
orig_topic === compose_state.topic()
) {
changed_compose = true;
compose_state.topic(new_topic);
compose_fade.set_focused_recipient("stream");
}
}
}
for (const msg of event_messages) {
if (msg === undefined) {
@@ -224,7 +226,8 @@ exports.update_messages = function update_messages(events) {
});
}
if (going_forward_change) {
if (
going_forward_change &&
// This logic is a bit awkward. What we're trying to
// accomplish is two things:
//
@@ -238,8 +241,10 @@ exports.update_messages = function update_messages(events) {
//
// Code further down takes care of the actual rerendering of
// messages within a narrow.
if (selection_changed_topic) {
if (current_filter && current_filter.has_topic(stream_name, orig_topic)) {
selection_changed_topic &&
current_filter &&
current_filter.has_topic(stream_name, orig_topic)
) {
let new_filter = current_filter;
if (new_filter && stream_changed) {
// TODO: This logic doesn't handle the
@@ -277,8 +282,6 @@ exports.update_messages = function update_messages(events) {
narrow.activate(operators, opts);
}
}
}
}
// Ensure messages that are no longer part of this
// narrow are deleted and messages that are now part

View File

@@ -69,12 +69,10 @@ exports.get_messages_in_topic = function (stream_id, topic) {
exports.get_max_message_id_in_stream = function (stream_id) {
let max_message_id = 0;
for (const msg of message_list.all.all_messages()) {
if (msg.type === "stream" && msg.stream_id === stream_id) {
if (msg.id > max_message_id) {
if (msg.type === "stream" && msg.stream_id === stream_id && msg.id > max_message_id) {
max_message_id = msg.id;
}
}
}
return max_message_id;
};

View File

@@ -32,11 +32,9 @@ const get_step = function ($process) {
function should_show_notifications(ls) {
// if the user said to never show banner on this computer again, it will
// be stored as `true` so we want to negate that.
if (localstorage.supported()) {
if (ls.get("dontAskForNotifications") === true) {
if (localstorage.supported() && ls.get("dontAskForNotifications") === true) {
return false;
}
}
return (
// notifications *basically* don't work on any mobile platforms, so don't

View File

@@ -3,8 +3,7 @@
$(() => {
// This code will be executed when the user visits /login and
// dev_login.html is rendered.
if ($("[data-page-id='dev-login']").length > 0) {
if (window.location.hash.slice(0, 1) === "#") {
if ($("[data-page-id='dev-login']").length > 0 && window.location.hash.slice(0, 1) === "#") {
/* We append the location.hash to the input field with name next so that URL can be
preserved after user is logged in. See this:
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
@@ -13,5 +12,4 @@ $(() => {
$(this).attr("value", new_value);
});
}
}
});

View File

@@ -95,16 +95,16 @@ $(() => {
// Code in this block will be executed when the user visits /register
// i.e. accounts_home.html is rendered.
if ($("[data-page-id='accounts-home']").length > 0) {
if (window.location.hash.slice(0, 1) === "#") {
if (
$("[data-page-id='accounts-home']").length > 0 &&
window.location.hash.slice(0, 1) === "#"
) {
document.email_form.action += window.location.hash;
}
}
// Code in this block will be executed when the user is at login page
// i.e. login.html is rendered.
if ($("[data-page-id='login-page']").length > 0) {
if (window.location.hash.slice(0, 1) === "#") {
if ($("[data-page-id='login-page']").length > 0 && window.location.hash.slice(0, 1) === "#") {
/* We append the location.hash to the formaction so that URL can be
preserved after user is logged in. See this:
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
@@ -112,7 +112,6 @@ $(() => {
$("#login_form").attr("action", email_formaction + "/" + window.location.hash);
$(".social_login_form input[name='next']").attr("value", "/" + window.location.hash);
}
}
$("#send_confirm").validate({
errorElement: "div",

View File

@@ -124,18 +124,14 @@ exports.update_info_from_event = function (user_id, info, server_timestamp) {
raw.server_timestamp = server_timestamp;
for (const rec of Object.values(info)) {
if (rec.status === "active") {
if (rec.timestamp > (raw.active_timestamp || 0)) {
if (rec.status === "active" && rec.timestamp > (raw.active_timestamp || 0)) {
raw.active_timestamp = rec.timestamp;
}
}
if (rec.status === "idle") {
if (rec.timestamp > (raw.idle_timestamp || 0)) {
if (rec.status === "idle" && rec.timestamp > (raw.idle_timestamp || 0)) {
raw.idle_timestamp = rec.timestamp;
}
}
}
raw_info.set(user_id, raw);

View File

@@ -114,12 +114,11 @@ exports.get_direction = function (str) {
// Extracting high and low surrogates and putting them together.
// See https://en.wikipedia.org/wiki/UTF-16#Description or section 3 of https://tools.ietf.org/html/rfc2781.
let ch = str.charCodeAt(i);
if (ch >= 0xd800 && ch < 0xe000) {
// 0xd800 <= ch < 0xe000
// ch is inside surrogate range.
if (ch >= 0xd800 && ch < 0xdc00) {
// 0xd800 <= ch < 0xdc00
// ch is inside high surrogate range.
// If it made a surrogate pair with the next character, put them together.
// Otherwise, ignore the encoding error and leave it as it is.
if (ch < 0xdc00) {
const ch2 = i + 1 < str.length ? str.charCodeAt(i + 1) : 0;
if (ch2 >= 0xdc00 && ch2 < 0xe000) {
// 0xdc00 <= ch2 < 0xe000
@@ -130,7 +129,6 @@ exports.get_direction = function (str) {
i += 1;
}
}
}
const bidi_class = get_bidi_class(ch);
if (bidi_class === "I") {
@@ -145,12 +143,10 @@ exports.get_direction = function (str) {
if (isolations === 0) {
return "rtl";
}
} else if (bidi_class === "L") {
if (isolations === 0) {
} else if (bidi_class === "L" && isolations === 0) {
return "ltr";
}
}
}
return "ltr";
};

View File

@@ -702,17 +702,18 @@ exports.get_search_result = function (base_query, query) {
}
}
if (!page_params.search_pills_enabled) {
if (
!page_params.search_pills_enabled &&
// This is unique to the legacy search system. With pills
// it is difficult to "suggest" a subset of operators,
// and there's a more natural mechanism under that paradigm,
// where the user just deletes one or more pills. So you
// won't see this is in the new code.
if (attacher.result.length < max_items) {
attacher.result.length < max_items
) {
const subset_suggestions = get_operator_subset_suggestions(search_operators);
attacher.concat(subset_suggestions);
}
}
return attacher.result.slice(0, max_items);
};

View File

@@ -97,12 +97,10 @@ exports.render_bots = function () {
user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active;
}
if (exports.can_create_new_bots()) {
if (!user_owns_an_active_bot) {
if (exports.can_create_new_bots() && !user_owns_an_active_bot) {
focus_tab.add_a_new_bot_tab();
return;
}
}
if ($("#bots_lists_navbar .add-a-new-bot-tab").hasClass("active")) {
$("#add-a-new-bot-form").show();

View File

@@ -154,13 +154,11 @@ class PerStreamHistory {
const existing = this.topics.get(topic_name);
if (existing) {
if (!existing.historical) {
if (existing && !existing.historical) {
// Trust out local data more, since it
// maintains counts.
continue;
}
}
// If we get here, we are either finding out about
// the topic for the first time, or we are getting

View File

@@ -343,12 +343,10 @@ exports.update_settings_for_unsubscribed = function (sub) {
};
function triage_stream(query, sub) {
if (query.subscribed_only) {
if (query.subscribed_only && !sub.subscribed) {
// reject non-subscribed streams
if (!sub.subscribed) {
return "rejected";
}
}
const search_terms = search_util.get_search_terms(query.input);

View File

@@ -95,14 +95,16 @@ exports.last_seen_status_from_date = function (last_active_date, current_date) {
if (days < 90) {
return i18n.t("__days__ days ago", {days});
} else if (days > 90 && days < 365) {
if (current_date.getFullYear() === last_active_date.getFullYear()) {
} else if (
days > 90 &&
days < 365 &&
current_date.getFullYear() === last_active_date.getFullYear()
) {
// Online more than 90 days ago, in the same year
return i18n.t("__last_active_date__", {
last_active_date: last_active_date.toString("MMM\u00A0dd"),
});
}
}
return i18n.t("__last_active_date__", {
last_active_date: last_active_date.toString("MMM\u00A0dd,\u00A0yyyy"),
});

View File

@@ -74,11 +74,9 @@ exports.initialize_kitchen_sink_stuff = function () {
if (message_viewport.at_top()) {
navigate.up();
}
} else if (delta > 0) {
if (message_viewport.at_bottom()) {
} else if (delta > 0 && message_viewport.at_bottom()) {
navigate.down();
}
}
message_viewport.set_last_movement_direction(delta);
}, 50);