From 81d69adfb02e99013d337aa8d2dda397cae01679 Mon Sep 17 00:00:00 2001 From: undefined Date: Sun, 16 Feb 2025 00:16:43 +0800 Subject: [PATCH] eslint-config: add de-morgan --- framework/eslint-config/base.yaml | 2 ++ framework/eslint-config/package.json | 7 ++++--- packages/hydrojudge/src/sandbox.ts | 2 +- packages/hydrooj/src/handler/discussion.ts | 6 ++---- packages/hydrooj/src/handler/record.ts | 2 +- packages/hydrooj/src/model/discussion.ts | 8 ++++---- packages/hydrooj/src/model/problem.ts | 2 +- packages/hydrooj/src/service/server.ts | 3 +-- packages/hydrooj/src/service/watcher.ts | 4 ++-- packages/vjudge/src/providers/codeforces.ts | 2 +- 10 files changed, 19 insertions(+), 19 deletions(-) diff --git a/framework/eslint-config/base.yaml b/framework/eslint-config/base.yaml index 44f08de5c..68840c5d1 100644 --- a/framework/eslint-config/base.yaml +++ b/framework/eslint-config/base.yaml @@ -1,6 +1,7 @@ extends: - airbnb-base - ./typescript + - plugin:de-morgan/recommended-legacy env: es6: true globals: @@ -14,6 +15,7 @@ plugins: - eslint-plugin-import - eslint-plugin-github - '@stylistic' + - eslint-plugin-de-morgan rules: '@typescript-eslint/no-shadow': 1 diff --git a/framework/eslint-config/package.json b/framework/eslint-config/package.json index c1df4364b..d9ba7891e 100644 --- a/framework/eslint-config/package.json +++ b/framework/eslint-config/package.json @@ -4,10 +4,11 @@ "license": "MIT", "main": "react.yaml", "dependencies": { - "@stylistic/eslint-plugin": "^3.0.1", - "@typescript-eslint/eslint-plugin": "^8.23.0", - "@typescript-eslint/parser": "^8.23.0", + "@stylistic/eslint-plugin": "^3.1.0", + "@typescript-eslint/eslint-plugin": "^8.24.0", + "@typescript-eslint/parser": "^8.24.0", "eslint-config-airbnb": "^19.0.4", + "eslint-plugin-de-morgan": "^1.0.1", "eslint-plugin-github": "^5.1.8", "eslint-plugin-import": "2.31.0", "eslint-plugin-jsx-a11y": "^6.10.2", diff --git a/packages/hydrojudge/src/sandbox.ts b/packages/hydrojudge/src/sandbox.ts index c9128804b..d0f1f8d28 100644 --- a/packages/hydrojudge/src/sandbox.ts +++ b/packages/hydrojudge/src/sandbox.ts @@ -229,7 +229,7 @@ export async function versionCheck(reportWarn: (str: string) => void, reportErro const { osinfo } = await sysinfo.get(); if (sandboxCgroup === 2) { const kernelVersion = osinfo.kernel.split('-')[0]; - if (!(gte(kernelVersion, '5.19.0') && gte(sandboxVersion, '1.6.10'))) { + if (!gte(kernelVersion, '5.19.0') || !gte(sandboxVersion, '1.6.10')) { reportWarn('You are using cgroup v2 without kernel 5.19+. This could result in inaccurate memory usage measurements.'); } } diff --git a/packages/hydrooj/src/handler/discussion.ts b/packages/hydrooj/src/handler/discussion.ts index 1068e0781..1a7357846 100644 --- a/packages/hydrooj/src/handler/discussion.ts +++ b/packages/hydrooj/src/handler/discussion.ts @@ -275,8 +275,7 @@ class DiscussionDetailHandler extends DiscussionHandler { @param('drid', Types.ObjectId) async postDeleteReply(domainId: string, drid: ObjectId) { const deleteBy = this.user.own(this.drdoc) ? 'self' : this.user.own(this.ddoc) ? 'DiscussionOwner' : 'Admin'; - if (!(this.user.own(this.ddoc) - && this.user.hasPerm(PERM.PERM_DELETE_DISCUSSION_REPLY_SELF_DISCUSSION))) { + if (!this.user.own(this.ddoc) || !this.user.hasPerm(PERM.PERM_DELETE_DISCUSSION_REPLY_SELF_DISCUSSION)) { if (!this.user.own(this.drdoc)) { this.checkPerm(PERM.PERM_DELETE_DISCUSSION_REPLY); } else this.checkPerm(PERM.PERM_DELETE_DISCUSSION_REPLY_SELF); @@ -316,8 +315,7 @@ class DiscussionDetailHandler extends DiscussionHandler { @param('drrid', Types.ObjectId) async postDeleteTailReply(domainId: string, drid: ObjectId, drrid: ObjectId) { const deleteBy = this.user.own(this.drrdoc) ? 'self' : 'Admin'; - if (!(this.user.own(this.drrdoc) - && this.user.hasPerm(PERM.PERM_DELETE_DISCUSSION_REPLY_SELF))) { + if (!this.user.own(this.drrdoc) || !this.user.hasPerm(PERM.PERM_DELETE_DISCUSSION_REPLY_SELF)) { this.checkPerm(PERM.PERM_DELETE_DISCUSSION_REPLY); } const msg = JSON.stringify({ diff --git a/packages/hydrooj/src/handler/record.ts b/packages/hydrooj/src/handler/record.ts index 4b6031b2c..8123d81f6 100644 --- a/packages/hydrooj/src/handler/record.ts +++ b/packages/hydrooj/src/handler/record.ts @@ -411,7 +411,7 @@ class RecordDetailConnectionHandler extends ConnectionHandler { rdoc.compilerTexts = []; } - if (!(rdoc.contest && this.user._id === rdoc.uid)) { + if (!rdoc.contest || this.user._id !== rdoc.uid) { if (!problem.canViewBy(pdoc, this.user)) throw new PermissionError(PERM.PERM_VIEW_PROBLEM_HIDDEN); } diff --git a/packages/hydrooj/src/model/discussion.ts b/packages/hydrooj/src/model/discussion.ts index e23242e6e..03f93855e 100644 --- a/packages/hydrooj/src/model/discussion.ts +++ b/packages/hydrooj/src/model/discussion.ts @@ -275,15 +275,15 @@ export function flushNodes(domainId: string) { export async function getVnode(domainId: string, type: number, id: string, uid?: number) { if (type === document.TYPE_PROBLEM) { const pdoc = await problem.get(domainId, Number.isSafeInteger(+id) ? +id : id, problem.PROJECTION_LIST); - if (!pdoc) throw new DiscussionNodeNotFoundError(id); + if (!pdoc) throw new DiscussionNodeNotFoundError(`problem/${id}`); return { ...pdoc, type, id: pdoc.docId }; } if ([document.TYPE_CONTEST, document.TYPE_TRAINING].includes(type as any)) { const model = type === document.TYPE_TRAINING ? training : contest; - if (!ObjectId.isValid(id)) throw new DiscussionNodeNotFoundError(id); + if (!ObjectId.isValid(id)) throw new DiscussionNodeNotFoundError(`contest/${id}`); const _id = new ObjectId(id); const tdoc = await model.get(domainId, _id); - if (!tdoc) throw new DiscussionNodeNotFoundError(id); + if (!tdoc) throw new DiscussionNodeNotFoundError(`contest/${id}`); if (uid) { const tsdoc = await model.getStatus(domainId, _id, uid); tdoc.attend = tsdoc?.attend || tsdoc?.enroll; @@ -320,7 +320,7 @@ export async function getListVnodes(domainId: string, ddocs: any, getHidden = fa export function checkVNodeVisibility(type: number, vnode: any, user: User) { if (type === document.TYPE_PROBLEM) { - if (vnode.hidden && !(user.own(vnode) || user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))) return false; + if (vnode.hidden && !user.own(vnode) && !user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) return false; } if ([document.TYPE_CONTEST, document.TYPE_TRAINING].includes(type as any)) { if (!user.own(vnode) && vnode.assign?.length && !Set.intersection(vnode.assign, user.group).size) return false; diff --git a/packages/hydrooj/src/model/problem.ts b/packages/hydrooj/src/model/problem.ts index 03fc69d7e..1e0c8a72d 100644 --- a/packages/hydrooj/src/model/problem.ts +++ b/packages/hydrooj/src/model/problem.ts @@ -396,7 +396,7 @@ export class ProblemModel { // TODO enhance if (pdocs.length !== pids.length) { for (const pid of pids) { - if (!(r[pid] || l[pid])) { + if (!r[pid] && !l[pid]) { if (doThrow) throw new ProblemNotFoundError(domainId, pid); if (!indexByDocIdOnly) r[pid] = { ...ProblemModel.default, domainId, pid: pid.toString() }; } diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index dd54e2e22..f29861714 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -123,8 +123,7 @@ export class ConnectionHandler extends ConnectionHandlerOriginal { } onerror(err: HydroError) { - if (!(err instanceof NotFoundError) - && !((err instanceof PrivilegeError || err instanceof PermissionError) && this.user?._id === 0)) { + if (![NotFoundError, PrivilegeError, PermissionError].some((i) => err instanceof i) || this.user?._id !== 0) { logger.error(`Path:${this.request.path}, User:${this.user?._id}(${this.user?.uname})`); logger.error(err); } diff --git a/packages/hydrooj/src/service/watcher.ts b/packages/hydrooj/src/service/watcher.ts index aac9fefcd..6b8dd2e07 100644 --- a/packages/hydrooj/src/service/watcher.ts +++ b/packages/hydrooj/src/service/watcher.ts @@ -154,11 +154,11 @@ export default class Watcher extends Service { for (const filename in require.cache) { const module = require.cache[filename]; const plugin = unwrapExports(module.exports); - if (!(typeof plugin === 'object' && plugin && 'apply' in plugin)) continue; + if (typeof plugin !== 'object' || !plugin || !('apply' in plugin)) continue; const runtime = this.ctx.registry.get(plugin); if (!runtime || this.declined.has(filename)) continue; pending.set(filename, runtime); - if (!(plugin && 'sideEffect' in plugin && plugin['sideEffect'])) this.declined.add(filename); + if (!plugin || !('sideEffect' in plugin) || !plugin['sideEffect']) this.declined.add(filename); } for (const [filename, runtime] of pending) { diff --git a/packages/vjudge/src/providers/codeforces.ts b/packages/vjudge/src/providers/codeforces.ts index 0e48785c8..4d8034a52 100644 --- a/packages/vjudge/src/providers/codeforces.ts +++ b/packages/vjudge/src/providers/codeforces.ts @@ -23,7 +23,7 @@ function parseProblemId(id: string) { export function getDifficulty(tags: string[]) { const d = tags.find((i) => /^\*\d+$/.test(i))?.split('*')[1]; - if (!(d && +d)) return null; + if (!Number.isSafeInteger(+d)) return null; const map = [ [500, 1], [800, 2],