From 65654624a6c6b902fcc93bcbd27ea8dc2b389633 Mon Sep 17 00:00:00 2001 From: Charles Ancheta <55412395+cbebe@users.noreply.github.com> Date: Wed, 23 Feb 2022 08:18:06 -0700 Subject: [PATCH] Remove some `@ts-ignores` in frontend scripts (#2575) * Remove some `@ts-ignores` in frontend scripts - Create interfaces for `String` prototype and the `global` object - Create type for errors in catch blocks - Create partial type for Firebase user object - Fix function signature of `objectToQueryString` - Use strict equality on string comparisons * Use Firebase types instead of partial types * Revert "Use Firebase types instead of partial types" This reverts commit 9d239205c797050b55d4e8f5e89fb6ea897b16bd. * Use Firebase types instead of partial types * Move Global type to MonkeyTypes - Also remove duplicate `TimeStats` type * Create global alias instead of typecasting --- frontend/src/scripts/index.ts | 37 ++++++++------------ frontend/src/scripts/misc.ts | 22 ++++++++---- frontend/src/scripts/popups/simple-popups.ts | 28 +++++++-------- frontend/src/scripts/types/types.d.ts | 13 +++++++ 4 files changed, 56 insertions(+), 44 deletions(-) diff --git a/frontend/src/scripts/index.ts b/frontend/src/scripts/index.ts index 8a4ffeb84..f82a59a38 100644 --- a/frontend/src/scripts/index.ts +++ b/frontend/src/scripts/index.ts @@ -1,5 +1,4 @@ -/* eslint-disable no-unused-vars */ -//this file should be concatenated at the top of the legacy js files +// this file should be concatenated at the top of the legacy js files // @ts-ignore import Chart from "chart.js"; @@ -36,32 +35,26 @@ import "./popups/mobile-test-config-popup"; import "./popups/edit-tags-popup"; import * as Account from "./pages/account"; -// @ts-ignore -global.snapshot = DB.getSnapshot; +type ExtendedGlobal = typeof globalThis & MonkeyTypes.Global; -// @ts-ignore -global.config = Config; +const extendedGlobal = global as ExtendedGlobal; -// @ts-ignore -global.toggleFilterDebug = Account.toggleFilterDebug; +extendedGlobal.snapshot = DB.getSnapshot; -// @ts-ignore -global.glarsesMode = enable; +extendedGlobal.config = Config; -// @ts-ignore -global.stats = TestStats.getStats; +extendedGlobal.toggleFilterDebug = Account.toggleFilterDebug; -// @ts-ignore -global.replay = Replay.getReplayExport; +extendedGlobal.glarsesMode = enable; -// @ts-ignore -global.enableTimerDebug = TestTimer.enableTimerDebug; +extendedGlobal.stats = TestStats.getStats; -// @ts-ignore -global.getTimerStats = TestTimer.getTimerStats; +extendedGlobal.replay = Replay.getReplayExport; -// @ts-ignore -global.toggleUnsmoothedRaw = Result.toggleUnsmoothedRaw; +extendedGlobal.enableTimerDebug = TestTimer.enableTimerDebug; -// @ts-ignore -global.enableSpacingDebug = TestInput.enableSpacingDebug; +extendedGlobal.getTimerStats = TestTimer.getTimerStats; + +extendedGlobal.toggleUnsmoothedRaw = Result.toggleUnsmoothedRaw; + +extendedGlobal.enableSpacingDebug = TestInput.enableSpacingDebug; diff --git a/frontend/src/scripts/misc.ts b/frontend/src/scripts/misc.ts index 83c94fb7b..9d3f7c41b 100644 --- a/frontend/src/scripts/misc.ts +++ b/frontend/src/scripts/misc.ts @@ -644,12 +644,16 @@ export function findGetParameter(parameterName: string): string | null { return result; } -export function objectToQueryString(obj: object): string { +export function objectToQueryString( + obj: Record +): string { const str = []; for (const p in obj) if (Object.prototype.hasOwnProperty.call(obj, p)) { - // @ts-ignore //todo help - str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); + // Arrays get encoded as a comma(%2C)-separated list + str.push( + encodeURIComponent(p) + "=" + encodeURIComponent(obj[p] as unknown as T) + ); } return str.join("&"); } @@ -744,8 +748,7 @@ export function cleanTypographySymbols(textToClean: string): string { }; return textToClean.replace( /[“”’‘—,…«»–\u2007\u202F\u00A0]/g, - // @ts-ignore - (char) => specials[char] || "" + (char) => specials[char as keyof typeof specials] || "" ); } @@ -842,8 +845,13 @@ export function convertRGBtoHEX(rgb: string): string | undefined { return "#" + hexCode(match[1]) + hexCode(match[2]) + hexCode(match[3]); } -// @ts-ignore -String.prototype.lastIndexOfRegex = function (regex: RegExp): number { +interface LastIndex extends String { + lastIndexOfRegex(regex: RegExp): number; +} + +(String.prototype as LastIndex).lastIndexOfRegex = function ( + regex: RegExp +): number { const match = this.match(regex); return match ? this.lastIndexOf(match[match.length - 1]) : -1; }; diff --git a/frontend/src/scripts/popups/simple-popups.ts b/frontend/src/scripts/popups/simple-popups.ts index ab9d1bfa6..74ec39194 100644 --- a/frontend/src/scripts/popups/simple-popups.ts +++ b/frontend/src/scripts/popups/simple-popups.ts @@ -1,3 +1,4 @@ +import type FirebaseTypes from "firebase"; import Ape from "../ape"; import * as AccountController from "../controllers/account-controller"; import * as DB from "../db"; @@ -135,7 +136,6 @@ class SimplePopup { $.each($("#simplePopup input"), (_, el) => { vals.push($(el).val() as string); }); - // @ts-ignore todo remove this.execFn(this, ...vals); this.hide(); } @@ -251,8 +251,8 @@ list["updateEmail"] = new SimplePopup( window.location.reload(); }, 1000); } catch (e) { - // @ts-ignore todo help - if (e.code == "auth/wrong-password") { + const typedError = e as FirebaseTypes.FirebaseError; + if (typedError.code === "auth/wrong-password") { Notifications.add("Incorrect password", -1); } else { Notifications.add("Something went wrong: " + e, -1); @@ -260,9 +260,8 @@ list["updateEmail"] = new SimplePopup( } }, (thisPopup) => { - const user = firebase.auth().currentUser; - // @ts-ignore todo remove ignore once firebase is initialised with code - if (!user.providerData.find((p) => p.providerId === "password")) { + const user: FirebaseTypes.User = firebase.auth().currentUser; + if (!user.providerData.find((p) => p?.providerId === "password")) { thisPopup.inputs = []; thisPopup.buttonText = ""; thisPopup.text = "Password authentication is not enabled"; @@ -324,8 +323,8 @@ list["updateName"] = new SimplePopup( DB.getSnapshot().name = newName; $("#menu .icon-button.account .text").text(newName); } catch (e) { - // @ts-ignore todo remove ignore - if (e.code === "auth/wrong-password") { + const typedError = e as FirebaseTypes.FirebaseError; + if (typedError.code === "auth/wrong-password") { Notifications.add("Incorrect password", -1); } else { Notifications.add("Something went wrong: " + e, -1); @@ -385,9 +384,9 @@ list["updatePassword"] = new SimplePopup( window.location.reload(); }, 1000); } catch (e) { + const typedError = e as FirebaseTypes.FirebaseError; Loader.hide(); - // @ts-ignore todo remove ignore - if (e.code == "auth/wrong-password") { + if (typedError.code === "auth/wrong-password") { Notifications.add("Incorrect password", -1); } else { Notifications.add("Something went wrong: " + e, -1); @@ -395,9 +394,8 @@ list["updatePassword"] = new SimplePopup( } }, (thisPopup) => { - const user = firebase.auth().currentUser; - // @ts-ignore todo remove ignore - if (!user.providerData.find((p) => p.providerId === "password")) { + const user: FirebaseTypes.User = firebase.auth().currentUser; + if (!user.providerData.find((p) => p?.providerId === "password")) { thisPopup.inputs = []; thisPopup.buttonText = ""; thisPopup.text = "Password authentication is not enabled"; @@ -513,9 +511,9 @@ list["deleteAccount"] = new SimplePopup( location.reload(); }, 3000); } catch (e) { + const typedError = e as FirebaseTypes.FirebaseError; Loader.hide(); - // @ts-ignore todo remove ignore - if (e.code == "auth/wrong-password") { + if (typedError.code === "auth/wrong-password") { Notifications.add("Incorrect password", -1); } else { Notifications.add("Something went wrong: " + e, -1); diff --git a/frontend/src/scripts/types/types.d.ts b/frontend/src/scripts/types/types.d.ts index 3046025fd..8e3ab6af6 100644 --- a/frontend/src/scripts/types/types.d.ts +++ b/frontend/src/scripts/types/types.d.ts @@ -455,6 +455,19 @@ declare namespace MonkeyTypes { nextDelay: number; } + interface Global { + snapshot(): Snapshot; + config: Config; + toggleFilterDebug(): void; + glarsesMode(): void; + stats(): void; + replay(): string; + enableTimerDebug(): void; + getTimerStats(): TimerStats[]; + toggleUnsmoothedRaw(): void; + enableSpacingDebug(): void; + } + interface GithubRelease { url: string; assets_url: string;