From a48ba1c77458da38583ee9cd488f70a59f6ee0fd Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Tue, 13 Feb 2018 09:54:04 -0500 Subject: [PATCH] Selectively run ESLint on `js/background.js` Enabling ESLint on a per function basis allows us to incrementally improve the codebase without requiring large and potentially risky refactorings. --- .eslintignore | 1 + js/background.js | 100 ++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 40 deletions(-) diff --git a/.eslintignore b/.eslintignore index f4b06432c7..8a994cf14e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -15,5 +15,6 @@ test/views/*.js /*.js # ES2015+ files +!js/background.js !main.js !prepare_build.js diff --git a/js/background.js b/js/background.js index a44473f8f7..90375af837 100644 --- a/js/background.js +++ b/js/background.js @@ -1,3 +1,17 @@ +/* eslint-disable */ + +/* eslint-env browser */ + +/* global Backbone: false */ +/* global $: false */ + +/* global ConversationController: false */ +/* global getAccountManager: false */ +/* global storage: false */ +/* global textsecure: false */ +/* global Whisper: false */ +/* global wrapDeferred: false */ + /* * vim: ts=4:sw=4:expandtab */ @@ -482,49 +496,55 @@ }); } - async function onMessageReceived(ev) { - const { data } = ev; + /* eslint-enable */ + /* jshint ignore:start */ + async function onMessageReceived(ev) { + const { data } = ev; - if (data.message.flags & textsecure.protobuf.DataMessage.Flags.PROFILE_KEY_UPDATE) { - const profileKey = data.message.profileKey.toArrayBuffer(); - const sender = ConversationController.getOrCreateAndWait(data.source, 'private'); - await sender.setProfileKey(profileKey); - // TODO: Is `ev.confirm` a `Promise`? Original code returned it: - return ev.confirm(); - } - - const message = initIncomingMessage(data); - const isDuplicate = await isMessageDuplicate(message); - if (isDuplicate) { - console.log('Received duplicate message', message.idForLogging()); - // TODO: Is `ev.confirm` a `Promise`? Original code didn’t return it: - return ev.confirm(); - } - - const { type, id } = data.message.group ? - { type: 'group', id: data.message.group.id } : - { type: 'private', id: data.source }; - - const processedData = Object.assign({}, data, { - message: Object.assign( - {}, - data.message, - { - attachments: await Promise.all( - data.message.attachments.map(Attachment.process) - ), - } - ), - }); - - await ConversationController.getOrCreateAndWait(id, type); - return message.handleDataMessage( - processedData.message, - ev.confirm, - { initialLoadComplete } - ); + // eslint-disable-next-line no-bitwise + if (data.message.flags & textsecure.protobuf.DataMessage.Flags.PROFILE_KEY_UPDATE) { + const profileKey = data.message.profileKey.toArrayBuffer(); + const sender = ConversationController.getOrCreateAndWait(data.source, 'private'); + await sender.setProfileKey(profileKey); + // TODO: Is `ev.confirm` a `Promise`? Original code returned it: + return ev.confirm(); } + const message = initIncomingMessage(data); + const isDuplicate = await isMessageDuplicate(message); + + if (isDuplicate) { + console.log('Received duplicate message', message.idForLogging()); + // TODO: Is `ev.confirm` a `Promise`? Original code didn’t return it: + return ev.confirm(); + } + + const { type, id } = data.message.group ? + { type: 'group', id: data.message.group.id } : + { type: 'private', id: data.source }; + + const processedData = Object.assign({}, data, { + message: Object.assign( + {}, + data.message, + { + attachments: await Promise.all( + data.message.attachments.map(Attachment.process) + ), + } + ), + }); + + await ConversationController.getOrCreateAndWait(id, type); + return message.handleDataMessage( + processedData.message, + ev.confirm, + { initialLoadComplete } + ); + } + /* jshint ignore:end */ + /* eslint-disable */ + function onSentMessage(ev) { var now = new Date().getTime(); var data = ev.data;