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 9d239205c7.

* Use Firebase types instead of partial types

* Move Global type to MonkeyTypes

- Also remove duplicate `TimeStats` type

* Create global alias instead of typecasting
This commit is contained in:
Charles Ancheta 2022-02-23 08:18:06 -07:00 committed by GitHub
parent 1ba7a76b86
commit 65654624a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 44 deletions

View file

@ -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;

View file

@ -644,12 +644,16 @@ export function findGetParameter(parameterName: string): string | null {
return result;
}
export function objectToQueryString(obj: object): string {
export function objectToQueryString<T extends string | number | boolean>(
obj: Record<string, T | T[]>
): 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;
};

View file

@ -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);

View file

@ -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;