From 0f512e39250494c7399dadd3bf300256f9aed823 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:03:13 -0500 Subject: [PATCH 01/12] Whitelist `js/logging.js` for ESLint --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index 8773f6f9f9..206f78eca0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -16,6 +16,7 @@ test/views/*.js # ES2015+ files !js/background.js +!js/logging.js !js/models/conversations.js !js/views/attachment_view.js !js/views/conversation_search_view.js From 9d638797de0dc5f83cd9343391cb0940f502dabe Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 17:36:41 -0500 Subject: [PATCH 02/12] Whitelist `js/views/debug_log_view.js` for ESLint --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index 206f78eca0..69eb4216d0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -20,6 +20,7 @@ test/views/*.js !js/models/conversations.js !js/views/attachment_view.js !js/views/conversation_search_view.js +!js/views/debug_log_view.js !js/views/file_input_view.js !js/views/inbox_view.js !main.js From 5d6cd0ea39720d82308d3b8658955165f1ab0ee9 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:05:10 -0500 Subject: [PATCH 03/12] Apply ESLint auto-fixes: `js/logging.js` --- js/logging.js | 63 ++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/js/logging.js b/js/logging.js index cd66f9e976..678d55b98d 100644 --- a/js/logging.js +++ b/js/logging.js @@ -23,13 +23,11 @@ const LEVELS = { // Backwards-compatible logging, simple strings and no level (defaulted to INFO) function redactPhone(text) { - return text.replace(PHONE_REGEX, "+[REDACTED]$1"); + return text.replace(PHONE_REGEX, '+[REDACTED]$1'); } function redactGroup(text) { - return text.replace(GROUP_REGEX, function(match, before, id, after) { - return before + '[REDACTED]' + id.slice(-3) + after; - }); + return text.replace(GROUP_REGEX, (match, before, id, after) => `${before}[REDACTED]${id.slice(-3)}${after}`); } function now() { @@ -41,15 +39,14 @@ function log() { const args = Array.prototype.slice.call(arguments, 0); const consoleArgs = ['INFO ', now()].concat(args); - console._log.apply(console, consoleArgs); + console._log(...consoleArgs); // To avoid [Object object] in our log since console.log handles non-strings smoothly - const str = args.map(function(item) { + const str = args.map((item) => { if (typeof item !== 'string') { try { return JSON.stringify(item); - } - catch (e) { + } catch (e) { return item; } } @@ -71,14 +68,14 @@ if (window.console) { function getHeader() { let header = window.navigator.userAgent; - header += ' node/' + window.config.node_version; - header += ' env/' + window.config.environment; + header += ` node/${window.config.node_version}`; + header += ` env/${window.config.environment}`; return header; } function getLevel(level) { - var text = LEVELS[level]; + const text = LEVELS[level]; if (!text) { return BLANK_LEVEL; } @@ -87,7 +84,7 @@ function getLevel(level) { } function formatLine(entry) { - return getLevel(entry.level) + ' ' + entry.time + ' ' + entry.msg; + return `${getLevel(entry.level)} ${entry.time} ${entry.msg}`; } function format(entries) { @@ -95,35 +92,35 @@ function format(entries) { } function fetch() { - return new Promise(function(resolve) { + return new Promise(((resolve) => { ipc.send('fetch-log'); - ipc.on('fetched-log', function(event, text) { - var result = getHeader() + '\n' + format(text); + ipc.on('fetched-log', (event, text) => { + const result = `${getHeader()}\n${format(text)}`; resolve(result); }); - }); + })); } function publish(log) { log = log || fetch(); - return new Promise(function(resolve) { + return new Promise(((resolve) => { const payload = textsecure.utils.jsonThing({ files: { 'debugLog.txt': { - content: log - } - } + content: log, + }, + }, }); $.post('https://api.github.com/gists', payload) - .then(function(response) { + .then((response) => { console._log('Posted debug log to ', response.html_url); resolve(response.html_url); }) .fail(resolve); - }); + })); } @@ -136,11 +133,11 @@ const logger = bunyan.createLogger({ streams: [{ level: 'debug', stream: { - write: function(entry) { + write(entry) { console._log(formatLine(JSON.parse(entry))); - } - } - }] + }, + }, + }], }); // The Bunyan API: https://github.com/trentm/node-bunyan#log-method-api @@ -148,10 +145,10 @@ function logAtLevel() { const level = arguments[0]; const args = Array.prototype.slice.call(arguments, 1); - const ipcArgs = ['log-' + level].concat(args); - ipc.send.apply(ipc, ipcArgs); + const ipcArgs = [`log-${level}`].concat(args); + ipc.send(...ipcArgs); - logger[level].apply(logger, args); + logger[level](...args); } window.log = { @@ -165,11 +162,11 @@ window.log = { publish, }; -window.onerror = function(message, script, line, col, error) { +window.onerror = function (message, script, line, col, error) { const errorInfo = error && error.stack ? error.stack : JSON.stringify(error); - window.log.error('Top-level unhandled error: ' + errorInfo); + window.log.error(`Top-level unhandled error: ${errorInfo}`); }; -window.addEventListener('unhandledrejection', function(rejectionEvent) { - window.log.error('Top-level unhandled promise rejection: ' + rejectionEvent.reason); +window.addEventListener('unhandledrejection', (rejectionEvent) => { + window.log.error(`Top-level unhandled promise rejection: ${rejectionEvent.reason}`); }); From d4c9422a99c2436204edbb4b86f5802d75f9362d Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 17:54:33 -0500 Subject: [PATCH 04/12] Apply ESLint auto-fixes: `debug_log_view.js` --- js/views/debug_log_view.js | 118 ++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/js/views/debug_log_view.js b/js/views/debug_log_view.js index 357e2bf5ea..ed6b7c0b93 100644 --- a/js/views/debug_log_view.js +++ b/js/views/debug_log_view.js @@ -2,65 +2,65 @@ * vim: ts=4:sw=4:expandtab */ (function () { - 'use strict'; - window.Whisper = window.Whisper || {}; + 'use strict'; - Whisper.DebugLogLinkView = Whisper.View.extend({ - templateName: 'debug-log-link', - initialize: function(options) { - this.url = options.url; - }, - render_attributes: function() { - return { - url: this.url, - reportIssue: i18n('reportIssue') - }; - } - }); - Whisper.DebugLogView = Whisper.View.extend({ - templateName: 'debug-log', - className: 'debug-log modal', - initialize: function() { - this.render(); - this.$('textarea').val(i18n('loading')); + window.Whisper = window.Whisper || {}; - window.log.fetch().then(function(text) { - this.$('textarea').val(text); - }.bind(this)); - }, - events: { - 'click .submit': 'submit', - 'click .close': 'close' - }, - render_attributes: { - title: i18n('submitDebugLog'), - cancel: i18n('cancel'), - submit: i18n('submit'), - close: i18n('gotIt'), - debugLogExplanation: i18n('debugLogExplanation') - }, - close: function(e) { - e.preventDefault(); - this.remove(); - }, - submit: function(e) { - e.preventDefault(); - var text = this.$('textarea').val(); - if (text.length === 0) { - return; - } - log.publish(text).then(function(url) { - var view = new Whisper.DebugLogLinkView({ - url: url, - el: this.$('.result') - }); - this.$('.loading').removeClass('loading'); - view.render(); - this.$('.link').focus().select(); - }.bind(this)); - this.$('.buttons, textarea').remove(); - this.$('.result').addClass('loading'); - } - }); + Whisper.DebugLogLinkView = Whisper.View.extend({ + templateName: 'debug-log-link', + initialize(options) { + this.url = options.url; + }, + render_attributes() { + return { + url: this.url, + reportIssue: i18n('reportIssue'), + }; + }, + }); + Whisper.DebugLogView = Whisper.View.extend({ + templateName: 'debug-log', + className: 'debug-log modal', + initialize() { + this.render(); + this.$('textarea').val(i18n('loading')); -})(); + window.log.fetch().then((text) => { + this.$('textarea').val(text); + }); + }, + events: { + 'click .submit': 'submit', + 'click .close': 'close', + }, + render_attributes: { + title: i18n('submitDebugLog'), + cancel: i18n('cancel'), + submit: i18n('submit'), + close: i18n('gotIt'), + debugLogExplanation: i18n('debugLogExplanation'), + }, + close(e) { + e.preventDefault(); + this.remove(); + }, + submit(e) { + e.preventDefault(); + const text = this.$('textarea').val(); + if (text.length === 0) { + return; + } + log.publish(text).then((url) => { + const view = new Whisper.DebugLogLinkView({ + url, + el: this.$('.result'), + }); + this.$('.loading').removeClass('loading'); + view.render(); + this.$('.link').focus().select(); + }); + this.$('.buttons, textarea').remove(); + this.$('.result').addClass('loading'); + }, + }); +}()); From 7db44e35bd648aa4c553f1b1ac51def43f8a5639 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:05:43 -0500 Subject: [PATCH 05/12] Allow `node` environment for `js/logging.js` --- js/logging.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/logging.js b/js/logging.js index 678d55b98d..e4576b241f 100644 --- a/js/logging.js +++ b/js/logging.js @@ -1,3 +1,5 @@ +/* eslint-env node */ + const electron = require('electron'); const bunyan = require('bunyan'); const _ = require('lodash'); From 090345523f48b135804c2d703c31ddcf5ef49f61 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:07:06 -0500 Subject: [PATCH 06/12] Disable ESLint `strict` rule for module `js/logging.js` acts as a module even though it lives in `js/*`. --- js/logging.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/logging.js b/js/logging.js index e4576b241f..a00f7e02ef 100644 --- a/js/logging.js +++ b/js/logging.js @@ -1,5 +1,7 @@ /* eslint-env node */ +/* eslint strict: ['error', 'never'] */ + const electron = require('electron'); const bunyan = require('bunyan'); const _ = require('lodash'); From 6f7f55f78315e113e57b1926f28f4a63039fe2cf Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:13:54 -0500 Subject: [PATCH 07/12] Whitelist globals --- js/logging.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js/logging.js b/js/logging.js index a00f7e02ef..f6aa4ebd33 100644 --- a/js/logging.js +++ b/js/logging.js @@ -2,6 +2,9 @@ /* eslint strict: ['error', 'never'] */ +/* global $: false */ +/* global textsecure: false */ + const electron = require('electron'); const bunyan = require('bunyan'); const _ = require('lodash'); From b3a3729261224165597f13c817080a947b77b7ed Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 16:14:22 -0500 Subject: [PATCH 08/12] Fix lint errors --- js/logging.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/js/logging.js b/js/logging.js index f6aa4ebd33..43ecaad0fa 100644 --- a/js/logging.js +++ b/js/logging.js @@ -34,7 +34,10 @@ function redactPhone(text) { } function redactGroup(text) { - return text.replace(GROUP_REGEX, (match, before, id, after) => `${before}[REDACTED]${id.slice(-3)}${after}`); + return text.replace( + GROUP_REGEX, + (match, before, id, after) => `${before}[REDACTED]${id.slice(-3)}${after}` + ); } function now() { @@ -42,9 +45,7 @@ function now() { return date.toJSON(); } -function log() { - const args = Array.prototype.slice.call(arguments, 0); - +function log(...args) { const consoleArgs = ['INFO ', now()].concat(args); console._log(...consoleArgs); @@ -53,7 +54,7 @@ function log() { if (typeof item !== 'string') { try { return JSON.stringify(item); - } catch (e) { + } catch (error) { return item; } } @@ -109,18 +110,19 @@ function fetch() { })); } -function publish(log) { - log = log || fetch(); +function publish(rawContent) { + const content = rawContent || fetch(); return new Promise(((resolve) => { const payload = textsecure.utils.jsonThing({ files: { 'debugLog.txt': { - content: log, + content, }, }, }); + // eslint-disable-next-line more/no-then $.post('https://api.github.com/gists', payload) .then((response) => { console._log('Posted debug log to ', response.html_url); @@ -148,10 +150,7 @@ const logger = bunyan.createLogger({ }); // The Bunyan API: https://github.com/trentm/node-bunyan#log-method-api -function logAtLevel() { - const level = arguments[0]; - const args = Array.prototype.slice.call(arguments, 1); - +function logAtLevel(level, ...args) { const ipcArgs = [`log-${level}`].concat(args); ipc.send(...ipcArgs); @@ -169,7 +168,7 @@ window.log = { publish, }; -window.onerror = function (message, script, line, col, error) { +window.onerror = (message, script, line, col, error) => { const errorInfo = error && error.stack ? error.stack : JSON.stringify(error); window.log.error(`Top-level unhandled error: ${errorInfo}`); }; From 325c0628cd6ae1e3e1cc1c940757e2e800e7c703 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 17:55:07 -0500 Subject: [PATCH 09/12] Remove Vim modeline --- js/views/debug_log_view.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/js/views/debug_log_view.js b/js/views/debug_log_view.js index ed6b7c0b93..e61f46f1d8 100644 --- a/js/views/debug_log_view.js +++ b/js/views/debug_log_view.js @@ -1,6 +1,3 @@ -/* - * vim: ts=4:sw=4:expandtab - */ (function () { 'use strict'; From 168788600a32081782c0085b1dabd157e7443ec4 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 17:57:23 -0500 Subject: [PATCH 10/12] Fix lint errors --- js/views/debug_log_view.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/js/views/debug_log_view.js b/js/views/debug_log_view.js index e61f46f1d8..032935a291 100644 --- a/js/views/debug_log_view.js +++ b/js/views/debug_log_view.js @@ -1,3 +1,9 @@ +/* eslint-env browser */ + +/* global i18n: false */ +/* global Whisper: false */ + +// eslint-disable-next-line func-names (function () { 'use strict'; @@ -22,6 +28,7 @@ this.render(); this.$('textarea').val(i18n('loading')); + // eslint-disable-next-line more/no-then window.log.fetch().then((text) => { this.$('textarea').val(text); }); @@ -47,7 +54,8 @@ if (text.length === 0) { return; } - log.publish(text).then((url) => { + // eslint-disable-next-line more/no-then + window.log.publish(text).then((url) => { const view = new Whisper.DebugLogLinkView({ url, el: this.$('.result'), From f6fd979ccb48d60e315ab913ebf92cf262bd1e75 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 5 Mar 2018 17:58:05 -0500 Subject: [PATCH 11/12] Remove explicit ESLint `browser` directives --- js/background.js | 2 -- js/views/attachment_view.js | 2 -- js/views/debug_log_view.js | 2 -- 3 files changed, 6 deletions(-) diff --git a/js/background.js b/js/background.js index 0c6e733f5e..506874823f 100644 --- a/js/background.js +++ b/js/background.js @@ -1,7 +1,5 @@ /* eslint-disable */ -/* eslint-env browser */ - /* global Backbone: false */ /* global $: false */ diff --git a/js/views/attachment_view.js b/js/views/attachment_view.js index b429872845..861ab218f3 100644 --- a/js/views/attachment_view.js +++ b/js/views/attachment_view.js @@ -1,5 +1,3 @@ -/* eslint-env browser */ - /* global $: false */ /* global _: false */ /* global Backbone: false */ diff --git a/js/views/debug_log_view.js b/js/views/debug_log_view.js index 032935a291..0abdea58f6 100644 --- a/js/views/debug_log_view.js +++ b/js/views/debug_log_view.js @@ -1,5 +1,3 @@ -/* eslint-env browser */ - /* global i18n: false */ /* global Whisper: false */ From 969127a72a8e1bc4effccaece7c14968a78e938f Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Thu, 8 Mar 2018 13:45:22 -0500 Subject: [PATCH 12/12] Remove triple parens --- js/logging.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/logging.js b/js/logging.js index 43ecaad0fa..d1961226f8 100644 --- a/js/logging.js +++ b/js/logging.js @@ -100,20 +100,20 @@ function format(entries) { } function fetch() { - return new Promise(((resolve) => { + return new Promise((resolve) => { ipc.send('fetch-log'); ipc.on('fetched-log', (event, text) => { const result = `${getHeader()}\n${format(text)}`; resolve(result); }); - })); + }); } function publish(rawContent) { const content = rawContent || fetch(); - return new Promise(((resolve) => { + return new Promise((resolve) => { const payload = textsecure.utils.jsonThing({ files: { 'debugLog.txt': { @@ -129,7 +129,7 @@ function publish(rawContent) { resolve(response.html_url); }) .fail(resolve); - })); + }); }