From b7d87157a6b3feb87e4aef5a4c560a80a625dfbb Mon Sep 17 00:00:00 2001 From: Denis Revunov Date: Sun, 12 Mar 2023 21:05:29 +0300 Subject: [PATCH] Try to fix more typing errors The messages object in background.ts implicitly had a type { [key: string]: { [key: string]: (...args: any) => any } }, and was used as such in messaging.ts. Now that TypeScript is updated, it got angry because the imported excmds_background and statsLogger are not strictly { [key: string]: (...args: any) => any }. For excmd_functions the problem were consts which are not functions. I namespaced all functions and if we import only that namespace and use it in messaging, typescript will be happy. I know namespaces are discouraged but i don't know of other ways to separate functions from other parts of a file without creating a new file for them. As for logger, i used a nasty cast because what else can we do? Using namespace also pisses off lint, and pretty seems to break everything, so this patch is purely to make build work. --- src/background.ts | 20 +++++--- src/background/user_actions.ts | 2 +- src/content.ts | 7 ++- src/excmds.ts | 94 ++++++++++++++++++---------------- src/lib/messaging.ts | 24 ++++----- 5 files changed, 78 insertions(+), 69 deletions(-) diff --git a/src/background.ts b/src/background.ts index 0b7a5acb..c3bd9573 100644 --- a/src/background.ts +++ b/src/background.ts @@ -8,7 +8,7 @@ import * as controller from "@src/lib/controller" import * as perf from "@src/perf" import { listenForCounters } from "@src/perf" import * as messaging from "@src/lib/messaging" -import * as excmds_background from "@src/.excmds_background.generated" +import { excmd_functions } from "@src/.excmds_background.generated" import { CmdlineCmds } from "@src/background/commandline_cmds" import { EditorCmds } from "@src/background/editor" import * as convert from "@src/lib/convert" @@ -34,7 +34,7 @@ import * as Proxy from "@src/lib/proxy" // Add various useful modules to the window for debugging ;(window as any).tri = Object.assign(Object.create(null), { messaging, - excmds: excmds_background, + excmds: excmd_functions, convert, config, controller, @@ -60,7 +60,7 @@ import { HintingCmds } from "@src/background/hinting" // background script, will use the excmds that we give to the module // here. controller.setExCmds({ - "": excmds_background, + "": excmd_functions, ex: CmdlineCmds, text: EditorCmds, hint: HintingCmds, @@ -150,7 +150,7 @@ browser.runtime.onStartup.addListener(() => { // Nag people about updates. // Hope that they're on a tab we can access. config.getAsync("update", "nag").then(nag => { - if (nag === true) excmds_background.updatecheck("auto_polite") + if (nag === true) excmd_functions.updatecheck("auto_polite") }) // }}} @@ -249,10 +249,16 @@ browser.tabs.onCreated.addListener(aucon.tabCreatedListener) // An object to collect all of our statistics in one place. const statsLogger: perf.StatsLogger = new perf.StatsLogger() -const messages = { - excmd_background: excmds_background, +const messages: { [key: string]: { [key: string]: (...args: any) => any } } = { + excmd_background: excmd_functions, controller_background: controller, - performance_background: statsLogger, + // We need to present logger object as an [string -> function] map to not be beaten up + // by typescript. + // There are probably better ways to do it, but for now i did this dirty thing just to + // make it build + performance_background: statsLogger as any as { + [key: string]: (...args: any) => any + }, download_background: { downloadUrl: download_background.downloadUrl, downloadUrlAs: download_background.downloadUrlAs, diff --git a/src/background/user_actions.ts b/src/background/user_actions.ts index 9cd7defa..d1f876c6 100644 --- a/src/background/user_actions.ts +++ b/src/background/user_actions.ts @@ -4,7 +4,7 @@ * https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/User_actions */ -import * as excmds from "@src/.excmds_background.generated" +import { excmd_functions as excmds } from "@src/.excmds_background.generated" import * as R from "ramda" import * as config from "@src/lib/config" import { getTridactylTabs } from "@src/background/meta" diff --git a/src/content.ts b/src/content.ts index 52106e99..8fc4f3b5 100644 --- a/src/content.ts +++ b/src/content.ts @@ -83,7 +83,6 @@ try { } const controller = await import("@src/lib/controller") -const excmds_content = await import("@src/.excmds_content.generated") const hinting_content = await import("@src/content/hinting") // Hook the keyboard up to the controller const ContentController = await import("@src/content/controller_content") @@ -91,7 +90,7 @@ const ContentController = await import("@src/content/controller_content") const commandline_content = await import("@src/content/commandline_content") const convert = await import("@src/lib/convert") const dom = await import("@src/lib/dom") -const excmds = await import("@src/.excmds_content.generated") +const excmds = (await import("@src/.excmds_content.generated")).excmd_functions const finding_content = await import("@src/content/finding") const itertools = await import("@src/lib/itertools") const messaging = await import("@src/lib/messaging") @@ -111,14 +110,14 @@ const { tabTgroup } = await import("@src/lib/tab_groups") const completion_providers = await import("@src/completions/providers") controller.setExCmds({ - "": excmds_content, + "": excmds, ex: CmdlineCmds, text: EditorCmds, hint: hinting_content.getHintCommands(), }) messaging.addListener( "excmd_content", - messaging.attributeCaller(excmds_content), + messaging.attributeCaller(excmds), ) messaging.addListener( "controller_content", diff --git a/src/excmds.ts b/src/excmds.ts index e8f92e0d..e115377a 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -120,6 +120,40 @@ import { generator as KEY_MUNCHER } from "@src/content/controller_content" */ export const cmd_params = new Map>() +// I went through the whole list https://developer.mozilla.org/en-US/Firefox/The_about_protocol +// about:blank is even more special +/** @hidden */ +export const ABOUT_WHITELIST = ["about:license", "about:logo", "about:rights", "about:blank"] + +/** The kinds of input elements that we want to be included in the "focusinput" + * command (gi) + * @hidden + */ +export const INPUTTAGS_selectors = ` +input:not([disabled]):not([readonly]):-moz-any( + :not([type]), + [type='text'], + [type='search'], + [type='password'], + [type='datetime'], + [type='datetime-local'], + [type='date'], + [type='month'], + [type='time'], + [type='week'], + [type='number'], + [type='range'], + [type='email'], + [type='url'], + [type='tel'], + [type='color'] +), +textarea:not([disabled]):not([readonly]), +object, +[role='application'], +[contenteditable='true'][role='textbox'] +` + /** @hidden */ const logger = new Logging.Logger("excmd") @@ -144,7 +178,7 @@ import * as gobbleMode from "@src/parsers/gobblemode" import * as nMode from "@src/parsers/nmode" ALL_EXCMDS = { - "": CTSELF, + "": CTSELF.excmd_functions, ex: CtCmdlineCmds, text: CtEditorCmds, } @@ -172,16 +206,29 @@ import * as webrequests from "@src/background/webrequests" import * as commandsHelper from "@src/background/commands" import { tgroups, tgroupActivate, setTabTgroup, setWindowTgroup, setTgroups, windowTgroup, windowLastTgroup, tgroupClearOldInfo, tgroupLastTabId, tgroupTabs, clearAllTgroupInfo, tgroupActivateLast, tgroupHandleTabActivated, tgroupHandleTabCreated, tgroupHandleTabAttached, tgroupHandleTabUpdated, tgroupHandleTabRemoved, tgroupHandleTabDetached } from "./lib/tab_groups" + ALL_EXCMDS = { - "": BGSELF, + "": BGSELF.excmd_functions, ex: BgCmdlineCmds, text: BgEditorCmds, } /** @hidden */ // } +//#background_helper +import { useractions } from "@src/background/user_actions" + +//#content_helper +import { getEditor } from "editor-adapter" + +//#background_helper +import { getTridactylTabs } from "@src/background/meta" + // }}} +// We create a namespace so that we can treat it +// as an object of functions in other parts of tridactyl. +export namespace excmd_functions { // {{{ Native messenger stuff /** @hidden **/ @@ -314,9 +361,6 @@ export function removeTridactylEditorClass(selector: string) { document.querySelector(selector)?.classList.remove("TridactylEditing") } -//#content_helper -import { getEditor } from "editor-adapter" - /** * Opens your favourite editor (which is currently gVim) and fills the last used input with whatever you write into that file. * **Requires that the native messenger is installed, see [[native]] and [[nativeinstall]]**. @@ -1567,8 +1611,6 @@ export async function reloadallbut(hard = false) { return Promise.all(tabs.map(tab => browser.tabs.reload(tab.id, reloadprops))) } -//#background_helper -import { getTridactylTabs } from "@src/background/meta" /** Reloads all tabs which Tridactyl isn't loaded in */ //#background export async function reloaddead(hard = false) { @@ -1584,11 +1626,6 @@ export async function reloadhard(n = 1) { return reload(n, true) } -// I went through the whole list https://developer.mozilla.org/en-US/Firefox/The_about_protocol -// about:blank is even more special -/** @hidden */ -export const ABOUT_WHITELIST = ["about:license", "about:logo", "about:rights", "about:blank"] - /** * Open a new page in the current tab. * @@ -2397,35 +2434,6 @@ export async function loadaucmds(cmdType: "DocStart" | "DocLoad" | "DocEnd" | "T } } -/** The kinds of input elements that we want to be included in the "focusinput" - * command (gi) - * @hidden - */ -export const INPUTTAGS_selectors = ` -input:not([disabled]):not([readonly]):-moz-any( - :not([type]), - [type='text'], - [type='search'], - [type='password'], - [type='datetime'], - [type='datetime-local'], - [type='date'], - [type='month'], - [type='time'], - [type='week'], - [type='number'], - [type='range'], - [type='email'], - [type='url'], - [type='tel'], - [type='color'] -), -textarea:not([disabled]):not([readonly]), -object, -[role='application'], -[contenteditable='true'][role='textbox'] -` - /** Password field selectors * @hidden */ @@ -3788,9 +3796,6 @@ export async function shellescape(...quoteme: string[]) { } } -//#background_helper -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. * @@ -5964,3 +5969,4 @@ export async function elementunhide() { elem.className = elem.className.replace("TridactylKilledElem", "") } // vim: tabstop=4 shiftwidth=4 expandtab +} \ No newline at end of file diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts index 92201b33..c0964895 100644 --- a/src/lib/messaging.ts +++ b/src/lib/messaging.ts @@ -72,27 +72,25 @@ export function attributeCaller(obj) { } interface TypedMessage< - Root, - Type extends keyof Root, - Command extends keyof Root[Type] + Type extends keyof Messages.Background, + Command extends keyof Messages.Background[Type] > { type: Type command: Command - args: Parameters + args: Parameters } function backgroundHandler< - Root, - Type extends keyof Root, - Command extends keyof Root[Type] + Type extends keyof Messages.Background, + Command extends keyof Messages.Background[Type] >( - root: Root, - message: TypedMessage, -): ReturnType { - return root[message.type][message.command](...message.args) + root: Messages.Background, + message: TypedMessage, +): ReturnType { + return root[message.type][message.command](message.args) } -export function setupListener(root: Root) { +export function setupListener(root: Messages.Background) { browser.runtime.onMessage.addListener( (message: any) => { if (message.type in root) { @@ -118,7 +116,7 @@ export async function message< Command extends keyof Messages.Background[Type], F extends((...args: any[]) => any) & Messages.Background[Type][Command] >(type: Type, command: Command, ...args: Parameters) { - const message: TypedMessage = { + const message: TypedMessage = { type, command, args, -- 2.39.2