From 3391e5beeb39a3df7d2d0e9ae26c27d5bb7e19e5 Mon Sep 17 00:00:00 2001 From: Rummskartoffel <20257197+Rummskartoffel@users.noreply.github.com> Date: Fri, 19 Feb 2021 16:48:18 +0100 Subject: [PATCH 1/3] Fix a few trivial no-unsafe-call errors --- src/background.ts | 2 +- src/background/commands.ts | 4 ++-- src/commandline_frame.ts | 46 ++++++++++++++++++-------------------- src/excmds.ts | 23 ++++++++++--------- src/lib/editor.ts | 2 +- 5 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/background.ts b/src/background.ts index 2c6cb240..12f86fcb 100644 --- a/src/background.ts +++ b/src/background.ts @@ -45,7 +45,7 @@ import * as meta from "@src/background/meta" state, webext, webrequests, - l: prom => prom.then(console.log).catch(console.error), + l: (prom: Promise) => prom.then(console.log).catch(console.error), contentLocation: window.location, R, perf, diff --git a/src/background/commands.ts b/src/background/commands.ts index c450d09a..5fcbf28c 100644 --- a/src/background/commands.ts +++ b/src/background/commands.ts @@ -3,14 +3,14 @@ import * as config from "@src/lib/config" import * as keyseq from "@src/lib/keyseq" import * as controller from "@src/lib/controller" -function makelistener(commands) { +function makelistener(commands: Array) { return (command_name: string) => { const command = commands.filter(c => c.name == command_name)[0] const exstring = config.get( "browsermaps", keyseq.mozMapToMinimalKey(command.shortcut).toMapstr(), ) - if (exstring in useractions) return useractions[exstring]() + if (exstring in useractions && typeof useractions[exstring] === "function") return (useractions[exstring] as () => void)() return controller.acceptExCmd(exstring) } } diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 63bd2fdf..84f49d38 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -17,53 +17,54 @@ /** Script used in the commandline iframe. Communicates with background. */ -import * as perf from "@src/perf" -import "@src/lib/number.clamp" -import "@src/lib/html-tagged-template" -import { TabAllCompletionSource } from "@src/completions/TabAll" +import * as SELF from "@src/commandline_frame" +import { CompletionSourceFuse } from "@src/completions" +import { AproposCompletionSource } from "@src/completions/Apropos" import { BindingsCompletionSource } from "@src/completions/Bindings" -import { BufferCompletionSource } from "@src/completions/Tab" import { BmarkCompletionSource } from "@src/completions/Bmark" -import { ExcmdCompletionSource } from "@src/completions/Excmd" -import { ThemeCompletionSource } from "@src/completions/Theme" import { CompositeCompletionSource } from "@src/completions/Composite" +import { ExcmdCompletionSource } from "@src/completions/Excmd" +import { ExtensionsCompletionSource } from "@src/completions/Extensions" import { FileSystemCompletionSource } from "@src/completions/FileSystem" import { GuisetCompletionSource } from "@src/completions/Guiset" import { HelpCompletionSource } from "@src/completions/Help" -import { AproposCompletionSource } from "@src/completions/Apropos" import { HistoryCompletionSource } from "@src/completions/History" import { PreferenceCompletionSource } from "@src/completions/Preferences" import { RssCompletionSource } from "@src/completions/Rss" import { SessionsCompletionSource } from "@src/completions/Sessions" import { SettingsCompletionSource } from "@src/completions/Settings" +import { BufferCompletionSource } from "@src/completions/Tab" +import { TabAllCompletionSource } from "@src/completions/TabAll" +import { ThemeCompletionSource } from "@src/completions/Theme" import { WindowCompletionSource } from "@src/completions/Window" -import { ExtensionsCompletionSource } from "@src/completions/Extensions" +import { contentState } from "@src/content/state_content" +import { theme } from "@src/content/styling" +import { getCommandlineFns } from "@src/lib/commandline_cmds" +import * as tri_editor from "@src/lib/editor" +import "@src/lib/html-tagged-template" +import Logger from "@src/lib/logging" import * as Messaging from "@src/lib/messaging" import "@src/lib/number.clamp" -import state from "@src/state" -import * as State from "@src/state" -import Logger from "@src/lib/logging" -import { theme } from "@src/content/styling" -import { contentState } from "@src/content/state_content" - import * as genericParser from "@src/parsers/genericmode" -import * as tri_editor from "@src/lib/editor" - +import * as perf from "@src/perf" +import state, * as State from "@src/state" import * as R from "ramda" +import { KeyEventLike } from "./lib/keyseq" + /** @hidden **/ const logger = new Logger("cmdline") /** @hidden **/ const commandline_state = { - activeCompletions: undefined, + activeCompletions: undefined as CompletionSourceFuse[], clInput: window.document.getElementById( "tridactyl-input", ) as HTMLInputElement, clear, cmdline_history_position: 0, completionsDiv: window.document.getElementById("completions"), - fns: undefined, + fns: undefined as ReturnType, getCompletion, history, /** @hidden @@ -206,7 +207,7 @@ commandline_state.clInput.addEventListener( // Abuse async to wrap non-promises in a promise // eslint-disable-next-line @typescript-eslint/require-await (async () => - commandline_state.fns[funcname]( + commandline_state.fns[funcname as keyof typeof commandline_state.fns]( args.length === 0 ? undefined : args.join(" "), ))(), ) @@ -347,7 +348,7 @@ export function getContent() { } /** @hidden **/ -export function editor_function(fn_name, ...args) { +export function editor_function(fn_name: keyof typeof tri_editor, ...args) { let result = Promise.resolve([]) if (tri_editor[fn_name]) { tri_editor[fn_name](commandline_state.clInput, ...args) @@ -360,11 +361,8 @@ export function editor_function(fn_name, ...args) { return result } -import * as SELF from "@src/commandline_frame" Messaging.addListener("commandline_frame", Messaging.attributeCaller(SELF)) -import { getCommandlineFns } from "@src/lib/commandline_cmds" -import { KeyEventLike } from "./lib/keyseq" commandline_state.fns = getCommandlineFns(commandline_state) Messaging.addListener( "commandline_cmd", diff --git a/src/excmds.ts b/src/excmds.ts index c69a8534..2ef11c6d 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -86,6 +86,7 @@ import { AutoContain } from "@src/lib/autocontainers" import * as CSS from "css" import * as Perf from "@src/perf" import * as Metadata from "@src/.metadata.generated" +import { ObjectType } from "../compiler/types/ObjectType" import * as Native from "@src/lib/native" import * as TTS from "@src/lib/text_to_speech" import * as excmd_parser from "@src/parsers/exmode" @@ -189,9 +190,9 @@ export async function getNativeVersion(): Promise { //#content export async function getRssLinks(): Promise> { const seen = new Set() - return Array.from(document.querySelectorAll("a, link[rel='alternate']")) - .filter((e: any) => typeof e.href === "string") - .reduce((acc, e: any) => { + return Array.from(document.querySelectorAll("a, link[rel='alternate']")) + .filter((e) => typeof e.href === "string") + .reduce((acc, e) => { let type = "" // Start by detecting type because url doesn't necessarily contain the words "rss" or "atom" if (e.type) { @@ -1339,7 +1340,7 @@ export async function url2args() { /** @hidden */ //#content_helper -let sourceElement +let sourceElement: Element /** @hidden */ //#content_helper function removeSource() { @@ -1527,7 +1528,7 @@ export async function apropos(...helpItems: string[]) { //#background export async function tutor(newtab?: string) { const tutor = browser.runtime.getURL("static/clippy/1-tutor.html") - let done + let done: Promise if (newtab) { done = tabopen(tutor) } else { @@ -1976,7 +1977,7 @@ export async function loadaucmds(cmdType: "DocStart" | "DocLoad" | "DocEnd" | "T try { await controller.acceptExCmd(aucmds[aukey]) } catch (e) { - logger.error(e.toString()) + logger.error((e as Error).toString()) } } } @@ -2165,7 +2166,7 @@ export async function tabnext(increment = 1) { */ //#background export async function tabnext_gt(index?: number) { - let done + let done: Promise if (index === undefined) { done = tabnext() } else { @@ -2288,7 +2289,7 @@ export async function tabopen(...addressarr: string[]): Promise { + async function argParse(args: string[]): Promise { if (args[0] === "-b") { active = false args.shift() @@ -2735,7 +2736,7 @@ export async function mute(...muteArgs: string[]): Promise { if (mute) { updateObj.muted = true } - let done + let done: Promise if (all) { const tabs = await browser.tabs.query({ currentWindow: true }) const promises = [] @@ -3628,7 +3629,7 @@ function validateSetArgs(key: string, values: string[]) { const strval = values.join(" ") // Note: the conversion will throw if strval can't be converted to the right type if (md.type.kind === "object" && target.length > 1) { - value = (md as any).type.convertMember(target.slice(1), strval) + value = (md.type as ObjectType).convertMember(target.slice(1), strval) } else { value = md.type.convert(strval) } @@ -4859,7 +4860,7 @@ export async function bmark(url?: string, ...titlearr: string[]) { if (path != "") { const tree = (await browser.bookmarks.getTree())[0] // Why would getTree return a tree? Obviously it returns an array of unit length. // I hate recursion. - const treeClimber = (tree, treestr) => { + const treeClimber = (tree: browser.bookmarks.BookmarkTreeNode, treestr) => { if (tree.type !== "folder") return {} treestr += tree.title + "/" if (!("children" in tree) || tree.children.length === 0) return [{ path: treestr, id: tree.id }] diff --git a/src/lib/editor.ts b/src/lib/editor.ts index f88e985b..14225f8a 100644 --- a/src/lib/editor.ts +++ b/src/lib/editor.ts @@ -108,7 +108,7 @@ export const transpose_chars = wrap_input( * Applies a function to the word the caret is in, or to the next word if the caret is not in a word, or to the previous word if the current word is empty. */ function applyWord( - text, + text: string, selectionStart, selectionEnd, fn: (s: string) => string, From 61e6e9263c88125552eedd6ef67e06c3724b20c7 Mon Sep 17 00:00:00 2001 From: Rummskartoffel <20257197+Rummskartoffel@users.noreply.github.com> Date: Sat, 20 Feb 2021 11:34:11 +0100 Subject: [PATCH 2/3] Use Record for useractions --- src/background/commands.ts | 4 ++-- src/background/user_actions.ts | 6 +++++- src/excmds.ts | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/background/commands.ts b/src/background/commands.ts index 5fcbf28c..255ef2f8 100644 --- a/src/background/commands.ts +++ b/src/background/commands.ts @@ -1,4 +1,4 @@ -import * as useractions from "@src/background/user_actions" +import { useractions } from "@src/background/user_actions" import * as config from "@src/lib/config" import * as keyseq from "@src/lib/keyseq" import * as controller from "@src/lib/controller" @@ -10,7 +10,7 @@ function makelistener(commands: Array) { "browsermaps", keyseq.mozMapToMinimalKey(command.shortcut).toMapstr(), ) - if (exstring in useractions && typeof useractions[exstring] === "function") return (useractions[exstring] as () => void)() + if (exstring in useractions) useractions[exstring]() return controller.acceptExCmd(exstring) } } diff --git a/src/background/user_actions.ts b/src/background/user_actions.ts index d537fa2b..9cd7defa 100644 --- a/src/background/user_actions.ts +++ b/src/background/user_actions.ts @@ -9,7 +9,7 @@ import * as R from "ramda" import * as config from "@src/lib/config" import { getTridactylTabs } from "@src/background/meta" -export function escapehatch() { +function escapehatch() { if (config.get("escapehatchsidebarhack") == "true") { // Only works if called via commands API command - fail silently if called otherwise browser.sidebarAction.open().catch() @@ -35,3 +35,7 @@ export function escapehatch() { return browser.tabs.update(best.id, { active: true }) })() } + +export const useractions: Record void> = { + escapehatch, +} diff --git a/src/excmds.ts b/src/excmds.ts index 2ef11c6d..faae2f02 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -3106,7 +3106,7 @@ export async function shellescape(...quoteme: string[]) { } //#background_helper -import * as useractions from "@src/background/user_actions" +import { useractions } from "@src/background/user_actions" /** * Magic escape hatch: if Tridactyl can't run in the current tab, return to a tab in the current window where Tridactyl can run, making such a tab if it doesn't currently exist. If Tridactyl can run in the current tab, return focus to the document body from e.g. the URL bar or a video player. From 573ab2b7908e4362e9cf32f42b8790f50cf9a792 Mon Sep 17 00:00:00 2001 From: Rummskartoffel <20257197+Rummskartoffel@users.noreply.github.com> Date: Sat, 20 Feb 2021 12:17:50 +0100 Subject: [PATCH 3/3] Re-add missing return --- src/background/commands.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background/commands.ts b/src/background/commands.ts index 255ef2f8..f9c3b466 100644 --- a/src/background/commands.ts +++ b/src/background/commands.ts @@ -10,7 +10,7 @@ function makelistener(commands: Array) { "browsermaps", keyseq.mozMapToMinimalKey(command.shortcut).toMapstr(), ) - if (exstring in useractions) useractions[exstring]() + if (exstring in useractions) return useractions[exstring]() return controller.acceptExCmd(exstring) } }