From 5b4743768ebbfc94b3fb1972e38ee0b0179a89ee Mon Sep 17 00:00:00 2001 From: a Date: Tue, 18 Aug 2020 21:53:38 +0200 Subject: [PATCH] Eslint --- .eslintignore | 1 + .eslintrc.js | 15 ++++++ package.json | 2 +- src/background/gtranslate_commands.ts | 76 +++++++++++++++++++++++++++ src/file.d.ts | 4 ++ src/types.ts | 33 ++++++++++++ 6 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 .eslintignore create mode 100644 .eslintrc.js create mode 100644 src/background/gtranslate_commands.ts create mode 100644 src/file.d.ts create mode 100644 src/types.ts diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..b20d435 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +*.svelte diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..6bb1bf1 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,15 @@ +module.exports = { + root: true, + env: { webextensions: true }, + parser: "@typescript-eslint/parser", + plugins: ["@typescript-eslint"], + extends: [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "prettier/@typescript-eslint", + ], + rules: { + "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/explicit-module-boundary-types": "off", + }, +}; diff --git a/package.json b/package.json index 699c4bb..6865c6b 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,6 @@ } }, "lint-staged": { - "*/**": "prettier --write" + "*/**": ["eslint --fix ","prettier --write"] } } diff --git a/src/background/gtranslate_commands.ts b/src/background/gtranslate_commands.ts new file mode 100644 index 0000000..ae5753e --- /dev/null +++ b/src/background/gtranslate_commands.ts @@ -0,0 +1,76 @@ +import { browser, Runtime } from "webextension-polyfill-ts"; +import { Communicator, commandList, commandFunction } from "../communicator"; +import { newPromise } from "../utils"; + +interface BackgroundCommands extends commandList { + translationFinished: TranslationMessage; + languageList: LanguageListMessage; +} + +interface ContentScriptCommands extends commandList { + translationRequest: TranslationRequest; +} + +interface TranslationMessage extends commandFunction { + args: Translation; +} + +interface LanguageListMessage extends commandFunction { + args: Array; +} + +interface TranslationRequest extends commandFunction { + args: string; +} + +export class BackgroundMessenger extends Communicator< + BackgroundCommands, + ContentScriptCommands +> { + conn: Promise; + private resolvConn: (p: Runtime.Port) => void; + constructor() { + super(); + + [this.conn, this.resolvConn] = newPromise(); + + browser.runtime.onConnect.addListener((p) => { + if (p.name != "gtranslate_scraper_conn") return; + this.resolvConn(p); + p.onDisconnect.addListener( + () => ([this.conn, this.resolvConn] = newPromise()) + ); + p.onMessage.addListener((m) => this.onMessage(m, p.sender!)); + }); + } + + async runCommand( + command: K, + args: ContentScriptCommands[K]["args"] + ) { + const msg = this.getCommandMessage(command, args); + (await this.conn).postMessage(msg); + } +} + +export class ContentScriptMessenger extends Communicator< + ContentScriptCommands, + BackgroundCommands +> { + conn: Runtime.Port; + constructor() { + super(); + this.conn = browser.runtime.connect({ + name: "gtranslate_scraper_conn", + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any); //The declaration file is wrong + this.conn.onMessage.addListener((m) => this.onMessage(m)); + } + async runCommand( + command: K, + args: BackgroundCommands[K]["args"] + ) { + const msg = this.getCommandMessage(command, args); + return this.conn.postMessage(msg); + } +} diff --git a/src/file.d.ts b/src/file.d.ts new file mode 100644 index 0000000..ecba911 --- /dev/null +++ b/src/file.d.ts @@ -0,0 +1,4 @@ +declare module "*?file" { + const file: string; + export = file; +} diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..76f0730 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,33 @@ +import type { Flashcards } from "./background/database"; + +declare global { + interface Translation { + src: string; + result: string; + languages: LanguagePair; + } + + interface Flashcard { + id?: IDBValidKey; + src: string; + result: string; + dateAdded: Date; + exported: boolean; + languages: LanguagePair; + } + + interface LanguagePair { + srcLang: Language; + dstLang: Language; + } + + interface Language { + code: string; + name: string; + } + + interface Window { + hasRun?: boolean; + flashcardDB: Flashcards; + } +}