chore(lint): enable no unsafe return

This commit is contained in:
Miodec 2024-07-30 22:10:30 +02:00
parent 20c2fbc116
commit 47438fa8d1
22 changed files with 107 additions and 33 deletions

View file

@ -60,9 +60,9 @@
"winston": "3.6.0"
},
"devDependencies": {
"@monkeytype/eslint-config": "*",
"@monkeytype/shared-types": "*",
"@monkeytype/typescript-config": "*",
"@monkeytype/eslint-config": "*",
"@redocly/cli": "1.18.1",
"@types/bcrypt": "5.0.2",
"@types/cors": "2.8.12",
@ -70,6 +70,7 @@
"@types/express": "4.17.21",
"@types/ioredis": "4.28.10",
"@types/lodash": "4.14.178",
"@types/mjml": "4.7.4",
"@types/mustache": "4.2.2",
"@types/node": "20.14.11",
"@types/node-fetch": "2.6.1",

View file

@ -24,6 +24,8 @@ export async function handleReports(
accept: boolean
): Promise<MonkeyResponse> {
const { reports } = req.body;
// TODO: remove once this gets converted to ts-rest
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
const reportIds = reports.map(({ reportId }) => reportId);
const reportsFromDb = await ReportDAL.getReports(reportIds);

View file

@ -185,8 +185,11 @@ async function updateUser(uid: string): Promise<void> {
])
.toArray();
const timeTyping = stats.reduce((a, c) => a + c["timeTyping"], 0);
const completedTests = stats.reduce((a, c) => a + c["completedTests"], 0);
const timeTyping = stats.reduce((a, c) => (a + c["timeTyping"]) as number, 0);
const completedTests = stats.reduce(
(a, c) => (a + c["completedTests"]) as number,
0
);
//update PBs
const lbPersonalBests: MonkeyTypes.LbPersonalBests = {
@ -203,7 +206,15 @@ async function updateUser(uid: string): Promise<void> {
zen: {},
quote: {},
};
const modes = stats.map((it) => it["_id"]);
const modes = stats.map(
(it) =>
it["_id"] as {
language: string;
mode: "time" | "custom" | "words" | "quote" | "zen";
mode2: `${number}` | "custom" | "zen";
}
);
for (const mode of modes) {
const best = (
await ResultDal.getResultCollection()

View file

@ -75,7 +75,7 @@ const usernameValidation = joi
return helpers.error("string.pattern.base");
}
return value;
return value as string;
})
.messages({
"string.profanity":
@ -537,7 +537,7 @@ const profileDetailsBase = joi
return helpers.error("string.profanity");
}
return value;
return value as string;
})
.messages({
"string.profanity":

View file

@ -35,12 +35,21 @@ export async function getSpeedHistogram(
language: string,
mode: string,
mode2: string
): Promise<Record<string, number>> {
const key = `${language}_${mode}_${mode2}`;
): Promise<SpeedHistogram> {
const key = `${language}_${mode}_${mode2}` as keyof PublicSpeedStatsDB;
if (key === "_id") {
throw new MonkeyError(
400,
"Invalid speed histogram key",
"get speed histogram"
);
}
const stats = await db
.collection<PublicSpeedStatsDB>("public")
.findOne({ _id: "speedStatsHistogram" }, { projection: { [key]: 1 } });
return stats?.[key] ?? {};
}

View file

@ -742,10 +742,10 @@ export async function getPersonalBests(
]);
if (mode2 !== undefined) {
return user.personalBests?.[mode]?.[mode2];
return user.personalBests?.[mode]?.[mode2] as PersonalBest;
}
return user.personalBests?.[mode];
return user.personalBests?.[mode] as PersonalBest;
}
export async function getStats(

View file

@ -42,9 +42,12 @@ export function withApeRateLimiter(
return (req: MonkeyTypes.Request, res: Response, next: NextFunction) => {
if (req.ctx.decodedToken.type === "ApeKey") {
const rateLimiter = apeRateLimiterOverride ?? apeRateLimiter;
// TODO: bump version?
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return rateLimiter(req, res, next);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return defaultRateLimiter(req, res, next);
};
}

View file

@ -109,7 +109,7 @@ export class WeeklyXpLeaderboard {
entry.uid,
xpGained,
JSON.stringify({ ...entry, timeTypedSeconds: totalTimeTypedSeconds })
),
) as Promise<number>,
LaterQueue.scheduleForNextWeek(
"weekly-xp-leaderboard-results",
"weekly-xp"
@ -155,11 +155,16 @@ export class WeeklyXpLeaderboard {
}
const resultsWithRanks: WeeklyXpLeaderboardEntry[] = results.map(
(resultJSON: string, index: number) => ({
...JSON.parse(resultJSON),
rank: minRank + index + 1,
totalXp: parseInt(scores[index] as string, 10),
})
(resultJSON: string, index: number) => {
//TODO parse with zod?
const parsed = JSON.parse(resultJSON) as WeeklyXpLeaderboardEntry;
return {
...parsed,
rank: minRank + index + 1,
totalXp: parseInt(scores[index] as string, 10),
};
}
);
return resultsWithRanks;
@ -193,11 +198,17 @@ export class WeeklyXpLeaderboard {
return null;
}
//TODO parse with zod?
const parsed = JSON.parse(result ?? "null") as Omit<
WeeklyXpLeaderboardEntry,
"rank" | "count" | "totalXp"
>;
return {
rank: rank + 1,
count: count ?? 0,
totalXp: parseInt(totalXp, 10),
...JSON.parse(result ?? "null"),
...parsed,
};
}
}

View file

@ -91,7 +91,7 @@ export class DailyLeaderboard {
const resultScore = kogascore(entry.wpm, entry.acc, entry.timestamp);
// @ts-expect-error
const rank = await connection.addResult(
const rank = (await connection.addResult(
2,
leaderboardScoresKey,
leaderboardResultsKey,
@ -100,7 +100,7 @@ export class DailyLeaderboard {
entry.uid,
resultScore,
JSON.stringify(entry)
);
)) as number;
if (
isValidModeRule(
@ -153,10 +153,15 @@ export class DailyLeaderboard {
}
const resultsWithRanks: LbEntryWithRank[] = results.map(
(resultJSON, index) => ({
...JSON.parse(resultJSON),
rank: minRank + index + 1,
})
(resultJSON, index) => {
// TODO: parse with zod?
const parsed = JSON.parse(resultJSON) as LbEntryWithRank;
return {
...parsed,
rank: minRank + index + 1,
};
}
);
if (!premiumFeaturesEnabled) {

View file

@ -101,10 +101,12 @@ function apeifyClientMethod(
errorMessage = typedError.message;
if (isAxiosError(typedError)) {
const data = typedError.response?.data as { data: TData };
return {
status: typedError.response?.status ?? 500,
message: typedError.message,
...typedError.response?.data,
...data,
};
}
}

View file

@ -36,5 +36,6 @@ export function getResponse(id: string): string {
}
//@ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return grecaptcha.getResponse(captchas[id]);
}

View file

@ -95,6 +95,7 @@ class ChartWithUpdateColors<
id: DatasetIds extends never ? never : "x" | DatasetIds
): DatasetIds extends never ? never : CartesianScaleOptions {
//@ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.options.scales[id];
}
}

View file

@ -28,7 +28,8 @@ function getParams(match: {
(result) => result[1]
);
return Object.fromEntries(keys.map((key, index) => [key, values[index]]));
const a = keys.map((key, index) => [key, values[index]]);
return Object.fromEntries(a) as Record<string, string>;
}
type Route = {

View file

@ -221,7 +221,7 @@ export async function setFilterPreset(id: string): Promise<void> {
}
function deepCopyFilter(filter: ResultFilters): ResultFilters {
return JSON.parse(JSON.stringify(filter));
return JSON.parse(JSON.stringify(filter)) as ResultFilters;
}
function addFilterPresetToSnapshot(filter: ResultFilters): void {

View file

@ -11,7 +11,12 @@ function clearMemory(): void {
}
function getMemory(): string[] {
return JSON.parse(window.localStorage.getItem("confirmedPSAs") ?? "[]") ?? [];
//TODO verify with zod?
return (
(JSON.parse(
window.localStorage.getItem("confirmedPSAs") ?? "[]"
) as string[]) ?? []
);
}
function setMemory(id: string): void {

View file

@ -57,7 +57,9 @@ export class TestActivityCalendar implements MonkeyTypes.TestActivityCalendar {
lastDay: Date
): (number | null | undefined)[] {
//fill calendar with enough values
const values = new Array(Math.max(0, 386 - data.length)).fill(undefined);
const values: (number | null | undefined)[] = new Array(
Math.max(0, 386 - data.length)
).fill(undefined);
values.push(...data);
//discard values outside the calendar range

View file

@ -13,7 +13,8 @@ type Accepted = {
function getAcceptedObject(): Accepted | null {
const acceptedCookies = localStorage.getItem("acceptedCookies") ?? "";
if (acceptedCookies) {
return JSON.parse(acceptedCookies);
//TODO verify with zod?
return JSON.parse(acceptedCookies) as Accepted;
} else {
return null;
}

View file

@ -9,7 +9,9 @@ import { Mode, Mode2 } from "@monkeytype/contracts/schemas/shared";
import { CustomTextData } from "@monkeytype/shared-types";
function getCheckboxValue(checkbox: string): boolean {
return $(`#shareTestSettingsModal label.${checkbox} input`).prop("checked");
return $(`#shareTestSettingsModal label.${checkbox} input`).prop(
"checked"
) as boolean;
}
type SharedTestSettings = [

View file

@ -874,7 +874,7 @@ export async function finish(difficultyFailed = false): Promise<void> {
return 1;
} else if (typeof input === "object" && input !== null) {
return Object.values(input).reduce(
(a, b) => a + countUndefined(b),
(a, b) => (a + countUndefined(b)) as number,
0
) as number;
} else {

View file

@ -11,7 +11,7 @@ async function fetchJson<T>(url: string): Promise<T> {
if (!url) throw new Error("No URL");
const res = await fetch(url);
if (res.ok) {
return await res.json();
return (await res.json()) as T;
} else {
throw new Error(`${res.status} ${res.statusText}`);
}

18
package-lock.json generated
View file

@ -93,6 +93,7 @@
"@types/express": "4.17.21",
"@types/ioredis": "4.28.10",
"@types/lodash": "4.14.178",
"@types/mjml": "4.7.4",
"@types/mustache": "4.2.2",
"@types/node": "20.14.11",
"@types/node-fetch": "2.6.1",
@ -6884,6 +6885,23 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/mjml": {
"version": "4.7.4",
"resolved": "https://registry.npmjs.org/@types/mjml/-/mjml-4.7.4.tgz",
"integrity": "sha512-vyi1vzWgMzFMwZY7GSZYX0GU0dmtC8vLHwpgk+NWmwbwRSrlieVyJ9sn5elodwUfklJM7yGl0zQeet1brKTWaQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/mjml-core": "*"
}
},
"node_modules/@types/mjml-core": {
"version": "4.15.0",
"resolved": "https://registry.npmjs.org/@types/mjml-core/-/mjml-core-4.15.0.tgz",
"integrity": "sha512-jSRWTOpwRS/uHIBfGdvLl0a7MaoBZZYHKI+HhsFYChrUOKVJTnjSYsuV6wx0snv6ZaX3TUo5OP/gNsz/uzZz1A==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/mustache": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-4.2.2.tgz",

View file

@ -70,7 +70,6 @@ module.exports = {
"@typescript-eslint/restrict-plus-operands": "off",
// TODO: enable at some point
"@typescript-eslint/no-unsafe-return": "off", //~12
"@typescript-eslint/no-unsafe-assignment": "off", //~63
"@typescript-eslint/no-unsafe-argument": "off", //~37
"@typescript-eslint/no-unsafe-call": "off", //~76