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,10 +46,8 @@ 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) {
$(this).trigger("longtap");
}
if (meta.touchdown === true && !meta.invalid && id === meta.current_target) {
$(this).trigger("longtap");
}
}, MS_DELAY);
});

View File

@@ -19,13 +19,15 @@ 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])) {
syntax = " " + syntax;
}
!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

View File

@@ -227,12 +227,13 @@ 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) {
nextFocus.trigger("focus");
nextFocus = false;
}
(code === 13 || (code === 9 && !e.shiftKey)) &&
nextFocus
) {
nextFocus.trigger("focus");
nextFocus = false;
}
}

View File

@@ -473,30 +473,28 @@ 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) {
const draft_row = row_with_focus();
const next_draft_row = row_after_focus();
const prev_draft_row = row_before_focus();
let draft_to_be_focused_id;
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();
let draft_to_be_focused_id;
// Try to get the next draft in the list and 'focus' it
// Use previous draft as a fallback
if (next_draft_row[0] !== undefined) {
draft_to_be_focused_id = next_draft_row.data("draft-id");
} else if (prev_draft_row[0] !== undefined) {
draft_to_be_focused_id = prev_draft_row.data("draft-id");
}
const new_focus_element = document.querySelectorAll(
'[data-draft-id="' + draft_to_be_focused_id + '"]',
);
if (new_focus_element[0] !== undefined) {
activate_element(new_focus_element[0].children[0]);
}
remove_draft(draft_row);
// Try to get the next draft in the list and 'focus' it
// Use previous draft as a fallback
if (next_draft_row[0] !== undefined) {
draft_to_be_focused_id = next_draft_row.data("draft-id");
} else if (prev_draft_row[0] !== undefined) {
draft_to_be_focused_id = prev_draft_row.data("draft-id");
}
const new_focus_element = document.querySelectorAll(
'[data-draft-id="' + draft_to_be_focused_id + '"]',
);
if (new_focus_element[0] !== undefined) {
activate_element(new_focus_element[0].children[0]);
}
remove_draft(draft_row);
}
// This handles when pressing Enter while looking at drafts.

View File

@@ -512,30 +512,32 @@ 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) {
// Handle only printable characters or Backspace.
e.preventDefault();
e.stopPropagation();
pressed_key !== 58 &&
((pressed_key >= 32 && pressed_key <= 126) || pressed_key === 8)
) {
// Handle only printable characters or Backspace.
e.preventDefault();
e.stopPropagation();
const emoji_filter = $(".emoji-popover-filter");
const old_query = emoji_filter.val();
let new_query = "";
const emoji_filter = $(".emoji-popover-filter");
const old_query = emoji_filter.val();
let new_query = "";
if (pressed_key === 8) {
// Handles Backspace.
new_query = old_query.slice(0, -1);
} else {
// Handles any printable character.
const key_str = String.fromCharCode(e.which);
new_query = old_query + key_str;
}
emoji_filter.val(new_query);
change_focus_to_filter();
filter_emojis();
if (pressed_key === 8) {
// Handles Backspace.
new_query = old_query.slice(0, -1);
} else {
// Handles any printable character.
const key_str = String.fromCharCode(e.which);
new_query = old_query + key_str;
}
emoji_filter.val(new_query);
change_focus_to_filter();
filter_emojis();
}
}

View File

@@ -58,15 +58,17 @@ 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) {
result = message;
}
top_offset(message) < frb_bottom - date_bar_height_offset
) {
result = message;
}
if (result) {
return result;

View File

@@ -150,37 +150,35 @@ 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 (base === "streams") {
subs.change_state(section);
return;
}
if (base === "settings") {
if (!section) {
// We may be on a really old browser or somebody
// hand-typed a hash.
blueslip.warn("missing section for settings");
}
settings_panel_menu.normal_settings.activate_section_or_default(section);
return;
}
if (base === "organization") {
if (!section) {
// We may be on a really old browser or somebody
// hand-typed a hash.
blueslip.warn("missing section for organization");
}
settings_panel_menu.org_settings.activate_section_or_default(section);
return;
}
// TODO: handle other cases like internal settings
// changes.
if (coming_from_overlay && base === old_base) {
if (base === "streams") {
subs.change_state(section);
return;
}
if (base === "settings") {
if (!section) {
// We may be on a really old browser or somebody
// hand-typed a hash.
blueslip.warn("missing section for settings");
}
settings_panel_menu.normal_settings.activate_section_or_default(section);
return;
}
if (base === "organization") {
if (!section) {
// We may be on a really old browser or somebody
// hand-typed a hash.
blueslip.warn("missing section for organization");
}
settings_panel_menu.org_settings.activate_section_or_default(section);
return;
}
// TODO: handle other cases like internal settings
// changes.
return;
}
// It's not super likely that an overlay is already open,

View File

@@ -559,18 +559,14 @@ exports.process_hotkey = function (e, hotkey) {
return false;
}
if (event_name === "up_arrow") {
if (list_util.inside_list(e)) {
list_util.go_up(e);
return true;
}
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)) {
list_util.go_down(e);
return true;
}
if (event_name === "down_arrow" && list_util.inside_list(e)) {
list_util.go_down(e);
return true;
}
if (menu_dropdown_hotkeys.has(event_name)) {
@@ -618,11 +614,9 @@ 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")) {
// Ignore Backspace; don't navigate back a page.
return true;
}
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") {

View File

@@ -282,10 +282,8 @@ 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) {
store.$parent.find(".pill").last().trigger("focus");
}
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,

View File

@@ -35,14 +35,14 @@ const ls = {
let data = localStorage.getItem(key);
data = ls.parseJSON(data);
if (data) {
if (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) {
return data;
}
}
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.
(!ls.isExpired(data.expires) || data.expires === null)
) {
return data;
}
return undefined;

View File

@@ -168,14 +168,16 @@ 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()) {
changed_compose = true;
compose_state.topic(new_topic);
compose_fade.set_focused_recipient("stream");
}
}
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) {
@@ -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,45 +241,45 @@ 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)) {
let new_filter = current_filter;
if (new_filter && stream_changed) {
// TODO: This logic doesn't handle the
// case where we're a guest user and the
// message moves to a stream we cannot
// access, which would cause the
// stream_data lookup here to fail.
//
// The fix is likely somewhat involved, so punting for now.
const new_stream_name = stream_data.get_sub_by_id(new_stream_id).name;
new_filter = new_filter.filter_with_new_params({
operator: "stream",
operand: new_stream_name,
});
changed_narrow = true;
}
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
// case where we're a guest user and the
// message moves to a stream we cannot
// access, which would cause the
// stream_data lookup here to fail.
//
// The fix is likely somewhat involved, so punting for now.
const new_stream_name = stream_data.get_sub_by_id(new_stream_id).name;
new_filter = new_filter.filter_with_new_params({
operator: "stream",
operand: new_stream_name,
});
changed_narrow = true;
}
if (new_filter && topic_edited) {
new_filter = new_filter.filter_with_new_params({
operator: "topic",
operand: new_topic,
});
changed_narrow = true;
}
// NOTE: We should always be changing narrows after we finish
// updating the local data and UI. This avoids conflict
// with data fetched from the server (which is already updated)
// when we move to new narrow and what data is locally available.
if (changed_narrow) {
const operators = new_filter.operators();
const opts = {
trigger: "stream/topic change",
then_select_id: current_selected_id,
};
narrow.activate(operators, opts);
}
}
if (new_filter && topic_edited) {
new_filter = new_filter.filter_with_new_params({
operator: "topic",
operand: new_topic,
});
changed_narrow = true;
}
// NOTE: We should always be changing narrows after we finish
// updating the local data and UI. This avoids conflict
// with data fetched from the server (which is already updated)
// when we move to new narrow and what data is locally available.
if (changed_narrow) {
const operators = new_filter.operators();
const opts = {
trigger: "stream/topic change",
then_select_id: current_selected_id,
};
narrow.activate(operators, opts);
}
}

View File

@@ -69,10 +69,8 @@ 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) {
max_message_id = msg.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,10 +32,8 @@ 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) {
return false;
}
if (localstorage.supported() && ls.get("dontAskForNotifications") === true) {
return false;
}
return (

View File

@@ -3,15 +3,13 @@
$(() => {
// 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) === "#") {
/* We append the location.hash to the input field with name next so that URL can be
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 */
$("input[name='next']").each(function () {
const new_value = $(this).attr("value") + window.location.hash;
$(this).attr("value", new_value);
});
}
$("input[name='next']").each(function () {
const new_value = $(this).attr("value") + window.location.hash;
$(this).attr("value", new_value);
});
}
});

View File

@@ -95,23 +95,22 @@ $(() => {
// 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) === "#") {
document.email_form.action += window.location.hash;
}
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) === "#") {
/* 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 */
const email_formaction = $("#login_form").attr("action");
$("#login_form").attr("action", email_formaction + "/" + window.location.hash);
$(".social_login_form input[name='next']").attr("value", "/" + window.location.hash);
}
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 */
const email_formaction = $("#login_form").attr("action");
$("#login_form").attr("action", email_formaction + "/" + window.location.hash);
$(".social_login_form input[name='next']").attr("value", "/" + window.location.hash);
}
$("#send_confirm").validate({

View File

@@ -124,16 +124,12 @@ 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)) {
raw.active_timestamp = rec.timestamp;
}
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)) {
raw.idle_timestamp = rec.timestamp;
}
if (rec.status === "idle" && rec.timestamp > (raw.idle_timestamp || 0)) {
raw.idle_timestamp = rec.timestamp;
}
}

View File

@@ -114,21 +114,19 @@ 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
// ch = ch & 0x3ff;
ch -= 0xd800;
// ch = 0x10000 | (ch << 10) | (ch2 & 0x3ff);
ch = 0x10000 + ch * 0x400 + (ch2 - 0xdc00);
i += 1;
}
const ch2 = i + 1 < str.length ? str.charCodeAt(i + 1) : 0;
if (ch2 >= 0xdc00 && ch2 < 0xe000) {
// 0xdc00 <= ch2 < 0xe000
// ch = ch & 0x3ff;
ch -= 0xd800;
// ch = 0x10000 | (ch << 10) | (ch2 & 0x3ff);
ch = 0x10000 + ch * 0x400 + (ch2 - 0xdc00);
i += 1;
}
}
@@ -145,10 +143,8 @@ exports.get_direction = function (str) {
if (isolations === 0) {
return "rtl";
}
} else if (bidi_class === "L") {
if (isolations === 0) {
return "ltr";
}
} else if (bidi_class === "L" && isolations === 0) {
return "ltr";
}
}
return "ltr";

View File

@@ -702,16 +702,17 @@ 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) {
const subset_suggestions = get_operator_subset_suggestions(search_operators);
attacher.concat(subset_suggestions);
}
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,11 +97,9 @@ 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) {
focus_tab.add_a_new_bot_tab();
return;
}
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")) {

View File

@@ -154,12 +154,10 @@ class PerStreamHistory {
const existing = this.topics.get(topic_name);
if (existing) {
if (!existing.historical) {
// Trust out local data more, since it
// maintains counts.
continue;
}
if (existing && !existing.historical) {
// Trust out local data more, since it
// maintains counts.
continue;
}
// If we get here, we are either finding out about

View File

@@ -343,11 +343,9 @@ 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";
}
return "rejected";
}
const search_terms = search_util.get_search_terms(query.input);

View File

@@ -95,13 +95,15 @@ 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()) {
// 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"),
});
}
} 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,10 +74,8 @@ exports.initialize_kitchen_sink_stuff = function () {
if (message_viewport.at_top()) {
navigate.up();
}
} else if (delta > 0) {
if (message_viewport.at_bottom()) {
navigate.down();
}
} else if (delta > 0 && message_viewport.at_bottom()) {
navigate.down();
}
message_viewport.set_last_movement_direction(delta);