Add ESLint (#941)

* Add eslint, switch to lint-staged for precommit

* Fix all ESLint errors

* Update GH workflow to check prettier and eslint
This commit is contained in:
Madeline
2023-01-20 18:10:47 +11:00
committed by GitHub
parent 0c815fde91
commit 084dc0be08
157 changed files with 1905 additions and 668 deletions
+3 -11
View File
@@ -16,16 +16,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToOne,
RelationId,
} from "typeorm";
import { Column, Entity, JoinColumn, ManyToOne, OneToOne } from "typeorm";
import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { Team } from "./Team";
import { User } from "./User";
@@ -44,7 +36,7 @@ export class Application extends BaseClass {
summary: string = "";
@Column({ type: "simple-json", nullable: true })
type?: any;
type?: object; // TODO: this type is bad
@Column()
hook: boolean = true;
@@ -176,6 +168,6 @@ export interface ApplicationCommandInteractionData {
export interface ApplicationCommandInteractionDataOption {
name: string;
value?: any;
value?: unknown;
options?: ApplicationCommandInteractionDataOption[];
}
+2 -2
View File
@@ -173,8 +173,8 @@ export interface AuditLogChangeValue {
explicit_content_filter?: number;
default_message_notifications?: number;
vanity_url_code?: string;
$add?: {}[];
$remove?: {}[];
$add?: object[]; // TODO: These types are bad.
$remove?: object[];
prune_delete_days?: number;
widget_enabled?: boolean;
widget_channel_id?: string;
+2 -2
View File
@@ -16,7 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
import { Column, Entity, JoinColumn, ManyToOne } from "typeorm";
import { BaseClass } from "./BaseClass";
import { User } from "./User";
import crypto from "crypto";
@@ -38,7 +38,7 @@ export class BackupCode extends BaseClass {
}
export function generateMfaBackupCodes(user_id: string) {
let backup_codes: BackupCode[] = [];
const backup_codes: BackupCode[] = [];
for (let i = 0; i < 10; i++) {
const code = BackupCode.create({
user: { id: user_id },
+7 -2
View File
@@ -29,7 +29,7 @@ import { getDatabase } from "../util/Database";
import { OrmUtils } from "../imports/OrmUtils";
export class BaseClassWithoutId extends BaseEntity {
private get construct(): any {
private get construct() {
return this.constructor;
}
@@ -37,19 +37,24 @@ export class BaseClassWithoutId extends BaseEntity {
return getDatabase()?.getMetadata(this.construct);
}
assign(props: any) {
assign(props: object) {
OrmUtils.mergeDeep(this, props);
return this;
}
// TODO: fix eslint
// eslint-disable-next-line @typescript-eslint/no-explicit-any
toJSON(): any {
return Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/no-non-null-assertion
this.metadata!.columns // @ts-ignore
.map((x) => [x.propertyName, this[x.propertyName]])
.concat(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.metadata.relations.map((x) => [
x.propertyName,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this[x.propertyName],
]),
+7 -7
View File
@@ -35,7 +35,6 @@ import {
Snowflake,
trimSpecial,
InvisibleCharacters,
ChannelTypes,
} from "../util";
import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces";
import { Recipient } from "./Recipient";
@@ -219,7 +218,7 @@ export class Channel extends BaseClass {
!guild.features.includes("ALLOW_INVALID_CHANNEL_NAMES") &&
channel.name
) {
for (var character of InvisibleCharacters)
for (const character of InvisibleCharacters)
if (channel.name.includes(character))
throw new HTTPError(
"Channel name cannot include invalid characters",
@@ -237,7 +236,7 @@ export class Channel extends BaseClass {
403,
);
if (channel.name.match(/\-\-+/g))
if (channel.name.match(/--+/g))
throw new HTTPError(
"Channel name cannot include multiple adjacent dashes.",
403,
@@ -344,8 +343,9 @@ export class Channel extends BaseClass {
relations: ["channel", "channel.recipients"],
});
for (let ur of userRecipients) {
let re = ur.channel.recipients!.map((r) => r.user_id);
for (const ur of userRecipients) {
if (!ur.channel.recipients) continue;
const re = ur.channel.recipients.map((r) => r.user_id);
if (re.length === channelRecipients.length) {
if (containsAll(re, channelRecipients)) {
if (channel == null) {
@@ -380,8 +380,8 @@ export class Channel extends BaseClass {
const channel_dto = await DmChannelDTO.from(channel);
if (type === ChannelType.GROUP_DM) {
for (let recipient of channel.recipients!) {
if (type === ChannelType.GROUP_DM && channel.recipients) {
for (const recipient of channel.recipients) {
await emitEvent({
event: "CHANNEL_CREATE",
data: channel_dto.excludedRecipients([recipient.user_id]),
+4 -2
View File
@@ -20,8 +20,10 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
import { BaseClass } from "./BaseClass";
import { User } from "./User";
export interface PublicConnectedAccount
extends Pick<ConnectedAccount, "name" | "type" | "verified"> {}
export type PublicConnectedAccount = Pick<
ConnectedAccount,
"name" | "type" | "verified"
>;
@Entity("connected_accounts")
export class ConnectedAccount extends BaseClass {
-1
View File
@@ -20,7 +20,6 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
import { User } from ".";
import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { Role } from "./Role";
@Entity("emojis")
export class Emoji extends BaseClass {
+1 -25
View File
@@ -16,32 +16,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
RelationId,
} from "typeorm";
import { Column, Entity } from "typeorm";
import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { PublicUserProjection, User } from "./User";
import { HTTPError } from "lambert-server";
import {
containsAll,
emitEvent,
getPermission,
Snowflake,
trimSpecial,
InvisibleCharacters,
} from "../util";
import { BitField, BitFieldResolvable, BitFlag } from "../util/BitField";
import { Recipient } from "./Recipient";
import { Message } from "./Message";
import { ReadState } from "./ReadState";
import { Invite } from "./Invite";
import { DmChannelDTO } from "../dtos";
@Entity("security_settings")
export class SecuritySettings extends BaseClass {
+3 -5
View File
@@ -20,10 +20,8 @@ import {
Column,
Entity,
JoinColumn,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
RelationId,
} from "typeorm";
import { Config, handleFile, Snowflake } from "..";
@@ -370,12 +368,12 @@ export class Guild extends BaseClass {
}
});
for (const channel of body.channels?.sort((a, b) =>
for (const channel of body.channels.sort((a) =>
a.parent_id ? 1 : -1,
)) {
var id = ids.get(channel.id) || Snowflake.generate();
const id = ids.get(channel.id) || Snowflake.generate();
var parent_id = ids.get(channel.parent_id);
const parent_id = ids.get(channel.parent_id);
await Channel.createChannel(
{ ...channel, guild_id, id, parent_id },
+16 -9
View File
@@ -33,7 +33,7 @@ import {
RelationId,
} from "typeorm";
import { Guild } from "./Guild";
import { Config, emitEvent, FieldErrors } from "../util";
import { Config, emitEvent } from "../util";
import {
GuildCreateEvent,
GuildDeleteEvent,
@@ -212,12 +212,16 @@ export class Member extends BaseClassWithoutId {
}
static async addRole(user_id: string, guild_id: string, role_id: string) {
const [member, role] = await Promise.all([
const [member] = await Promise.all([
Member.findOneOrFail({
where: { id: user_id, guild_id },
relations: ["user", "roles"], // we don't want to load the role objects just the ids
//@ts-ignore
select: ["index", "roles.id"], // TODO fix type
select: {
index: true,
roles: {
id: true,
},
},
}),
Role.findOneOrFail({
where: { id: role_id, guild_id },
@@ -249,8 +253,12 @@ export class Member extends BaseClassWithoutId {
Member.findOneOrFail({
where: { id: user_id, guild_id },
relations: ["user", "roles"], // we don't want to load the role objects just the ids
//@ts-ignore
select: ["roles.id", "index"], // TODO: fix type
select: {
index: true,
roles: {
id: true,
},
},
}),
await Role.findOneOrFail({ where: { id: role_id, guild_id } }),
]);
@@ -327,7 +335,7 @@ export class Member extends BaseClassWithoutId {
guild_id,
user: {
sessions: {
status: Not("invisible" as "invisible"), // lol typescript?
status: Not("invisible" as const), // lol typescript?
},
},
},
@@ -506,8 +514,7 @@ export const PublicMemberProjection: PublicMemberKeys[] = [
"premium_since",
];
// @ts-ignore
export type PublicMember = Pick<Member, Omit<PublicMemberKeys, "roles">> & {
export type PublicMember = Omit<Pick<Member, PublicMemberKeys>, "roles"> & {
user: PublicUser;
roles: string[]; // only role ids not objects
};
-1
View File
@@ -26,7 +26,6 @@ import {
} from "typeorm";
import { BaseClass } from "./BaseClass";
import { Channel } from "./Channel";
import { Message } from "./Message";
import { User } from "./User";
// for read receipts
-1
View File
@@ -22,7 +22,6 @@ import {
JoinColumn,
ManyToOne,
OneToMany,
OneToOne,
RelationId,
} from "typeorm";
import { Sticker } from ".";
-1
View File
@@ -20,7 +20,6 @@ import {
Column,
Entity,
JoinColumn,
ManyToMany,
ManyToOne,
OneToMany,
RelationId,
+11 -8
View File
@@ -17,8 +17,6 @@
*/
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
FindOneOptions,
@@ -34,6 +32,7 @@ import { Member } from "./Member";
import { UserSettings } from "./UserSettings";
import { Session } from "./Session";
import { Config, FieldErrors, Snowflake, trimSpecial, adjustEmail } from "..";
import { Request } from "express";
export enum PublicUserEnum {
username,
@@ -80,7 +79,7 @@ export const PrivateUserProjection = [
// Private user data that should never get sent to the client
export type PublicUser = Pick<User, PublicUserKeys>;
export interface UserPublic extends Pick<User, PublicUserKeys> {}
export type UserPublic = Pick<User, PublicUserKeys>;
export interface UserPrivate extends Pick<User, PrivateUserKeys> {
locale: string;
@@ -266,6 +265,7 @@ export class User extends BaseClass {
}
toPublicUser() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = {};
PublicUserProjection.forEach((x) => {
user[x] = this[x];
@@ -277,6 +277,7 @@ export class User extends BaseClass {
return await User.findOneOrFail({
where: { id: user_id },
...opts,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
select: [...PublicUserProjection, ...(opts?.select || [])], // TODO: fix
});
@@ -328,7 +329,6 @@ export class User extends BaseClass {
email,
username,
password,
date_of_birth,
id,
req,
}: {
@@ -337,7 +337,7 @@ export class User extends BaseClass {
email?: string;
date_of_birth?: Date; // "2000-04-03"
id?: string;
req?: any;
req?: Request;
}) {
// trim special uf8 control characters -> Backspace, Newline, ...
username = trimSpecial(username);
@@ -348,7 +348,8 @@ export class User extends BaseClass {
throw FieldErrors({
username: {
code: "USERNAME_TOO_MANY_USERS",
message: req.t("auth:register.USERNAME_TOO_MANY_USERS"),
message:
req?.t("auth:register.USERNAME_TOO_MANY_USERS") || "",
},
});
}
@@ -357,7 +358,7 @@ export class User extends BaseClass {
// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed
// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
const language =
req.language === "en" ? "en-US" : req.language || "en-US";
req?.language === "en" ? "en-US" : req?.language || "en-US";
const settings = UserSettings.create({
locale: language,
@@ -386,7 +387,9 @@ export class User extends BaseClass {
setImmediate(async () => {
if (Config.get().guild.autoJoin.enabled) {
for (const guild of Config.get().guild.autoJoin.guilds || []) {
await Member.addToGuild(user.id, guild).catch((e) => {});
await Member.addToGuild(user.id, guild).catch((e) =>
console.error("[Autojoin]", e),
);
}
}
});