From 57981890c30b0863f788deb63394bef69d003997 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Sun, 30 Dec 2018 09:18:14 +0800 Subject: [PATCH 1/2] Add eslint rules space-before-blocks and brace-style --- chromium/.eslintrc.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chromium/.eslintrc.json b/chromium/.eslintrc.json index 2851620f3a87..7369e6067c4d 100644 --- a/chromium/.eslintrc.json +++ b/chromium/.eslintrc.json @@ -12,7 +12,9 @@ "indent": ["error", 2, {"outerIIFEBody": 0}], "no-unused-vars": "error", "no-undef": "error", - "strict": ["error", "global"] + "strict": ["error", "global"], + "space-before-blocks": ["error"], + "brace-style": ["error"] }, "globals": { "exports": true, From ae936753c158cc2382c04d238650fa65b8f98da8 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Sun, 30 Dec 2018 09:19:39 +0800 Subject: [PATCH 2/2] $linter --fix $srcdir --- chromium/background-scripts/background.js | 29 +++++++++--------- chromium/background-scripts/rules.js | 14 ++++----- chromium/background-scripts/store.js | 2 +- chromium/background-scripts/update.js | 36 +++++++++++------------ chromium/background-scripts/util.js | 2 +- chromium/pages/cancel/ux.js | 6 ++-- chromium/pages/debugging-rulesets/ux.js | 4 ++- chromium/pages/options/ux.js | 26 ++++++++-------- chromium/pages/popup/ux.js | 6 ++-- chromium/test/update_test.js | 10 ++++--- 10 files changed, 71 insertions(+), 64 deletions(-) diff --git a/chromium/background-scripts/background.js b/chromium/background-scripts/background.js index 6ffc648f1187..96099af475a9 100644 --- a/chromium/background-scripts/background.js +++ b/chromium/background-scripts/background.js @@ -42,7 +42,7 @@ var showCounter = true; var isExtensionEnabled = true; let disabledList = new Set(); -function initializeStoredGlobals(){ +function initializeStoredGlobals() { return new Promise(resolve => { store.get({ httpNowhere: false, @@ -301,11 +301,11 @@ let simpleHTTPNowhereRedirect = new Map(); const cancelUrl = chrome.runtime.getURL("/pages/cancel/index.html"); -function redirectOnCancel(shouldCancel, originURL){ +function redirectOnCancel(shouldCancel, originURL) { return shouldCancel ? {redirectUrl: newCancelUrl(originURL)} : {cancel: false}; } -function newCancelUrl(originURL){ +function newCancelUrl(originURL) { return cancelUrl + "?originURL=" + encodeURI(originURL); } @@ -553,7 +553,9 @@ function sortSwitchPlanner(tab_id, rewritten) { var score = activeCount * 100 + passiveCount; asset_host_list.push([score, activeCount, passiveCount, asset_host]); } - asset_host_list.sort(function(a,b){return a[0]-b[0];}); + asset_host_list.sort(function(a,b) { + return a[0]-b[0]; + }); return asset_host_list; } @@ -665,8 +667,7 @@ function onErrorOccurred(details) { details.error.indexOf("Unable to communicate securely with peer: requested domain name does not match the server’s certificate.") == 0 || details.error.indexOf("Cannot communicate securely with peer: no common encryption algorithm(s).") == 0 || details.error.indexOf("SSL peer has no certificate for the requested DNS name.") == 0 - )) - { + )) { let url = new URL(details.url); if (url.protocol == "https:") { url.protocol = "http:"; @@ -782,7 +783,7 @@ function enableSwitchPlannerFor(tabId) { // Listen for connection from the DevTools panel so we can set up communication. chrome.runtime.onConnect.addListener(function (port) { if (port.name == "devtools-page") { - chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ + chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { var tabId = message.tabId; var disableOnCloseCallback = function() { @@ -815,9 +816,9 @@ chrome.runtime.onConnect.addListener(function (port) { // This is necessary for communication with the popup in Firefox Private // Browsing Mode, see https://bugzilla.mozilla.org/show_bug.cgi?id=1329304 -chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ +chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { - function get_update_channels_generic(update_channels){ + function get_update_channels_generic(update_channels) { let last_updated_promises = []; for(let update_channel of update_channels) { last_updated_promises.push(new Promise(resolve => { @@ -944,7 +945,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ return obj; }, new Set()); - if(update_channel_names.has(message.object)){ + if(update_channel_names.has(message.object)) { return sendResponse(false); } @@ -983,8 +984,8 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ store.get({update_channels: []}, item => { let scope_changed = false; item.update_channels = item.update_channels.map(update_channel => { - if(update_channel.name == message.object.name){ - if(update_channel.scope != message.object.scope){ + if(update_channel.name == message.object.name) { + if(update_channel.scope != message.object.scope) { scope_changed = true; } update_channel = message.object; @@ -1000,7 +1001,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ // necesssary to avoid a race condition, see #16673 update.loadUpdateChannelsKeys().then(() => { update.resetTimer(); - if(scope_changed){ + if(scope_changed) { initializeAllRules(); } sendResponse(true); @@ -1029,7 +1030,7 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ return true; }, is_firefox: () => { - if(typeof(browser) != "undefined"){ + if(typeof(browser) != "undefined") { browser.runtime.getBrowserInfo().then(function(info) { if (info.name == "Firefox") { sendResponse(true); diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index 6b42cc6885a5..43680a69bbd4 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -29,7 +29,7 @@ const nullIterable = Object.create(null, { const scopes = new Map(); /* Returns the scope object from the map for the given scope string */ -function getScope(scope){ +function getScope(scope) { if (!scopes.has(scope)) { scopes.set(scope, new RegExp(scope)); } @@ -174,7 +174,7 @@ RuleSet.prototype = { } if(this_rules_length > 0) { - for(let x = 0; x < this.rules.length; x++){ + for(let x = 0; x < this.rules.length; x++) { if(this.rules[x].to != ruleset.rules[x].to) { return false; } @@ -431,7 +431,7 @@ RuleSets.prototype = { } }, - addStoredCustomRulesets: function(){ + addStoredCustomRulesets: function() { return new Promise(resolve => { this.store.get({ legacy_custom_rulesets: [], @@ -445,17 +445,17 @@ RuleSets.prototype = { }, // Load in the legacy custom rulesets, if any - loadCustomRulesets: function(legacy_custom_rulesets){ - for(let legacy_custom_ruleset of legacy_custom_rulesets){ + loadCustomRulesets: function(legacy_custom_rulesets) { + for(let legacy_custom_ruleset of legacy_custom_rulesets) { this.loadCustomRuleset(legacy_custom_ruleset); } }, - loadCustomRuleset: function(ruleset_string){ + loadCustomRuleset: function(ruleset_string) { this.addFromXml((new DOMParser()).parseFromString(ruleset_string, 'text/xml')); }, - setRuleActiveState: async function(ruleset_name, active){ + setRuleActiveState: async function(ruleset_name, active) { if (active == undefined) { delete this.ruleActiveStates[ruleset_name]; } else { diff --git a/chromium/background-scripts/store.js b/chromium/background-scripts/store.js index c41ccd0a5cc2..925e6057c06a 100644 --- a/chromium/background-scripts/store.js +++ b/chromium/background-scripts/store.js @@ -8,7 +8,7 @@ function initialize() { return new Promise(resolve => { if (chrome.storage.sync) { chrome.storage.sync.set({"sync-set-test": true}, () => { - if(chrome.runtime.lastError){ + if(chrome.runtime.lastError) { setStorage(chrome.storage.local); } else { setStorage(chrome.storage.sync); diff --git a/chromium/background-scripts/update.js b/chromium/background-scripts/update.js index 883929597da6..930974ff3c03 100644 --- a/chromium/background-scripts/update.js +++ b/chromium/background-scripts/update.js @@ -41,7 +41,7 @@ async function loadUpdateChannelsKeys() { imported_keys = {}; combined_update_channels = []; - for(let update_channel of combined_update_channels_preflight){ + for(let update_channel of combined_update_channels_preflight) { try{ imported_keys[update_channel.name] = await window.crypto.subtle.importKey( @@ -88,7 +88,7 @@ async function checkForNewRulesets(update_channel) { if(timestamp_result.status == 200) { let rulesets_timestamp = Number(await timestamp_result.text()); - if((await store.local.get_promise('rulesets-timestamp: ' + update_channel.name, 0)) < rulesets_timestamp){ + if((await store.local.get_promise('rulesets-timestamp: ' + update_channel.name, 0)) < rulesets_timestamp) { return rulesets_timestamp; } } @@ -96,9 +96,9 @@ async function checkForNewRulesets(update_channel) { } // Retrieve the timestamp for when a stored ruleset bundle was published -async function getRulesetTimestamps(){ +async function getRulesetTimestamps() { let timestamp_promises = []; - for(let update_channel of combined_update_channels){ + for(let update_channel of combined_update_channels) { timestamp_promises.push(new Promise(async resolve => { let timestamp = await store.local.get_promise('rulesets-stored-timestamp: ' + update_channel.name, 0); resolve([update_channel.name, timestamp]); @@ -135,7 +135,7 @@ async function getNewRulesets(rulesets_timestamp, update_channel) { // Returns a promise which verifies that the rulesets have a valid EFF // signature, and if so, stores them and returns true. // Otherwise, it throws an exception. -function verifyAndStoreNewRulesets(new_rulesets, rulesets_timestamp, update_channel){ +function verifyAndStoreNewRulesets(new_rulesets, rulesets_timestamp, update_channel) { return new Promise((resolve, reject) => { window.crypto.subtle.verify( { @@ -154,7 +154,7 @@ function verifyAndStoreNewRulesets(new_rulesets, rulesets_timestamp, update_chan const rulesets = new TextDecoder("utf-8").decode(rulesets_byte_array); const rulesets_json = JSON.parse(rulesets); - if(rulesets_json.timestamp != rulesets_timestamp){ + if(rulesets_json.timestamp != rulesets_timestamp) { reject(update_channel.name + ': Downloaded ruleset had an incorrect timestamp. This may be an attempted downgrade attack. Aborting.'); } else { await store.local.set_promise('rulesets: ' + update_channel.name, window.btoa(rulesets_gz)); @@ -170,13 +170,13 @@ function verifyAndStoreNewRulesets(new_rulesets, rulesets_timestamp, update_chan } // Unzip and apply the rulesets we have stored. -async function applyStoredRulesets(rulesets_obj){ +async function applyStoredRulesets(rulesets_obj) { let rulesets_promises = []; - for(let update_channel of combined_update_channels){ + for(let update_channel of combined_update_channels) { rulesets_promises.push(new Promise(resolve => { const key = 'rulesets: ' + update_channel.name; chrome.storage.local.get(key, root => { - if(root[key]){ + if(root[key]) { util.log(util.NOTE, update_channel.name + ': Applying stored rulesets.'); const rulesets_gz = window.atob(root[key]); @@ -192,14 +192,14 @@ async function applyStoredRulesets(rulesets_obj){ })); } - function isNotUndefined(subject){ + function isNotUndefined(subject) { return (typeof subject != 'undefined'); } const channel_results = (await Promise.all(rulesets_promises)).filter(isNotUndefined); - if(channel_results.length > 0){ - for(let channel_result of channel_results){ + if(channel_results.length > 0) { + for(let channel_result of channel_results) { rulesets_obj.addFromJson(channel_result.json.rulesets, channel_result.scope); } } else { @@ -215,9 +215,9 @@ async function performCheck() { store.local.set_promise('last-checked', current_timestamp); let num_updates = 0; - for(let update_channel of combined_update_channels){ + for(let update_channel of combined_update_channels) { let new_rulesets_timestamp = await checkForNewRulesets(update_channel); - if(new_rulesets_timestamp){ + if(new_rulesets_timestamp) { util.log(util.NOTE, update_channel.name + ': A new ruleset bundle has been released. Downloading now.'); let new_rulesets = await getNewRulesets(new_rulesets_timestamp, update_channel); try{ @@ -229,7 +229,7 @@ async function performCheck() { } } } - if(num_updates > 0){ + if(num_updates > 0) { background_callback(); } }; @@ -253,7 +253,7 @@ chrome.storage.onChanged.addListener(async function(changes, areaName) { let initialCheck, subsequentChecks; -async function createTimer(){ +async function createTimer() { const time_to_next_check = await timeToNextCheck(); initialCheck = setTimeout(() => { @@ -262,7 +262,7 @@ async function createTimer(){ }, time_to_next_check * 1000); } -function destroyTimer(){ +function destroyTimer() { if (initialCheck) { clearTimeout(initialCheck); } @@ -271,7 +271,7 @@ function destroyTimer(){ } } -async function initialize(store_param, cb){ +async function initialize(store_param, cb) { store = store_param; background_callback = cb; diff --git a/chromium/background-scripts/util.js b/chromium/background-scripts/util.js index b470408d2160..1946e141122d 100644 --- a/chromium/background-scripts/util.js +++ b/chromium/background-scripts/util.js @@ -69,7 +69,7 @@ function ArrayBufferToString(ab) { let array = new Uint8Array(ab); let string = ""; - for (let byte of array){ + for (let byte of array) { string += String.fromCharCode(byte); } diff --git a/chromium/pages/cancel/ux.js b/chromium/pages/cancel/ux.js index 8afb4fb71846..d21c3b708a59 100644 --- a/chromium/pages/cancel/ux.js +++ b/chromium/pages/cancel/ux.js @@ -5,7 +5,9 @@ let observer; document.addEventListener("DOMContentLoaded", () => { const explainer = document.querySelector("[data-i18n=cancel_he_blocking_explainer]"); - observer = new MutationObserver(() => {replaceLink(explainer)}); + observer = new MutationObserver(() => { + replaceLink(explainer) + }); if (explainer.innerText.length > 0) { replaceLink(explainer); } else { @@ -14,7 +16,7 @@ document.addEventListener("DOMContentLoaded", () => { displayURL(); }); -function replaceLink(explainer){ +function replaceLink(explainer) { observer.disconnect(); const linkText = chrome.i18n.getMessage("cancel_he_blocking_network"); const link = document.createElement("a"); diff --git a/chromium/pages/debugging-rulesets/ux.js b/chromium/pages/debugging-rulesets/ux.js index be6c8b56fd22..058066568538 100644 --- a/chromium/pages/debugging-rulesets/ux.js +++ b/chromium/pages/debugging-rulesets/ux.js @@ -34,7 +34,9 @@ document.getElementById("save-button").addEventListener("click", e => { e.preventDefault(); sendMessage("set_option", { debugging_rulesets: cm.getValue() }, () => { savedTextElement.style.display = "block"; - setTimeout(() => { savedTextElement.style.display = "none" }, 1000); + setTimeout(() => { + savedTextElement.style.display = "none" + }, 1000); valueHasChanged = false; document.title = savedTitle; diff --git a/chromium/pages/options/ux.js b/chromium/pages/options/ux.js index ddf360d4dd55..9ffb4cc3c533 100644 --- a/chromium/pages/options/ux.js +++ b/chromium/pages/options/ux.js @@ -42,7 +42,7 @@ document.addEventListener("DOMContentLoaded", () => { }); }); - function onlyShowSection(sectionId){ + function onlyShowSection(sectionId) { document.querySelectorAll('.section-wrapper').forEach(sw => { sw.style.display = "none"; }); @@ -62,10 +62,10 @@ document.addEventListener("DOMContentLoaded", () => { }); }); - function create_update_channel_element(update_channel, last_updated, pinned){ + function create_update_channel_element(update_channel, last_updated, pinned) { let ruleset_version_string; - if(last_updated){ + if(last_updated) { const ruleset_date = new Date(last_updated * 1000); ruleset_version_string = ruleset_date.getUTCFullYear() + "." + (ruleset_date.getUTCMonth() + 1) + "." + ruleset_date.getUTCDate(); } else { @@ -188,14 +188,14 @@ document.addEventListener("DOMContentLoaded", () => { return update_channel_div; } - function render_update_channels(){ + function render_update_channels() { const update_channels_list = document.getElementById("update-channels-list"); - while(update_channels_list.firstChild){ + while(update_channels_list.firstChild) { update_channels_list.removeChild(update_channels_list.firstChild); } sendMessage("get_pinned_update_channels", null, item => { - for(const update_channel of item.update_channels){ + for(const update_channel of item.update_channels) { update_channels_list.appendChild( create_update_channel_element( update_channel, @@ -208,7 +208,7 @@ document.addEventListener("DOMContentLoaded", () => { }); sendMessage("get_stored_update_channels", null, item => { - for(const update_channel of item.update_channels){ + for(const update_channel of item.update_channels) { update_channels_list.appendChild( create_update_channel_element( update_channel, @@ -227,7 +227,7 @@ document.addEventListener("DOMContentLoaded", () => { const update_channels_error = document.getElementById("update-channels-error"); update_channel_name_div.setAttribute("placeholder", chrome.i18n.getMessage("options_enterUpdateChannelName")); - function displayError(text){ + function displayError(text) { update_channels_error_text.innerText = text; update_channels_error.style.display = "block"; window.scrollTo(0,0); @@ -272,7 +272,7 @@ document.addEventListener("DOMContentLoaded", () => { getOption_("disabledList", [], function(item) { let rule_host_parent = e("disabled-rules-wrapper"); - if( 0 === item.disabledList.length ){ + if( 0 === item.disabledList.length ) { hide(rule_host_parent); return; } @@ -281,7 +281,7 @@ document.addEventListener("DOMContentLoaded", () => { templateRemove.src = chrome.runtime.getURL("images/remove.png"); templateRemove.className = "remove"; - if( item ){ + if( item ) { for (const key of item.disabledList) { let rule_host = document.createElement("div"); let remove = templateRemove.cloneNode(true); @@ -304,12 +304,12 @@ document.addEventListener("DOMContentLoaded", () => { add_update_channel.addEventListener("click", () => { const update_channel_name = update_channel_name_div.value; - if(update_channel_name.trim() == ""){ + if(update_channel_name.trim() == "") { displayError("Error: The update channel name is blank. Please enter another name."); } else { update_channel_name_div.value = ""; sendMessage("create_update_channel", update_channel_name, result => { - if(result == true){ + if(result == true) { render_update_channels(); } else { displayError("Error: There already exists an update channel with this name."); @@ -326,7 +326,7 @@ document.addEventListener("DOMContentLoaded", () => { const update_channels_last_checked = document.getElementById("update-channels-last-checked"); sendMessage("get_last_checked", null, last_checked => { let last_checked_string; - if(last_checked){ + if(last_checked) { const last_checked_date = new Date(last_checked * 1000); const options = { year: '2-digit', diff --git a/chromium/pages/popup/ux.js b/chromium/pages/popup/ux.js index 041fbb051a76..5030bb7ecf47 100644 --- a/chromium/pages/popup/ux.js +++ b/chromium/pages/popup/ux.js @@ -161,7 +161,7 @@ document.addEventListener("DOMContentLoaded", function () { getTab(tab => { const url = new URL(tab.url); sendMessage("check_if_site_disabled", url.host, disabled => { - if(!disabled){ + if(!disabled) { listRules(tab); } showEnableOrDisable(url, disabled); @@ -195,8 +195,8 @@ document.addEventListener("DOMContentLoaded", function () { let rulesets_versions = e('rulesets-versions'); sendMessage("get_ruleset_timestamps", null, timestamps => { - for(let [update_channel, timestamp] of timestamps){ - if(timestamp > 0){ + for(let [update_channel, timestamp] of timestamps) { + if(timestamp > 0) { let ruleset_date = new Date(timestamp * 1000); let ruleset_version_string = ruleset_date.getUTCFullYear() + "." + (ruleset_date.getUTCMonth() + 1) + "." + ruleset_date.getUTCDate(); diff --git a/chromium/test/update_test.js b/chromium/test/update_test.js index 56ba04f5431d..bc9bde52bf1b 100644 --- a/chromium/test/update_test.js +++ b/chromium/test/update_test.js @@ -20,13 +20,15 @@ describe('update.js', function() { describe('applyStoredRulesets', function() { beforeEach(() => { chrome.flush(); - if(util.loadExtensionFile.restore){ util.loadExtensionFile.restore(); } + if(util.loadExtensionFile.restore) { + util.loadExtensionFile.restore(); + } }); it('applies compressed rulesets from chrome.storage', function(done) { let apply_promises = []; - for(let update_channel of update_channels){ + for(let update_channel of update_channels) { const key = 'rulesets: ' + update_channel.name; chrome.storage.local.get.withArgs(key).yields({[key]: example_rulesets_gz}); } @@ -38,7 +40,7 @@ describe('update.js', function() { resolve(); })); - if(apply_promises.length == update_channels.length){ + if(apply_promises.length == update_channels.length) { Promise.all(apply_promises).then(() => done()); } }}); @@ -46,7 +48,7 @@ describe('update.js', function() { }); it('applies rulesets from local extension file', function(done) { - for(let update_channel of update_channels){ + for(let update_channel of update_channels) { const key = 'rulesets: ' + update_channel.name; chrome.storage.local.get.withArgs(key).yields({}); }