From 9beea2cbd3b0cb4857a750a03ba52e6d69789662 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Thu, 5 Sep 2019 11:34:46 +1000 Subject: [PATCH] Enforce curlies completely and lint with new settings --- .eslintrc.js | 3 +- app/profile_images.js | 4 +- js/blocked_number_controller.js | 4 +- js/models/conversations.js | 88 ++++++++++++++----- js/models/messages.js | 36 ++++++-- js/modules/data.js | 4 +- js/modules/loki_p2p_api.js | 4 +- js/modules/loki_public_chat_api.js | 4 +- js/modules/loki_rss_api.js | 4 +- js/modules/loki_snode_api.js | 5 +- js/views/blocked_number_view.js | 12 ++- js/views/conversation_view.js | 4 +- libloki/api.js | 4 +- libloki/modules/local_loki_server.js | 7 +- libloki/proof-of-work.js | 12 ++- libtextsecure/helpers.js | 9 +- libtextsecure/key_worker.js | 4 +- libtextsecure/message_receiver.js | 16 +++- libtextsecure/storage.js | 8 +- libtextsecure/storage/user.js | 8 +- .../test/in_memory_signal_protocol_store.js | 4 +- preload.js | 4 +- 22 files changed, 184 insertions(+), 64 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 720b5cb6499..6451fae054a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -22,7 +22,8 @@ module.exports = { ], // Enforce curlies always - curly: 'error', + curly: ['error', 'all'], + 'brace-style': ['error', '1tbs'], // prevents us from accidentally checking in exclusive tests (`.only`): 'mocha/no-exclusive-tests': 'error', diff --git a/app/profile_images.js b/app/profile_images.js index 339f80fed63..ee39ee3a9ff 100644 --- a/app/profile_images.js +++ b/app/profile_images.js @@ -15,7 +15,9 @@ const hasImage = pubKey => fs.existsSync(getImagePath(pubKey)); const getImagePath = pubKey => `${PATH}/${pubKey}.png`; const getOrCreateImagePath = pubKey => { // If the image doesn't exist then create it - if (!hasImage(pubKey)) return generateImage(pubKey); + if (!hasImage(pubKey)) { + return generateImage(pubKey); + } return getImagePath(pubKey); }; diff --git a/js/blocked_number_controller.js b/js/blocked_number_controller.js index 918a21749b2..7e1a30ab6b5 100644 --- a/js/blocked_number_controller.js +++ b/js/blocked_number_controller.js @@ -42,7 +42,9 @@ storage.addBlockedNumber(number); // Make sure we don't add duplicates - if (blockedNumbers.getModel(number)) return; + if (blockedNumbers.getModel(number)) { + return; + } blockedNumbers.add({ number }); }, diff --git a/js/models/conversations.js b/js/models/conversations.js index 52cdaf6105a..dff8043fa7c 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -309,7 +309,9 @@ }, async updateProfileAvatar() { - if (this.isRss()) return; + if (this.isRss()) { + return; + } const path = profileImages.getOrCreateImagePath(this.id); await this.setProfileAvatar(path); }, @@ -358,7 +360,9 @@ // Get messages with the given timestamp _getMessagesWithTimestamp(pubKey, timestamp) { - if (this.id !== pubKey) return []; + if (this.id !== pubKey) { + return []; + } // Go through our messages and find the one that we need to update return this.messageCollection.models.filter( @@ -414,7 +418,9 @@ // Get the pending friend requests that match the direction // If no direction is supplied then return all pending friend requests return messages.models.filter(m => { - if (!status.includes(m.get('friendStatus'))) return false; + if (!status.includes(m.get('friendStatus'))) { + return false; + } return direction === null || m.get('direction') === direction; }); }, @@ -424,7 +430,9 @@ addSingleMessage(message, setToExpire = true) { const model = this.messageCollection.add(message, { merge: true }); - if (setToExpire) model.setToExpire(); + if (setToExpire) { + model.setToExpire(); + } return model; }, format() { @@ -680,7 +688,9 @@ }, async setFriendRequestStatus(newStatus) { // Ensure that the new status is a valid FriendStatusEnum value - if (!(newStatus in Object.values(FriendRequestStatusEnum))) return; + if (!(newStatus in Object.values(FriendRequestStatusEnum))) { + return; + } if ( this.ourNumber === this.id && newStatus !== FriendRequestStatusEnum.friends @@ -698,11 +708,15 @@ async respondToAllFriendRequests(options) { const { response, status, direction = null } = options; // Ignore if no response supplied - if (!response) return; + if (!response) { + return; + } const pending = await this.getFriendRequests(direction, status); await Promise.all( pending.map(async request => { - if (request.hasErrors()) return; + if (request.hasErrors()) { + return; + } request.set({ friendStatus: response }); await window.Signal.Data.saveMessage(request.attributes, { @@ -736,7 +750,9 @@ }, // We have accepted an incoming friend request async onAcceptFriendRequest() { - if (this.unlockTimer) clearTimeout(this.unlockTimer); + if (this.unlockTimer) { + clearTimeout(this.unlockTimer); + } if (this.hasReceivedFriendRequest()) { this.setFriendRequestStatus(FriendRequestStatusEnum.friends); await this.respondToAllFriendRequests({ @@ -752,7 +768,9 @@ if (this.isFriend()) { return false; } - if (this.unlockTimer) clearTimeout(this.unlockTimer); + if (this.unlockTimer) { + clearTimeout(this.unlockTimer); + } if (this.hasSentFriendRequest()) { this.setFriendRequestStatus(FriendRequestStatusEnum.friends); await this.respondToAllFriendRequests({ @@ -766,9 +784,13 @@ }, async onFriendRequestTimeout() { // Unset the timer - if (this.unlockTimer) clearTimeout(this.unlockTimer); + if (this.unlockTimer) { + clearTimeout(this.unlockTimer); + } this.unlockTimer = null; - if (this.isFriend()) return; + if (this.isFriend()) { + return; + } // Set the unlock timestamp to null if (this.get('unlockTimestamp')) { @@ -822,7 +844,9 @@ await this.setFriendRequestStatus(FriendRequestStatusEnum.requestSent); }, setFriendRequestExpiryTimeout() { - if (this.isFriend()) return; + if (this.isFriend()) { + return; + } const unlockTimestamp = this.get('unlockTimestamp'); if (unlockTimestamp && !this.unlockTimer) { const delta = Math.max(unlockTimestamp - Date.now(), 0); @@ -1080,12 +1104,18 @@ }, validateNumber() { - if (!this.id) return 'Invalid ID'; - if (!this.isPrivate()) return null; + if (!this.id) { + return 'Invalid ID'; + } + if (!this.isPrivate()) { + return null; + } // Check if it's hex const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); - if (!isHex) return 'Invalid Hex ID'; + if (!isHex) { + return 'Invalid Hex ID'; + } // Check if the pubkey length is 33 and leading with 05 or of length 32 const len = this.id.length; @@ -1216,7 +1246,9 @@ async sendMessage(body, attachments, quote, preview) { // Input should be blocked if there is a pending friend request - if (this.isPendingFriendRequest()) return; + if (this.isPendingFriendRequest()) { + return; + } this.clearTypingTimers(); @@ -1277,7 +1309,9 @@ // If the requests didn't error then don't add a new friend request // because one of them was sent successfully - if (friendRequestSent) return null; + if (friendRequestSent) { + return null; + } } await this.setFriendRequestStatus( FriendRequestStatusEnum.pendingSend @@ -1762,7 +1796,9 @@ }, async setSessionResetStatus(newStatus) { // Ensure that the new status is a valid SessionResetEnum value - if (!(newStatus in Object.values(SessionResetEnum))) return; + if (!(newStatus in Object.values(SessionResetEnum))) { + return; + } if (this.get('sessionResetStatus') !== newStatus) { this.set({ sessionResetStatus: newStatus }); await window.Signal.Data.updateConversation(this.id, this.attributes, { @@ -2014,7 +2050,9 @@ async setNickname(nickname) { const trimmed = nickname && nickname.trim(); - if (this.get('nickname') === trimmed) return; + if (this.get('nickname') === trimmed) { + return; + } this.set({ nickname: trimmed }); await window.Signal.Data.updateConversation(this.id, this.attributes, { @@ -2467,7 +2505,9 @@ const avatar = this.get('avatar') || this.get('profileAvatar'); if (avatar) { - if (avatar.path) return getAbsoluteAttachmentPath(avatar.path); + if (avatar.path) { + return getAbsoluteAttachmentPath(avatar.path); + } return avatar; } @@ -2511,7 +2551,9 @@ } return this.notifyFriendRequest(message.get('source'), 'requested'); } - if (!message.isIncoming()) return Promise.resolve(); + if (!message.isIncoming()) { + return Promise.resolve(); + } const conversationId = this.id; return ConversationController.getOrCreateAndWait( @@ -2544,7 +2586,9 @@ // Notification for friend request received async notifyFriendRequest(source, type) { // Data validation - if (!source) throw new Error('Invalid source'); + if (!source) { + throw new Error('Invalid source'); + } if (!['accepted', 'requested'].includes(type)) { throw new Error('Type must be accepted or requested.'); } diff --git a/js/models/messages.js b/js/models/messages.js index b6769e8cc2e..8f228329342 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -322,7 +322,9 @@ getNotificationText() { const description = this.getDescription(); if (description) { - if (this.isFriendRequest()) return `Friend Request: ${description}`; + if (this.isFriendRequest()) { + return `Friend Request: ${description}`; + } return description; } if (this.get('attachments').length > 0) { @@ -432,7 +434,9 @@ }, async acceptFriendRequest() { - if (this.get('friendStatus') !== 'pending') return; + if (this.get('friendStatus') !== 'pending') { + return; + } const conversation = this.getConversation(); this.set({ friendStatus: 'accepted' }); @@ -442,7 +446,9 @@ conversation.onAcceptFriendRequest(); }, async declineFriendRequest() { - if (this.get('friendStatus') !== 'pending') return; + if (this.get('friendStatus') !== 'pending') { + return; + } const conversation = this.getConversation(); this.set({ friendStatus: 'declined' }); @@ -591,7 +597,9 @@ return 'sent'; } const calculatingPoW = this.get('calculatingPoW'); - if (calculatingPoW) return 'pow'; + if (calculatingPoW) { + return 'pow'; + } return 'sending'; }, @@ -1235,7 +1243,9 @@ return null; }, async setCalculatingPoW() { - if (this.calculatingPoW) return; + if (this.calculatingPoW) { + return; + } this.set({ calculatingPoW: true, @@ -1246,7 +1256,9 @@ }); }, async setIsP2p(isP2p) { - if (_.isEqual(this.get('isP2p'), isP2p)) return; + if (_.isEqual(this.get('isP2p'), isP2p)) { + return; + } this.set({ isP2p: !!isP2p, @@ -1260,7 +1272,9 @@ return this.get('serverId'); }, async setServerId(serverId) { - if (_.isEqual(this.get('serverId'), serverId)) return; + if (_.isEqual(this.get('serverId'), serverId)) { + return; + } this.set({ serverId, @@ -1271,7 +1285,9 @@ }); }, async setIsPublic(isPublic) { - if (_.isEqual(this.get('isPublic'), isPublic)) return; + if (_.isEqual(this.get('isPublic'), isPublic)) { + return; + } this.set({ isPublic: !!isPublic, @@ -2098,7 +2114,9 @@ // Need to do this here because the conversation has already changed states if (autoAccept) { await conversation.notifyFriendRequest(source, 'accepted'); - } else await conversation.notify(message); + } else { + await conversation.notify(message); + } } confirm(); diff --git a/js/modules/data.js b/js/modules/data.js index c410f946444..14c9dddacdf 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -663,7 +663,9 @@ async function getAllSessions(id) { // Conversation function setifyProperty(data, propertyName) { - if (!data) return data; + if (!data) { + return data; + } const returnData = { ...data }; if (Array.isArray(returnData[propertyName])) { returnData[propertyName] = new Set(returnData[propertyName]); diff --git a/js/modules/loki_p2p_api.js b/js/modules/loki_p2p_api.js index 370305649cb..7f9900091d3 100644 --- a/js/modules/loki_p2p_api.js +++ b/js/modules/loki_p2p_api.js @@ -70,7 +70,9 @@ class LokiP2pAPI extends EventEmitter { } getContactP2pDetails(pubKey) { - if (!this.contactP2pDetails[pubKey]) return null; + if (!this.contactP2pDetails[pubKey]) { + return null; + } return { ...this.contactP2pDetails[pubKey] }; } diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index 07629bf187b..51ac4852013 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -458,7 +458,9 @@ class LokiPublicChannelAPI { // if any problems, abort out if (res.err || !res.response) { - if (res.err) log.error(`Error ${res.err}`); + if (res.err) { + log.error(`Error ${res.err}`); + } break; } diff --git a/js/modules/loki_rss_api.js b/js/modules/loki_rss_api.js index b220b1bd2a6..6a7c3f23a0f 100644 --- a/js/modules/loki_rss_api.js +++ b/js/modules/loki_rss_api.js @@ -73,7 +73,9 @@ class LokiRssAPI extends EventEmitter { log.error('xmlerror', e); success = false; } - if (!success) return; + if (!success) { + return; + } const feedObj = xml2json(feedDOM); let receivedAt = new Date().getTime(); diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index d46b2755d26..398a5960277 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -48,8 +48,9 @@ class LokiSnodeAPI { const upnpClient = natUpnp.createClient(); return new Promise((resolve, reject) => { upnpClient.externalIp((err, ip) => { - if (err) reject(err); - else { + if (err) { + reject(err); + } else { resolve(ip); } }); diff --git a/js/views/blocked_number_view.js b/js/views/blocked_number_view.js index 3b8696355d6..cc14c1c48a9 100644 --- a/js/views/blocked_number_view.js +++ b/js/views/blocked_number_view.js @@ -39,7 +39,9 @@ }, onUnblock() { const number = this.$('select option:selected').val(); - if (!number) return; + if (!number) { + return; + } if (BlockedNumberController.isBlocked(number)) { BlockedNumberController.unblock(number); @@ -73,7 +75,9 @@ }, truncate(string, limit) { // Make sure an element and number of items to truncate is provided - if (!string || !limit) return string; + if (!string || !limit) { + return string; + } // Get the inner content of the element let content = string.trim(); @@ -84,7 +88,9 @@ // Convert the array of words back into a string // If there's content to add after it, add it - if (string.length > limit) content = `${content}...`; + if (string.length > limit) { + content = `${content}...`; + } return content; }, diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 94299aab8ee..4064d358fce 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -391,7 +391,9 @@ }, onChangePlaceholder(type) { - if (!this.$messageField) return; + if (!this.$messageField) { + return; + } let placeholder; switch (type) { case 'friend-request': diff --git a/libloki/api.js b/libloki/api.js index e25d69cba4b..ec5ef568807 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -14,7 +14,9 @@ ); await Promise.all( friendKeys.map(async pubKey => { - if (pubKey === textsecure.storage.user.getNumber()) return; + if (pubKey === textsecure.storage.user.getNumber()) { + return; + } try { await sendOnlineBroadcastMessage(pubKey); } catch (e) { diff --git a/libloki/modules/local_loki_server.js b/libloki/modules/local_loki_server.js index 42448438572..aac6419fddb 100644 --- a/libloki/modules/local_loki_server.js +++ b/libloki/modules/local_loki_server.js @@ -148,8 +148,11 @@ class LocalLokiServer extends EventEmitter { ttl, }, err => { - if (err) reject(err); - else resolve(); + if (err) { + reject(err); + } else { + resolve(); + } } ); }); diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index c6a3a4b1e29..b537e17af31 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -46,11 +46,17 @@ const pow = { // Compare two Uint8Arrays, return true if arr1 is > arr2 greaterThan(arr1, arr2) { // Early exit if lengths are not equal. Should never happen - if (arr1.length !== arr2.length) return false; + if (arr1.length !== arr2.length) { + return false; + } for (let i = 0, len = arr1.length; i < len; i += 1) { - if (arr1[i] > arr2[i]) return true; - if (arr1[i] < arr2[i]) return false; + if (arr1[i] > arr2[i]) { + return true; + } + if (arr1[i] < arr2[i]) { + return false; + } } return false; }, diff --git a/libtextsecure/helpers.js b/libtextsecure/helpers.js index a53a811f892..31a6605d01b 100644 --- a/libtextsecure/helpers.js +++ b/libtextsecure/helpers.js @@ -51,8 +51,9 @@ window.textsecure.utils = (() => { *** JSON'ing Utilities *** ************************* */ function ensureStringed(thing) { - if (getStringable(thing)) return getString(thing); - else if (thing instanceof Array) { + if (getStringable(thing)) { + return getString(thing); + } else if (thing instanceof Array) { const res = []; for (let i = 0; i < thing.length; i += 1) { res[i] = ensureStringed(thing[i]); @@ -60,7 +61,9 @@ window.textsecure.utils = (() => { return res; } else if (thing === Object(thing)) { const res = {}; - for (const key in thing) res[key] = ensureStringed(thing[key]); + for (const key in thing) { + res[key] = ensureStringed(thing[key]); + } return res; } else if (thing === null) { return null; diff --git a/libtextsecure/key_worker.js b/libtextsecure/key_worker.js index ba8f32155ae..96f3c275a38 100644 --- a/libtextsecure/key_worker.js +++ b/libtextsecure/key_worker.js @@ -33,7 +33,9 @@ window.textsecure.storage.impl = { *** Override Storage Routines *** **************************** */ put(key, value) { - if (value === undefined) throw new Error('Tried to store undefined'); + if (value === undefined) { + throw new Error('Tried to store undefined'); + } store[key] = value; postMessage({ method: 'set', key, value }); }, diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 1d1cebcf86b..c7454c8e5f5 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -730,9 +730,13 @@ MessageReceiver.prototype.extend({ } const getCurrentSessionBaseKey = async () => { const record = await sessionCipher.getRecord(address.toString()); - if (!record) return null; + if (!record) { + return null; + } const openSession = record.getOpenSession(); - if (!openSession) return null; + if (!openSession) { + return null; + } const { baseKey } = openSession.indexInfo; return baseKey; }; @@ -741,7 +745,9 @@ MessageReceiver.prototype.extend({ }; const restoreActiveSession = async () => { const record = await sessionCipher.getRecord(address.toString()); - if (!record) return; + if (!record) { + return; + } record.archiveCurrentState(); const sessionToRestore = record.sessions[this.activeSessionBaseKey]; record.promoteState(sessionToRestore); @@ -753,7 +759,9 @@ MessageReceiver.prototype.extend({ }; const deleteAllSessionExcept = async sessionBaseKey => { const record = await sessionCipher.getRecord(address.toString()); - if (!record) return; + if (!record) { + return; + } const sessionToKeep = record.sessions[sessionBaseKey]; record.sessions = {}; record.updateSessionState(sessionToKeep); diff --git a/libtextsecure/storage.js b/libtextsecure/storage.js index dff324cbf36..d59b83e2767 100644 --- a/libtextsecure/storage.js +++ b/libtextsecure/storage.js @@ -14,13 +14,17 @@ *** Base Storage Routines *** **************************** */ put(key, value) { - if (value === undefined) throw new Error('Tried to store undefined'); + if (value === undefined) { + throw new Error('Tried to store undefined'); + } localStorage.setItem(`${key}`, textsecure.utils.jsonThing(value)); }, get(key, defaultValue) { const value = localStorage.getItem(`${key}`); - if (value === null) return defaultValue; + if (value === null) { + return defaultValue; + } return JSON.parse(value); }, diff --git a/libtextsecure/storage/user.js b/libtextsecure/storage/user.js index b6330acf87a..3e17b80af7b 100644 --- a/libtextsecure/storage/user.js +++ b/libtextsecure/storage/user.js @@ -18,13 +18,17 @@ getNumber() { const numberId = textsecure.storage.get('number_id'); - if (numberId === undefined) return undefined; + if (numberId === undefined) { + return undefined; + } return textsecure.utils.unencodeNumber(numberId)[0]; }, getDeviceId() { const numberId = textsecure.storage.get('number_id'); - if (numberId === undefined) return undefined; + if (numberId === undefined) { + return undefined; + } return textsecure.utils.unencodeNumber(numberId)[1]; }, diff --git a/libtextsecure/test/in_memory_signal_protocol_store.js b/libtextsecure/test/in_memory_signal_protocol_store.js index 41c538c4a21..a594a6d79ff 100644 --- a/libtextsecure/test/in_memory_signal_protocol_store.js +++ b/libtextsecure/test/in_memory_signal_protocol_store.js @@ -171,7 +171,9 @@ SignalProtocolStore.prototype = { async loadPreKeyForContact(contactPubKey) { return new Promise(resolve => { const key = this.get(`25519KeypreKey${contactPubKey}`); - if (!key) resolve(undefined); + if (!key) { + resolve(undefined); + } resolve({ pubKey: key.publicKey, privKey: key.privateKey, diff --git a/preload.js b/preload.js index d1d26243b2e..fc5b12ca0db 100644 --- a/preload.js +++ b/preload.js @@ -415,7 +415,9 @@ contextMenu({ }, menu: (actions, params) => { // If it's not a QR then show the default options - if (!isQR(params)) return actions; + if (!isQR(params)) { + return actions; + } return [actions.copyImage()]; },