From 2070352ca0698a34cc15c7e0c6417e8cf488b32c Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Thu, 4 Mar 2021 13:15:01 +0100 Subject: [PATCH] Lint scripts --- src/example.ts | 1 + src/index.ts | 4 +-- src/scripts/classes/mapping/platform-user.ts | 34 +++++++++---------- src/scripts/classes/mapping/post.ts | 18 +++++----- src/scripts/classes/mapping/thread.ts | 22 ++++++------ src/scripts/classes/mapping/user-profile.ts | 10 +++--- .../classes/query/handiwork-search-query.ts | 2 +- .../classes/query/latest-search-query.ts | 7 +++- src/scripts/classes/session.ts | 12 +++---- src/scripts/network-helper.ts | 7 ++-- src/scripts/scrape-data/post-parse.ts | 10 ++---- src/scripts/search.ts | 7 ++-- src/scripts/shared.ts | 5 ++- 13 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/example.ts b/src/example.ts index 62d9a8c..3831d2b 100644 --- a/src/example.ts +++ b/src/example.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ /* to use this example, create an .env file in the project root with the following values: diff --git a/src/index.ts b/src/index.ts index 3373fde..3daf550 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,8 +45,8 @@ export { default as ThreadSearchQuery } from "./scripts/classes/query/thread-sea /** * Set the logger level for module debugging. */ -/* istambul ignore next */ -export var loggerLevel = shared.logger.level; +// eslint-disable-next-line prefer-const +export let loggerLevel = shared.logger.level; shared.logger.level = "warn"; // By default log only the warn messages /** diff --git a/src/scripts/classes/mapping/platform-user.ts b/src/scripts/classes/mapping/platform-user.ts index 6a5c8a5..c8d2caa 100644 --- a/src/scripts/classes/mapping/platform-user.ts +++ b/src/scripts/classes/mapping/platform-user.ts @@ -38,91 +38,91 @@ export default class PlatformUser { /** * Unique user ID. */ - public get id() { + public get id(): number { return this._id; } /** * Username. */ - public get name() { + public get name(): string { return this._name; } /** * Title assigned to the user by the platform. */ - public get title() { + public get title(): string { return this._title; } /** * List of banners assigned by the platform. */ - public get banners() { + public get banners(): string[] { return this._banners; } /** * Number of messages written by the user. */ - public get messages() { + public get messages(): number { return this._messages; } /** * @todo Reaction score. */ - public get reactionScore() { + public get reactionScore(): number { return this._reactionScore; } /** * @todo Points. */ - public get points() { + public get points(): number { return this._points; } /** * Number of ratings received. */ - public get ratingsReceived() { + public get ratingsReceived(): number { return this._ratingsReceived; } /** * Date of joining the platform. */ - public get joined() { + public get joined(): Date { return this._joined; } /** * Date of the last connection to the platform. */ - public get lastSeen() { + public get lastSeen(): Date { return this._lastSeen; } /** * Indicates whether the user is followed by the currently logged in user. */ - public get followed() { + public get followed(): boolean { return this._followed; } /** * Indicates whether the user is ignored by the currently logged on user. */ - public get ignored() { + public get ignored(): boolean { return this._ignored; } /** * Indicates that the profile is private and not viewable by the user. */ - public get private() { + public get private(): boolean { return this._private; } /** * URL of the image used as the user's avatar. */ - public get avatar() { + public get avatar(): string { return this._avatar; } /** * Value of donations made. */ - public get donation() { + public get donation(): number { return this._amountDonated; } @@ -134,11 +134,11 @@ export default class PlatformUser { //#region Public methods - public setID(id: number) { + public setID(id: number): void { this._id = id; } - public async fetch() { + public async fetch(): Promise { // Check ID if (!this.id && this.id < 1) throw new Error("Invalid user ID"); diff --git a/src/scripts/classes/mapping/post.ts b/src/scripts/classes/mapping/post.ts index ae64bd4..ffc2151 100644 --- a/src/scripts/classes/mapping/post.ts +++ b/src/scripts/classes/mapping/post.ts @@ -35,49 +35,49 @@ export default class Post { /** * Represents a post published by a user on the F95Zone platform. */ - public get id() { + public get id(): number { return this._id; } /** * Unique ID of the post within the thread in which it is present. */ - public get number() { + public get number(): number { return this._number; } /** * Date the post was first published. */ - public get published() { + public get published(): Date { return this._published; } /** * Date the post was last modified. */ - public get lastEdit() { + public get lastEdit(): Date { return this._lastEdit; } /** * User who owns the post. */ - public get owner() { + public get owner(): PlatformUser { return this._owner; } /** * Indicates whether the post has been bookmarked. */ - public get bookmarked() { + public get bookmarked(): boolean { return this._bookmarked; } /** * Post message text. */ - public get message() { + public get message(): string { return this._message; } /** * Set of the elements that make up the body of the post. */ - public get body() { + public get body(): IPostElement[] { return this._body; } @@ -92,7 +92,7 @@ export default class Post { /** * Gets the post data starting from its unique ID for the entire platform. */ - public async fetch() { + public async fetch(): Promise { // Fetch HTML page containing the post const url = new URL(this.id.toString(), urls.F95_POSTS).toString(); const htmlResponse = await fetchHTML(url); diff --git a/src/scripts/classes/mapping/thread.ts b/src/scripts/classes/mapping/thread.ts index aafa378..5b41797 100644 --- a/src/scripts/classes/mapping/thread.ts +++ b/src/scripts/classes/mapping/thread.ts @@ -45,7 +45,7 @@ export default class Thread { /** * Unique ID of the thread on the platform. */ - public get id() { + public get id(): number { return this._id; } /** @@ -53,55 +53,55 @@ export default class Thread { * * It may vary depending on any versions of the contained product. */ - public get url() { + public get url(): string { return this._url; } /** * Thread title. */ - public get title() { + public get title(): string { return this._title; } /** * Tags associated with the thread. */ - public get tags() { + public get tags(): string[] { return this._tags; } /** * Prefixes associated with the thread */ - public get prefixes() { + public get prefixes(): string[] { return this._prefixes; } /** * Rating assigned to the thread. */ - public get rating() { + public get rating(): TRating { return this._rating; } /** * Owner of the thread. */ - public get owner() { + public get owner(): PlatformUser { return this._owner; } /** * Date the thread was first published. */ - public get publication() { + public get publication(): Date { return this._publication; } /** * Date the thread was last modified. */ - public get modified() { + public get modified(): Date { return this._modified; } /** * Category to which the content of the thread belongs. */ - public get category() { + public get category(): TCategory { return this._category; } @@ -233,7 +233,7 @@ export default class Thread { /** * Gets information about this thread. */ - public async fetch() { + public async fetch(): Promise { // Prepare the url this._url = new URL(this.id.toString(), urls.F95_THREADS).toString(); diff --git a/src/scripts/classes/mapping/user-profile.ts b/src/scripts/classes/mapping/user-profile.ts index ee33f96..946ac7f 100644 --- a/src/scripts/classes/mapping/user-profile.ts +++ b/src/scripts/classes/mapping/user-profile.ts @@ -52,28 +52,28 @@ export default class UserProfile extends PlatformUser { /** * List of followed thread data. */ - public get watched() { + public get watched(): IWatchedThread[] { return this._watched; } /** * List of bookmarked posts. * @todo */ - public get bookmarks() { + public get bookmarks(): Post[] { return this._bookmarks; } /** * List of alerts. * @todo */ - public get alerts() { + public get alerts(): string[] { return this._alerts; } /** * List of conversations. * @todo */ - public get conversation() { + public get conversation(): string[] { return this._conversations; } @@ -85,7 +85,7 @@ export default class UserProfile extends PlatformUser { //#region Public methods - public async fetch() { + public async fetch(): Promise { // First get the user ID and set it const id = await this.fetchUserID(); super.setID(id); diff --git a/src/scripts/classes/query/handiwork-search-query.ts b/src/scripts/classes/query/handiwork-search-query.ts index e38b22b..6f056f2 100644 --- a/src/scripts/classes/query/handiwork-search-query.ts +++ b/src/scripts/classes/query/handiwork-search-query.ts @@ -6,7 +6,7 @@ import validator from "class-validator"; // Module from files import { IQuery, TCategory, TQueryInterface } from "../../interfaces.js"; -import { GenericAxiosError, UnexpectedResponseContentType } from "../errors.js"; +import { GenericAxiosError } from "../errors.js"; import { Result } from "../result.js"; import LatestSearchQuery, { TLatestOrder } from "./latest-search-query.js"; import ThreadSearchQuery, { TThreadOrder } from "./thread-search-query.js"; diff --git a/src/scripts/classes/query/latest-search-query.ts b/src/scripts/classes/query/latest-search-query.ts index f0848ff..7e5f8d6 100644 --- a/src/scripts/classes/query/latest-search-query.ts +++ b/src/scripts/classes/query/latest-search-query.ts @@ -8,6 +8,9 @@ import { urls } from "../../constants/url.js"; import PrefixParser from "../prefix-parser.js"; import { IQuery, TCategory, TQueryInterface } from "../../interfaces.js"; import { fetchGETResponse } from "../../network-helper.js"; +import { AxiosResponse } from "axios"; +import { GenericAxiosError } from "../errors.js"; +import { Result } from "../result.js"; // Type definitions export type TLatestOrder = "date" | "likes" | "views" | "title" | "rating"; @@ -64,7 +67,9 @@ export default class LatestSearchQuery implements IQuery { return validator.validateSync(this).length === 0; } - public async execute() { + public async execute(): Promise< + Result> + > { // Check if the query is valid if (!this.validate()) { throw new Error( diff --git a/src/scripts/classes/session.ts b/src/scripts/classes/session.ts index ff5fb02..2686786 100644 --- a/src/scripts/classes/session.ts +++ b/src/scripts/classes/session.ts @@ -37,37 +37,37 @@ export default class Session { /** * Path of the session map file on disk. */ - public get path() { + public get path(): string { return this._path; } /** * Indicates if the session is mapped on disk. */ - public get isMapped() { + public get isMapped(): boolean { return this._isMapped; } /** * Date of creation of the session. */ - public get created() { + public get created(): Date { return this._created; } /** * MD5 hash of the username and the password. */ - public get hash() { + public get hash(): string { return this._hash; } /** * Token used to login to F95Zone. */ - public get token() { + public get token(): string { return this._token; } /** * Cookie holder. */ - public get cookieJar() { + public get cookieJar(): tough.CookieJar { return this._cookieJar; } diff --git a/src/scripts/network-helper.ts b/src/scripts/network-helper.ts index a7cd517..ce67d59 100644 --- a/src/scripts/network-helper.ts +++ b/src/scripts/network-helper.ts @@ -84,7 +84,7 @@ export async function fetchHTML( */ export async function authenticate( credentials: credentials, - force = false + force: boolean = false ): Promise { shared.logger.info(`Authenticating with user ${credentials.username}`); if (!credentials.token) @@ -140,7 +140,7 @@ export async function authenticate( /** * Obtain the token used to authenticate the user to the platform. */ -export async function getF95Token() { +export async function getF95Token(): Promise { // Fetch the response of the platform const response = await fetchGETResponse(f95url.F95_LOGIN_URL); @@ -167,7 +167,6 @@ export async function fetchGETResponse( const response = await axios.get(secureURL, commonConfig); return success(response); } catch (e) { - console.log(e.response); shared.logger.error( `(GET) Error ${e.message} occurred while trying to fetch ${secureURL}` ); @@ -217,7 +216,7 @@ export function isStringAValidURL(url: string): boolean { */ export async function urlExists( url: string, - checkRedirect = false + checkRedirect: boolean = false ): Promise { // Local variables let valid = false; diff --git a/src/scripts/scrape-data/post-parse.ts b/src/scripts/scrape-data/post-parse.ts index b54c58b..5ba2cab 100644 --- a/src/scripts/scrape-data/post-parse.ts +++ b/src/scripts/scrape-data/post-parse.ts @@ -90,13 +90,7 @@ function parseCheerioSpoilerNode( if (element.attr("class") === "bbCodeSpoiler") { const spoiler = parseCheerioSpoilerNode($, element); content.content.push(spoiler); - } - //@ts-ignore - // else if (el.name === "br") { - // // Add new line - // content.text += "\n"; - // } - else if (el.type === "text") { + } else if (el.type === "text") { // Append text content.text += element.text(); } @@ -266,7 +260,7 @@ function parseCheerioNode( function parsePostElements(elements: IPostElement[]): IPostElement[] { // Local variables const pairs: IPostElement[] = []; - const specialCharsRegex = /^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/; + const specialCharsRegex = /^[-!$%^&*()_+|~=`{}[\]:";'<>?,./]/; const specialRegex = new RegExp(specialCharsRegex); for (let i = 0; i < elements.length; i++) { diff --git a/src/scripts/search.ts b/src/scripts/search.ts index a4102fd..cae91e6 100644 --- a/src/scripts/search.ts +++ b/src/scripts/search.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-inferrable-types */ "use strict"; // Modules from file @@ -13,15 +14,13 @@ import getURLsFromQuery from "./fetch-data/fetch-query.js"; */ export default async function search( query: IQuery, - limit = 30 + limit: number = 30 ): Promise { // Fetch the URLs const urls: string[] = await getURLsFromQuery(query, limit); // Fetch the data - const results = urls.map((url, idx) => { - return getHandiworkInformation(url); - }); + const results = urls.map((url) => getHandiworkInformation(url)); return Promise.all(results); } diff --git a/src/scripts/shared.ts b/src/scripts/shared.ts index be9ce9a..2134d5e 100644 --- a/src/scripts/shared.ts +++ b/src/scripts/shared.ts @@ -14,6 +14,7 @@ import Session from "./classes/session.js"; // Types declaration export type TPrefixDict = { [n: number]: string }; type TPrefixKey = "engines" | "statuses" | "tags" | "others"; +type TPrefixes = { [key in TPrefixKey]: TPrefixDict }; /** * Class containing variables shared between modules. @@ -22,9 +23,7 @@ export default abstract class Shared { //#region Fields private static _isLogged = false; - private static _prefixes: { [key in TPrefixKey]: TPrefixDict } = {} as { - [key in TPrefixKey]: TPrefixDict; - }; + private static _prefixes: TPrefixes = {} as TPrefixes; private static _logger: log4js.Logger = log4js.getLogger(); private static _session = new Session(join(tmpdir(), "f95session.json"));