From 66b2c550b4de85d0e43da31d59a8b681eab41d66 Mon Sep 17 00:00:00 2001 From: TonyRL Date: Tue, 14 Jul 2026 00:37:46 +0800 Subject: [PATCH] chore: fix typing --- .oxlintrc.json | 8 ++++++++ lib/routes/ai-bot/daily-ai-news.ts | 8 +++----- lib/routes/dailypush/utils.ts | 11 ++++++----- lib/routes/f95zone/thread.ts | 4 ++-- lib/routes/gigazine/en.ts | 6 +++--- lib/routes/whu/rsgis.ts | 5 +++-- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.oxlintrc.json b/.oxlintrc.json index 6a3ed7f70..b937c7658 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -602,6 +602,14 @@ { "selector": "CallExpression[callee.property.name=\"map\"] > FunctionExpression", "message": "Use an arrow function instead." + }, + { + "selector": "TSTypeReference[typeName.name='ReturnType'] TSTypeQuery[exprName.name='load']", + "message": "Usage of ReturnType is not allowed. Please use appropriate types from cheerio instead." + }, + { + "selector": "TSTypeReference[typeName.name='ReturnType'] TSTypeReference[typeName.name='CheerioAPI']", + "message": "Usage of ReturnType is not allowed. Please use appropriate types from cheerio and domhandler instead." } ], diff --git a/lib/routes/ai-bot/daily-ai-news.ts b/lib/routes/ai-bot/daily-ai-news.ts index 13f42a0cf..d40cb3a59 100755 --- a/lib/routes/ai-bot/daily-ai-news.ts +++ b/lib/routes/ai-bot/daily-ai-news.ts @@ -1,13 +1,11 @@ -import { load } from 'cheerio'; +import { type Cheerio, type CheerioAPI, load } from 'cheerio'; +import type { Element } from 'domhandler'; import type { Data, DataItem, Route } from '@/types'; import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; -type CheerioInstance = ReturnType; -type CheerioSelection = ReturnType; - interface DateContext { currentYear: number; prevMonth: number; @@ -36,7 +34,7 @@ function parseDateString(dateStr: string, ctx: DateContext): Date | undefined { return timezone(parseDate(`${ctx.currentYear}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`), 8); } -function processNewsList($: CheerioInstance, $newsList: CheerioSelection, ctx: DateContext): DataItem[] { +function processNewsList($: CheerioAPI, $newsList: Cheerio, ctx: DateContext): DataItem[] { let currentPubDate: Date | undefined; return $newsList diff --git a/lib/routes/dailypush/utils.ts b/lib/routes/dailypush/utils.ts index 134eca52f..695f0cf75 100644 --- a/lib/routes/dailypush/utils.ts +++ b/lib/routes/dailypush/utils.ts @@ -1,5 +1,6 @@ -import type { CheerioAPI } from 'cheerio'; +import type { Cheerio, CheerioAPI } from 'cheerio'; import { load } from 'cheerio'; +import type { Element } from 'domhandler'; import type { BrowserContext } from 'patchright'; import type { DataItem } from '@/types'; @@ -68,7 +69,7 @@ function tryParseAsDate(text: string): Date | undefined { /** * Extract author from article element */ -function extractAuthor(article: ReturnType): DataItem['author'] { +function extractAuthor(article: Cheerio): DataItem['author'] { const container = article.find('.flex.items-center.gap-3').first(); if (container.length === 0) { return undefined; @@ -134,7 +135,7 @@ function extractAuthor(article: ReturnType): DataItem['author'] { /** * Extract categories/tags from article element */ -function extractCategories(article: ReturnType, $: CheerioAPI): string[] { +function extractCategories(article: Cheerio, $: CheerioAPI): string[] { return article .find('a[href^="/"]') .toArray() @@ -155,7 +156,7 @@ function extractCategories(article: ReturnType, $: CheerioAPI): stri /** * Extract publication date from article element */ -function extractPubDate(article: ReturnType): Date | undefined { +function extractPubDate(article: Cheerio): Date | undefined { const container = article.find('.flex.items-center.gap-3').first(); if (container.length === 0) { return undefined; @@ -208,7 +209,7 @@ function extractPubDate(article: ReturnType): Date | undefined { /** * Parse a single article element into an ArticleItem */ -function parseArticle(article: ReturnType, $: CheerioAPI, baseUrl: string): (DataItem & ArticleItem) | null { +function parseArticle(article: Cheerio, $: CheerioAPI, baseUrl: string): (DataItem & ArticleItem) | null { // Find the title link in h2 > a const titleLink = article.find('h2 a[href^="http"]'); if (titleLink.length === 0) { diff --git a/lib/routes/f95zone/thread.ts b/lib/routes/f95zone/thread.ts index 97c3b3963..6bbf15b30 100644 --- a/lib/routes/f95zone/thread.ts +++ b/lib/routes/f95zone/thread.ts @@ -1,4 +1,4 @@ -import { load } from 'cheerio'; +import { type CheerioAPI, load } from 'cheerio'; import { config } from '@/config'; import type { DataItem, Route } from '@/types'; @@ -61,7 +61,7 @@ Note: If you want to track a specific post's content changes (e.g., first post w const lastPageLink = $firstPage('ul.pageNav-main li.pageNav-page:last-child a').attr('href'); const totalPages = lastPageLink ? Number(lastPageLink.match(/page-(\d+)/)?.[1] || '1') : 1; - const extractPosts = ($: ReturnType): DataItem[] => + const extractPosts = ($: CheerioAPI): DataItem[] => $('article.message') .toArray() .flatMap((article) => { diff --git a/lib/routes/gigazine/en.ts b/lib/routes/gigazine/en.ts index 276ed27a8..baa148eeb 100644 --- a/lib/routes/gigazine/en.ts +++ b/lib/routes/gigazine/en.ts @@ -1,4 +1,4 @@ -import { load } from 'cheerio'; +import { type CheerioAPI, load } from 'cheerio'; import pMap from 'p-map'; import type { DataItem, Route } from '@/types'; @@ -19,12 +19,12 @@ const getRequestOptions = (referer: string) => ({ }, }); const getAbsoluteUrl = (path: string | undefined) => (path ? new URL(path, ROOT_URL).href : undefined); -const getArticleAuthor = ($: ReturnType) => +const getArticleAuthor = ($: CheerioAPI) => $('#article .items p') .text() .match(/Posted by\s+(\S.*)$/)?.[1] ?.trim(); -const getArticleCategories = ($: ReturnType) => [ +const getArticleCategories = ($: CheerioAPI) => [ ...new Set( $('#article .items p a[href*="/gsc_news/en/C"]') .toArray() diff --git a/lib/routes/whu/rsgis.ts b/lib/routes/whu/rsgis.ts index 283a76611..c9ce6eb71 100644 --- a/lib/routes/whu/rsgis.ts +++ b/lib/routes/whu/rsgis.ts @@ -1,5 +1,6 @@ -import type { AnyNode, Cheerio } from 'cheerio'; +import type { Cheerio } from 'cheerio'; import { load } from 'cheerio'; +import type { Element } from 'domhandler'; import type { Context } from 'hono'; import type { DataItem, Route } from '@/types'; @@ -103,7 +104,7 @@ function checkExternal(link: string): boolean { * @param element * @returns A list of RSS meta node. */ -function parseListLinkDateItem(element: Cheerio, currentUrl: string) { +function parseListLinkDateItem(element: Cheerio, currentUrl: string) { const linkElement = element.find('a').first(); const title = linkElement.text(); const href = linkElement.attr('href');