Skip to content

Commit b5a03e0

Browse files
authored
chore(linting): enable prefer-nullish-coalescing (@Miodec) (monkeytypegame#7209)
1 parent 0c1a8a7 commit b5a03e0

File tree

27 files changed

+131
-199
lines changed

27 files changed

+131
-199
lines changed

backend/src/api/controllers/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ async function updateUser(uid: string): Promise<void> {
221221
{ sort: { wpm: -1, timestamp: 1 } },
222222
)) as DBResult;
223223

224-
if (personalBests[mode.mode] === undefined) personalBests[mode.mode] = {};
224+
personalBests[mode.mode] ??= {};
225225
if (personalBests[mode.mode][mode.mode2] === undefined)
226226
personalBests[mode.mode][mode.mode2] = [];
227227

backend/src/api/controllers/quote.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ export async function submitRating(
125125
shouldUpdateRating,
126126
);
127127

128-
if (!userQuoteRatings[language]) {
129-
userQuoteRatings[language] = {};
130-
}
128+
userQuoteRatings[language] ??= {};
131129
userQuoteRatings[language][quoteId] = rating;
132130

133131
await updateQuoteRatings(uid, userQuoteRatings);

backend/src/api/controllers/result.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,12 @@ export async function updateTags(
170170
await ResultDAL.updateTags(uid, resultId, tagIds);
171171
const result = await ResultDAL.getResult(uid, resultId);
172172

173-
if (!result.difficulty) {
174-
result.difficulty = "normal";
175-
}
176-
if (!(result.language ?? "")) {
177-
result.language = "english";
178-
}
179-
if (result.funbox === undefined) {
180-
result.funbox = [];
181-
}
182-
if (!result.lazyMode) {
183-
result.lazyMode = false;
184-
}
185-
if (!result.punctuation) {
186-
result.punctuation = false;
187-
}
188-
if (!result.numbers) {
189-
result.numbers = false;
190-
}
173+
result.difficulty ??= "normal";
174+
result.language ??= "english";
175+
result.funbox ??= [];
176+
result.lazyMode ??= false;
177+
result.punctuation ??= false;
178+
result.numbers ??= false;
191179

192180
const user = await UserDAL.getPartialUser(uid, "update tags", ["tags"]);
193181
const tagPbs = await UserDAL.checkIfTagPb(uid, user, result);

backend/src/dal/result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function addResult(
2121
const { data: user } = await tryCatch(getUser(uid, "add result"));
2222

2323
if (!user) throw new MonkeyError(404, "User not found", "add result");
24-
if (result.uid === undefined) result.uid = uid;
24+
result.uid ??= uid;
2525
// result.ir = true;
2626
const res = await getResultCollection().insertOne(result);
2727
return {

backend/src/dal/user.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export async function findByName(name: string): Promise<DBUser | undefined> {
275275
{ collation: { locale: "en", strength: 1 } },
276276
);
277277

278-
return found !== null ? found : undefined;
278+
return found ?? undefined;
279279
}
280280

281281
export async function isNameAvailable(
@@ -1053,10 +1053,14 @@ export async function updateInbox(
10531053
.filter((it) => it.type === "badge")
10541054
.map((it) => it.item);
10551055

1056+
// mongo doesnt support ??= i think
1057+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
10561058
if (inventory === null)
10571059
inventory = {
10581060
badges: [],
10591061
};
1062+
// mongo doesnt support ??= i think
1063+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
10601064
if (inventory.badges === null) inventory.badges = [];
10611065

10621066
const uniqueBadgeIds = new Set();

backend/src/jobs/update-leaderboards.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function updateLeaderboardAndNotifyChanges(
4646
const isRecentRecord =
4747
record.timestamp > Date.now() - RECENT_AGE_MILLISECONDS;
4848

49-
return (userImprovedRank || newUserInTop10) && isRecentRecord;
49+
return (userImprovedRank === true || newUserInTop10) && isRecentRecord;
5050
});
5151

5252
if (newRecords.length > 0) {

backend/src/middlewares/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export function authenticateTsRestRequest<
6060
let authType = "None";
6161

6262
const isPublic =
63-
options.isPublic || (options.isPublicOnDev && isDevEnvironment());
63+
options.isPublic === true ||
64+
(options.isPublicOnDev && isDevEnvironment());
6465

6566
const {
6667
authorization: authHeader,
@@ -241,7 +242,7 @@ async function authenticateWithApeKey(
241242
options: RequestAuthenticationOptions,
242243
): Promise<DecodedToken> {
243244
const isPublic =
244-
options.isPublic || (options.isPublicOnDev && isDevEnvironment());
245+
options.isPublic === true || (options.isPublicOnDev && isDevEnvironment());
245246

246247
if (!isPublic) {
247248
if (!configuration.apeKeys.acceptKeys) {

backend/src/utils/misc.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ export function isDevEnvironment(): boolean {
195195
export function getFrontendUrl(): string {
196196
return isDevEnvironment()
197197
? "http://localhost:3000"
198-
: process.env["FRONTEND_URL"] !== undefined
199-
? process.env["FRONTEND_URL"]
200-
: "https://monkeytype.com";
198+
: (process.env["FRONTEND_URL"] ?? "https://monkeytype.com");
201199
}
202200

203201
/**

frontend/src/ts/controllers/chart-controller.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,7 @@ export const accountHistory = new ChartWithUpdateColors<
594594
label += resultData.mode2;
595595
}
596596

597-
let diff = resultData.difficulty;
598-
if (diff === undefined) {
599-
diff = "normal";
600-
}
597+
let diff = resultData.difficulty ?? "normal";
601598
label += `\ndifficulty: ${diff}`;
602599

603600
label +=

frontend/src/ts/controllers/pw-ad-controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ export function init(): void {
146146
headOfDocument.appendChild(rampScript);
147147

148148
window._pwGA4PageviewId = "".concat(Date.now());
149-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unsafe-assignment
149+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/prefer-nullish-coalescing
150150
window.dataLayer = window.dataLayer || [];
151151
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
152152
window.gtag =
153-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
153+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
154154
window.gtag ||
155155
function (): void {
156156
// eslint-disable-next-line prefer-rest-params

0 commit comments

Comments
 (0)