From f12d90ffa6c8fbd17344bd9e824b2178ebcd8ed6 Mon Sep 17 00:00:00 2001 From: Tim Brust Date: Wed, 28 Aug 2024 11:22:10 +0200 Subject: [PATCH] build: enable more strict ESLint ruleset --- eslint.config.mjs | 19 ++++++++++++++++++- src/cookies/basket-adder.ts | 8 ++++---- src/core/browser-manager.ts | 11 ++++++----- src/core/cooldown-manager.ts | 8 ++++++-- src/index.ts | 1 + src/notifiers/discord-notifier.ts | 6 ++---- src/stock-checkers/category-checker.ts | 8 ++++++-- src/stock-checkers/search-checker.ts | 8 ++++++-- src/stock-checkers/wishlist-checker.ts | 8 ++++++-- 9 files changed, 55 insertions(+), 22 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 957522a7e..5a6d7aada 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -6,7 +6,12 @@ import eslintPluginPrettier from "eslint-plugin-prettier/recommended"; export default tseslint.config({ files: ["**/*.ts"], - extends: [eslint.configs.recommended, ...tseslint.configs.strict, ...tseslint.configs.stylistic, eslintPluginPrettier], + extends: [ + eslint.configs.recommended, + ...tseslint.configs.strictTypeChecked, + ...tseslint.configs.stylisticTypeChecked, + eslintPluginPrettier, + ], rules: { "@typescript-eslint/no-unused-vars": [ "error", @@ -20,6 +25,12 @@ export default tseslint.config({ ignoreRestSiblings: true, }, ], + "@typescript-eslint/restrict-template-expressions": [ + "error", + { + allowNumber: true, + }, + ], "prettier/prettier": [ "error", { @@ -27,4 +38,10 @@ export default tseslint.config({ }, ], }, + languageOptions: { + parserOptions: { + projectService: true, + tsconfigDirName: import.meta.dirname, + }, + }, }); diff --git a/src/cookies/basket-adder.ts b/src/cookies/basket-adder.ts index e0fbbf413..a3615c33f 100644 --- a/src/cookies/basket-adder.ts +++ b/src/cookies/basket-adder.ts @@ -141,14 +141,14 @@ export class BasketAdder { status: addProductResponse.status, body: data, })) - .catch((_) => ({ + .catch((_: unknown) => ({ success: false, status: addProductResponse.status, body: null, retryAfterHeader: addProductResponse.headers.get("Retry-After"), })), ) - .catch((_) => ({ success: false, status: -2, body: null })), + .catch((_: unknown) => ({ success: false, status: -2, body: null })), this.store, id, v4(), @@ -168,8 +168,8 @@ export class BasketAdder { if (res.success) { try { - const basketCookie = (await this.browserManager.page.cookies()).filter((cookie) => cookie.name === "r")[0]; - if (basketCookie.value) { + const basketCookie = (await this.browserManager.page.cookies()).find((cookie) => cookie.name === "r"); + if (basketCookie) { cookies.push(basketCookie.value); this.logger.info( `Made cookie ${basketCookie.value} for product ${id}: ${this.store.baseUrl}?cookie=${basketCookie.value}`, diff --git a/src/core/browser-manager.ts b/src/core/browser-manager.ts index 526b36276..3ce37ead2 100644 --- a/src/core/browser-manager.ts +++ b/src/core/browser-manager.ts @@ -129,13 +129,13 @@ export class BrowserManager { loginResponse .json() .then((data: LoginResponse) => ({ status: loginResponse.status, body: data })) - .catch((_) => ({ + .catch((_: unknown) => ({ status: loginResponse.status, body: null, retryAfterHeader: loginResponse.headers.get("Retry-After"), })), ) - .catch((_) => ({ status: -2, body: null })), + .catch((_: unknown) => ({ status: -2, body: null })), this.store, email, password, @@ -235,13 +235,13 @@ export class BrowserManager { loginResponse .json() .then((data: LoginResponse) => ({ status: loginResponse.status, body: data })) - .catch((_) => ({ + .catch((_: unknown) => ({ status: loginResponse.status, body: null, retryAfterHeader: loginResponse.headers.get("Retry-After"), })), ) - .catch((_) => ({ status: -2, body: null })), + .catch((_: unknown) => ({ status: -2, body: null })), this.store, email, password, @@ -423,10 +423,11 @@ export class BrowserManager { Object.defineProperty(HTMLDivElement.prototype, "offsetHeight", { ...elementDescriptor, get: function () { - if (this.id === "modernizr") { + if ((this as HTMLElement).id === "modernizr") { return 1; } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return elementDescriptor?.get?.apply(this); }, }); diff --git a/src/core/cooldown-manager.ts b/src/core/cooldown-manager.ts index d01f51023..b414a0d64 100644 --- a/src/core/cooldown-manager.ts +++ b/src/core/cooldown-manager.ts @@ -18,7 +18,9 @@ export class CooldownManager { constructor(storeConfig: StoreConfiguration) { if (existsSync("basket-cooldowns.json")) { try { - this.basketCooldowns = new Map(JSON.parse(readFileSync("basket-cooldowns.json", "utf-8"))); + this.basketCooldowns = new Map( + JSON.parse(readFileSync("basket-cooldowns.json", "utf-8")) as Iterable, + ); } catch { this.basketCooldowns = new Map(); } @@ -26,7 +28,9 @@ export class CooldownManager { if (existsSync("cooldowns.json")) { try { - this.cooldowns = new Map(JSON.parse(readFileSync("cooldowns.json", "utf-8"))); + this.cooldowns = new Map( + JSON.parse(readFileSync("cooldowns.json", "utf-8")) as Iterable, + ); } catch { this.cooldowns = new Map(); } diff --git a/src/index.ts b/src/index.ts index 9352f6803..8246bef4f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -170,6 +170,7 @@ void (async function () { const wishlistRaceTimeout = 60000; const loginRaceTimeout = 30000; + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition while (shouldRun) { try { logger.info("🤖 Beep, I'm alive and well checking your stock"); diff --git a/src/notifiers/discord-notifier.ts b/src/notifiers/discord-notifier.ts index 52fbdc953..aaa8f9a1f 100644 --- a/src/notifiers/discord-notifier.ts +++ b/src/notifiers/discord-notifier.ts @@ -138,13 +138,11 @@ export class DiscordNotifier implements Notifier { }, ]); if (this.showMagicianLink) { - embed.addFields([ - { name: "Magician", value: `${this.productHelper.getProductURL(item, this.store, this.replacements, true)}` }, - ]); + embed.addFields([{ name: "Magician", value: this.productHelper.getProductURL(item, this.store, this.replacements, true) }]); } if (this.showCookiesAmount) { embed.addFields([ - { name: "Cookies", value: cookiesAmount ? `${cookiesAmount} 🍪` : `${this.noCookieEmoji ?? "👎"}`, inline: true }, + { name: "Cookies", value: cookiesAmount ? `${cookiesAmount} 🍪` : (this.noCookieEmoji ?? "👎"), inline: true }, ]); } embed.addFields([{ name: "Availability State", value: item.availability.delivery?.availabilityType ?? "UNKNOWN", inline: true }]); diff --git a/src/stock-checkers/category-checker.ts b/src/stock-checkers/category-checker.ts index 73be0d7f5..254e41ab3 100644 --- a/src/stock-checkers/category-checker.ts +++ b/src/stock-checkers/category-checker.ts @@ -180,9 +180,13 @@ export class CategoryChecker { res .json() .then((data: CategoryResponse) => ({ status: res.status, body: data })) - .catch((_) => ({ status: res.status, body: null, retryAfterHeader: res.headers.get("Retry-After") })), + .catch((_: unknown) => ({ + status: res.status, + body: null, + retryAfterHeader: res.headers.get("Retry-After"), + })), ) - .catch((_) => ({ status: -2, body: null })), + .catch((_: unknown) => ({ status: -2, body: null })), this.store, page, category, diff --git a/src/stock-checkers/search-checker.ts b/src/stock-checkers/search-checker.ts index bb854e967..ffa85cb63 100644 --- a/src/stock-checkers/search-checker.ts +++ b/src/stock-checkers/search-checker.ts @@ -186,9 +186,13 @@ export class SearchChecker { res .json() .then((data: SearchResponse) => ({ status: res.status, body: data })) - .catch((_) => ({ status: res.status, body: null, retryAfterHeader: res.headers.get("Retry-After") })), + .catch((_: unknown) => ({ + status: res.status, + body: null, + retryAfterHeader: res.headers.get("Retry-After"), + })), ) - .catch((_) => ({ status: -2, body: null })), + .catch((_: unknown) => ({ status: -2, body: null })), this.store, page, searchQuery, diff --git a/src/stock-checkers/wishlist-checker.ts b/src/stock-checkers/wishlist-checker.ts index d29933641..b560e7f14 100644 --- a/src/stock-checkers/wishlist-checker.ts +++ b/src/stock-checkers/wishlist-checker.ts @@ -167,9 +167,13 @@ export class WishlistChecker { res .json() .then((data: WishlistResponse) => ({ status: res.status, body: data })) - .catch((_) => ({ status: res.status, body: null, retryAfterHeader: res.headers.get("Retry-After") })), + .catch((_: unknown) => ({ + status: res.status, + body: null, + retryAfterHeader: res.headers.get("Retry-After"), + })), ) - .catch((_) => ({ status: -2, body: null })), + .catch((_: unknown) => ({ status: -2, body: null })), this.store, offset, v4(),