diff --git a/.jscsrc b/.jscsrc index 689d7b0525f1..9c02ffffbff0 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,4 +1,33 @@ { "disallowMixedSpacesAndTabs": true, - "disallowTrailingWhitespace": true + "disallowTrailingWhitespace": true, + "disallowNewlineBeforeBlockStatements": true, + "requireCommaBeforeLineBreak": true, + "requireSemicolons": true, + "requireSpaceBeforeBlockStatements": true, + "disallowSpacesInNamedFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + "requireSpacesInNamedFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireCurlyBraces": [ + "if", + "else", + "for", + "while", + "do", + "try", + "catch" + ], + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "case", + "try", + "typeof", + "return" + ] } diff --git a/Gruntfile.js b/Gruntfile.js index 029784c38c99..f798fb910957 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -142,6 +142,7 @@ module.exports = function(grunt) { '!js/components.js', 'test/**/*.js', '!test/blanket_mocha.js', + '!test/test.js', ] } }, diff --git a/js/axolotl_store.js b/js/axolotl_store.js index e9e67217878c..b47fc168b9eb 100644 --- a/js/axolotl_store.js +++ b/js/axolotl_store.js @@ -11,11 +11,13 @@ thing.__proto__ == StaticByteBufferProto)); } function convertToArrayBuffer(thing) { - if (thing === undefined) + if (thing === undefined) { return undefined; + } if (thing === Object(thing)) { - if (thing.__proto__ == StaticArrayBufferProto) + if (thing.__proto__ == StaticArrayBufferProto) { return thing; + } //TODO: Several more cases here... } @@ -23,22 +25,25 @@ // Assuming Uint16Array from curve25519 var res = new ArrayBuffer(thing.length * 2); var uint = new Uint16Array(res); - for (var i = 0; i < thing.length; i++) + for (var i = 0; i < thing.length; i++) { uint[i] = thing[i]; + } return res; } var str; - if (isStringable(thing)) + if (isStringable(thing)) { str = stringObject(thing); - else if (typeof thing == "string") + } else if (typeof thing == "string") { str = thing; - else + } else { throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer"); + } var res = new ArrayBuffer(str.length); var uint = new Uint8Array(res); - for (var i = 0; i < str.length; i++) + for (var i = 0; i < str.length; i++) { uint[i] = str.charCodeAt(i); + } return res; } @@ -168,8 +173,9 @@ getSession: function(encodedNumber) { console.log('getSession', encodedNumber); - if (encodedNumber === null || encodedNumber === undefined) + if (encodedNumber === null || encodedNumber === undefined) { throw new Error("Tried to get session for undefined/null number"); + } return new Promise(function(resolve) { var session = new Session({id: encodedNumber}); console.log('fetching session for', encodedNumber); @@ -182,8 +188,9 @@ }, putSession: function(encodedNumber, record) { console.log('putSession', encodedNumber); - if (encodedNumber === null || encodedNumber === undefined) + if (encodedNumber === null || encodedNumber === undefined) { throw new Error("Tried to put session for undefined/null number"); + } return new Promise(function(resolve) { var number = textsecure.utils.unencodeNumber(encodedNumber)[0]; var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]); @@ -204,8 +211,9 @@ }); }, getDeviceIds: function(number) { - if (number === null || number === undefined) + if (number === null || number === undefined) { throw new Error("Tried to get device ids for undefined/null number"); + } return new Promise(function(resolve) { var sessions = new SessionCollection(); sessions.fetchSessionsForNumber(number).always(function() { @@ -222,13 +230,14 @@ }); }, removeAllSessions: function(number) { - if (number === null || number === undefined) + if (number === null || number === undefined) { throw new Error("Tried to remove sessions for undefined/null number"); + } return new Promise(function(resolve) { var sessions = new SessionCollection(); sessions.fetchSessionsForNumber(number).always(function() { var promises = []; - while(sessions.length > 0) { + while (sessions.length > 0) { promises.push(new Promise(function(res) { sessions.pop().destroy().then(res); })); @@ -245,8 +254,9 @@ }, getIdentityKey: function(identifier) { - if (identifier === null || identifier === undefined) + if (identifier === null || identifier === undefined) { throw new Error("Tried to get identity key for undefined/null key"); + } var number = textsecure.utils.unencodeNumber(identifier)[0]; return new Promise(function(resolve) { var identityKey = new IdentityKey({id: number}); @@ -256,8 +266,9 @@ }); }, putIdentityKey: function(identifier, publicKey) { - if (identifier === null || identifier === undefined) + if (identifier === null || identifier === undefined) { throw new Error("Tried to put identity key for undefined/null key"); + } if (!(publicKey instanceof ArrayBuffer)) { publicKey = convertToArrayBuffer(publicKey); } @@ -292,8 +303,9 @@ }); }, getGroup: function(groupId) { - if (groupId === null || groupId === undefined) + if (groupId === null || groupId === undefined) { throw new Error("Tried to get group for undefined/null id"); + } return new Promise(function(resolve) { var group = new Group({id: groupId}); group.fetch().always(function() { @@ -302,18 +314,21 @@ }); }, putGroup: function(groupId, group) { - if (groupId === null || groupId === undefined) + if (groupId === null || groupId === undefined) { throw new Error("Tried to put group key for undefined/null id"); - if (group === null || group === undefined) + } + if (group === null || group === undefined) { throw new Error("Tried to put undefined/null group object"); + } var group = new Group({id: groupId, data: group}); return new Promise(function(resolve) { group.save().always(resolve); }); }, removeGroup: function(groupId) { - if (groupId === null || groupId === undefined) + if (groupId === null || groupId === undefined) { throw new Error("Tried to remove group key for undefined/null id"); + } return new Promise(function(resolve) { var group = new Group({id: groupId}); group.destroy().always(resolve); diff --git a/js/debugLog.js b/js/debugLog.js index c6e0f7fabc49..921458cc027c 100644 --- a/js/debugLog.js +++ b/js/debugLog.js @@ -45,7 +45,7 @@ var log = new DebugLog(); if (window.console) { console._log = console.log; - console.log = function(){ + console.log = function() { console._log.apply(this, arguments); var args = Array.prototype.slice.call(arguments); var str = args.join(' ').replace(PHONE_REGEX, "+[REDACTED]$1"); diff --git a/js/models/conversations.js b/js/models/conversations.js index 2d803773300f..681974a94f68 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -181,7 +181,7 @@ var conversationId = this.id; Whisper.Notifications.remove( Whisper.Notifications.models.filter( - function(model){ + function(model) { return model.attributes.conversationId===conversationId; })); } diff --git a/js/models/messages.js b/js/models/messages.js index f29a4f2e2029..1079d5fd9548 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -400,7 +400,7 @@ if (this.length === 0) { // fetch the most recent messages first upper = Number.MAX_VALUE; - } else { + } else { // not our first rodeo, fetch older messages. upper = this.at(0).get('received_at'); } diff --git a/js/register.js b/js/register.js index 6c844d0c8a08..0589a74ccb64 100644 --- a/js/register.js +++ b/js/register.js @@ -39,10 +39,11 @@ }); $('#code').on('change', function() { - if (!validateCode()) + if (!validateCode()) { $('#code').addClass('invalid'); - else + } else { $('#code').removeClass('invalid'); + } }); $('#request-voice').click(function() { diff --git a/js/storage.js b/js/storage.js index 9c0b8b5dc8ef..2ab924a036fc 100644 --- a/js/storage.js +++ b/js/storage.js @@ -22,16 +22,18 @@ *** Base Storage Routines *** *****************************/ put: function(key, value) { - if (value === undefined) + if (value === undefined) { throw new Error("Tried to store undefined"); + } var item = items.add({id: key, value: value}, {merge: true}); item.save(); }, get: function(key, defaultValue) { var item = items.get("" + key); - if (!item) + if (!item) { return defaultValue; + } return item.get('value'); }, diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 876faf5cea66..5a3633ac5588 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -231,7 +231,7 @@ }, replace_colons: function(str) { - return str.replace(emoji.rx_colons, function(m){ + return str.replace(emoji.rx_colons, function(m) { var idx = m.substr(1, m.length-2); var val = emoji.map.colons[idx]; if (val) { diff --git a/js/views/file_input_view.js b/js/views/file_input_view.js index 11a534d4e9d5..b8fa45ef2c03 100644 --- a/js/views/file_input_view.js +++ b/js/views/file_input_view.js @@ -250,7 +250,9 @@ }, openDropped: function(e) { - if (e.originalEvent.dataTransfer.types[0] != 'Files') return; + if (e.originalEvent.dataTransfer.types[0] != 'Files') { + return; + } e.stopPropagation(); e.preventDefault(); @@ -260,7 +262,9 @@ }, showArea: function(e) { - if (e.originalEvent.dataTransfer.types[0] != 'Files') return; + if (e.originalEvent.dataTransfer.types[0] != 'Files') { + return; + } e.stopPropagation(); e.preventDefault(); @@ -268,7 +272,9 @@ }, hideArea: function(e) { - if (e.originalEvent.dataTransfer.types[0] != 'Files') return; + if (e.originalEvent.dataTransfer.types[0] != 'Files') { + return; + } e.stopPropagation(); e.preventDefault(); diff --git a/js/views/new_conversation_view.js b/js/views/new_conversation_view.js index 31330acc376e..6584dd64a9f6 100644 --- a/js/views/new_conversation_view.js +++ b/js/views/new_conversation_view.js @@ -74,7 +74,9 @@ // TODO: css animation or error notification errors.removeClass('error'); - setTimeout(function(){ errors.addClass('error'); }, 300); + setTimeout(function() { + errors.addClass('error'); + }, 300); return; } diff --git a/js/views/whisper_view.js b/js/views/whisper_view.js index fac15f75f011..f9b3767ecccb 100644 --- a/js/views/whisper_view.js +++ b/js/views/whisper_view.js @@ -57,7 +57,7 @@ this.$el.closest('body').append(dialog.el); }.bind(this)); }, - i18n_with_link: function(message, href){ + i18n_with_link: function(message, href) { var attrs = 'class="link" href="' + encodeURI(href) + '" target="_blank"'; return i18n(message, attrs); } diff --git a/test/_test.js b/test/_test.js index e9d3d140394f..119c07aa48c5 100644 --- a/test/_test.js +++ b/test/_test.js @@ -56,8 +56,9 @@ function assertEqualArrayBuffers(ab1, ab2) { function hexToArrayBuffer(str) { var ret = new ArrayBuffer(str.length / 2); var array = new Uint8Array(ret); - for (var i = 0; i < str.length/2; i++) + for (var i = 0; i < str.length/2; i++) { array[i] = parseInt(str.substr(i*2, 2), 16); + } return ret; }; diff --git a/test/models/conversations_test.js b/test/models/conversations_test.js index a54e0c6b1e8b..7e559253d25f 100644 --- a/test/models/conversations_test.js +++ b/test/models/conversations_test.js @@ -93,7 +93,7 @@ sent_at : Date.now(), received_at : Date.now() }); - message.save().then(done) + message.save().then(done); }); }); after(clearDatabase); diff --git a/test/storage_test.js b/test/storage_test.js index d8226d7276d1..d267b02fd881 100644 --- a/test/storage_test.js +++ b/test/storage_test.js @@ -101,7 +101,7 @@ describe("AxolotlStore", function() { var promise = Promise.resolve(); devices.forEach(function(encodedNumber) { promise = promise.then(function() { - return store.putSession(encodedNumber, testRecord + encodedNumber) + return store.putSession(encodedNumber, testRecord + encodedNumber); }); }); promise.then(function() { @@ -133,7 +133,7 @@ describe("AxolotlStore", function() { var promise = Promise.resolve(); devices.forEach(function(encodedNumber) { promise = promise.then(function() { - return store.putSession(encodedNumber, testRecord + encodedNumber) + return store.putSession(encodedNumber, testRecord + encodedNumber); }); }); promise.then(function() { diff --git a/test/views/list_view_test.js b/test/views/list_view_test.js index 7167e7aa33bd..22999cbfd40d 100644 --- a/test/views/list_view_test.js +++ b/test/views/list_view_test.js @@ -1,7 +1,7 @@ describe('ListView', function() { var collection; - beforeEach(function(){ + beforeEach(function() { collection = new Backbone.Collection(); }); diff --git a/test/views/timestamp_view_test.js b/test/views/timestamp_view_test.js index 88dc56353b6c..d3b93d4ef4e3 100644 --- a/test/views/timestamp_view_test.js +++ b/test/views/timestamp_view_test.js @@ -25,7 +25,9 @@ describe('TimestampView', function() { // Helper to check an absolute TS for an exact match against both views var checkAbs = function(ts, expected_brief, expected_ext) { - if (!expected_ext) expected_ext = expected_brief; + if (!expected_ext) { + expected_ext = expected_brief; + } check(brief_view, ts, expected_brief); check(ext_view, ts, expected_ext); };