From af20bbe4df01c66439eb1761aceacebe2035a144 Mon Sep 17 00:00:00 2001 From: glacambre Date: Mon, 15 Apr 2019 19:47:41 +0200 Subject: [PATCH] TSLint: re-enable no-identical-functions rule --- src/background.ts | 20 ++++++++------------ src/content.ts | 29 ++++++++++++----------------- src/excmds.ts | 2 ++ src/lib/config.ts | 26 +++++++++++--------------- src/lib/test_utils.ts | 22 ++++++++-------------- tslint.json | 1 - 6 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/background.ts b/src/background.ts index 394a0234..5fd1a751 100644 --- a/src/background.ts +++ b/src/background.ts @@ -135,22 +135,18 @@ browser.tabs.onActivated.addListener(ev => { const aucon = new AutoContain() +function cancelReq(details) { + if (aucon.getCancelledRequest(details.tabId)) { + aucon.clearCancelledRequests(details.tabId) + } +} + // Handle cancelled requests as a result of autocontain. -browser.webRequest.onCompleted.addListener( - details => { - if (aucon.getCancelledRequest(details.tabId)) { - aucon.clearCancelledRequests(details.tabId) - } - }, +browser.webRequest.onCompleted.addListener(cancelReq, { urls: [" { - if (aucon.getCancelledRequest(details.tabId)) { - aucon.clearCancelledRequests(details.tabId) - } - }, +browser.webRequest.onErrorOccurred.addListener(cancelReq, { urls: [""], types: ["main_frame"] }, ) diff --git a/src/content.ts b/src/content.ts index 3829c1d2..fde646e7 100644 --- a/src/content.ts +++ b/src/content.ts @@ -262,30 +262,25 @@ config.getAsync("modeindicator").then(mode => { }) }) -// Site specific fix for / on GitHub.com +function protectSlash(e) { + if ("/".indexOf(e.key) !== -1 && contentState.mode === "normal") { + e.cancelBubble = true + e.stopImmediatePropagation() + } +} + +// Some sites like to prevent firefox's `/` from working so we need to protect +// ourselves against that +// This was originally a github-specific fix config.getAsync("leavegithubalone").then(v => { if (v === "true") return try { // On quick loading pages, the document is already loaded - // if (document.location.host === "github.com") { - document.body.addEventListener("keydown", function(e) { - if ("/".indexOf(e.key) !== -1 && contentState.mode === "normal") { - e.cancelBubble = true - e.stopImmediatePropagation() - } - }) - // } + document.body.addEventListener("keydown", protectSlash) } catch (e) { // But on slower pages we wait for the document to load window.addEventListener("DOMContentLoaded", () => { - // if (document.location.host === "github.com") { - document.body.addEventListener("keydown", function(e) { - if ("/".indexOf(e.key) !== -1 && contentState.mode === "normal") { - e.cancelBubble = true - e.stopImmediatePropagation() - } - }) - // } + document.body.addEventListener("keydown", protectSlash) }) } }) diff --git a/src/excmds.ts b/src/excmds.ts index e00f0c78..234c951d 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -4053,6 +4053,7 @@ export async function echo(...str: string[]) { * * `composite get_current_url | js -p alert(JS_ARG)` */ +/* tslint:disable:no-identical-functions */ //#content export async function js(...str: string[]) { if (str[0].startsWith("-p")) { @@ -4067,6 +4068,7 @@ export async function js(...str: string[]) { /** * Lets you execute JavaScript in the background context. All the help from [[js]] applies. Gives you a different `tri` object. */ +/* tslint:disable:no-identical-functions */ //#background export async function jsb(...str: string[]) { if (str[0].startsWith("-p")) { diff --git a/src/lib/config.ts b/src/lib/config.ts index 2f9ec7a3..6e48d2be 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -1280,6 +1280,15 @@ export function removeChangeListener

( browser.storage.onChanged.addListener(async (changes, areaname) => { if (CONFIGNAME in changes) { const defaultConf = new default_config() + const old = USERCONFIG + + function triggerChangeListeners(key) { + const arr = changeListeners.get(key) + if (arr) { + const v = old[key] === undefined ? defaultConf[key] : old[key] + arr.forEach(f => f(v, USERCONFIG[key])) + } + } // newValue is undefined when calling browser.storage.AREANAME.clear() if (changes[CONFIGNAME].newValue !== undefined) { @@ -1301,7 +1310,6 @@ browser.storage.onChanged.addListener(async (changes, areaname) => { ) !== JSON.stringify(changes[CONFIGNAME].newValue[k]), ) - const old = USERCONFIG USERCONFIG = changes[CONFIGNAME].newValue // Trigger listeners @@ -1312,26 +1320,14 @@ browser.storage.onChanged.addListener(async (changes, areaname) => { } }) - changedKeys.forEach(key => { - const arr = changeListeners.get(key) - if (arr) { - const v = old[key] === undefined ? defaultConf[key] : old[key] - arr.forEach(f => f(v, USERCONFIG[key])) - } - }) + changedKeys.forEach(key => triggerChangeListeners(key)) } else if (areaname === (await get("storageloc"))) { // If newValue is undefined and AREANAME is the same value as STORAGELOC, the user wants to clean their config - const old = USERCONFIG USERCONFIG = o({}) Object.keys(old) .filter(key => old[key] !== defaultConf[key]) - .forEach(key => { - const arr = changeListeners.get(key) - if (arr) { - arr.forEach(f => f(old[key], defaultConf[key])) - } - }) + .forEach(key => triggerChangeListeners(key)) } } }) diff --git a/src/lib/test_utils.ts b/src/lib/test_utils.ts index 9f41a624..ea1bd4ce 100644 --- a/src/lib/test_utils.ts +++ b/src/lib/test_utils.ts @@ -7,6 +7,12 @@ function wrapPrimitives(testcases) { }) } +function arrayify(toTest, args) { + const result = toTest(...args) + if (result instanceof Array) return result + else return [result] +} + /** Test each case in testcases. Warning: if your function really accepts an array, that array must be @@ -16,13 +22,7 @@ export function testAll(toTest, testcases) { testcases = testcases.map(wrapPrimitives) for (const [args, ans] of testcases) { test(`${toTest.name}(${args}) == ${JSON.stringify(ans)}`, () => - expect( - (() => { - const result = toTest(...args) - if (result instanceof Array) return result - else return [result] - })(), - ).toEqual(expect.arrayContaining(ans))) + expect(arrayify(toTest, args)).toEqual(expect.arrayContaining(ans))) } } @@ -37,13 +37,7 @@ export function testAllCustom(toTest, testcases, expectAttr, expectArg) { testcases = testcases.map(wrapPrimitives) for (const [args, ans] of testcases) { test(`${toTest.name}(${args}) == ${JSON.stringify(ans)}`, () => - expect( - (() => { - const result = toTest(...args) - if (result instanceof Array) return result - else return [result] - })(), - )[expectAttr](eval(expectArg))) + expect(arrayify(toTest, args))[expectAttr](eval(expectArg))) } } diff --git a/tslint.json b/tslint.json index 12ff23fa..8075ae08 100644 --- a/tslint.json +++ b/tslint.json @@ -23,7 +23,6 @@ "no-empty": [true, "allow-empty-catch", "allow-empty-functions"], "no-eval": false, "no-extra-semicolon": false, - "no-identical-functions": false, "no-shadowed-variable": false, "no-string-throw": false, "no-unsafe-finally": false,