CODEBASE: Fix lint errors 3 (#1758)

This is a really big refactor because it actually *fixes* a lot of the lint errors instead of disabling them.
This commit is contained in:
catloversg
2024-11-14 23:18:57 +07:00
committed by GitHub
parent 97ca8c5f5e
commit 75cf9c88b5
31 changed files with 187 additions and 51 deletions
+13 -1
View File
@@ -13,6 +13,7 @@ import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { IPAddress, isIPAddress } from "../Types/strings";
import "../Script/RunningScript"; // For reviver side-effect
import { objectAssert } from "../utils/helpers/typeAssertion";
/**
* Map of all Servers that exist in the game
@@ -63,6 +64,7 @@ export function DeleteServer(serverkey: string): void {
for (const key of Object.keys(AllServers)) {
const server = AllServers[key];
if (server.ip !== serverkey && server.hostname !== serverkey) continue;
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete AllServers[key];
break;
}
@@ -100,6 +102,7 @@ export function AddToAllServers(server: Server | HacknetServer): void {
export const renameServer = (hostname: string, newName: string): void => {
AllServers[newName] = AllServers[hostname];
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete AllServers[hostname];
};
@@ -188,13 +191,22 @@ export function initForeignServers(homeComputer: Server): void {
export function prestigeAllServers(): void {
for (const member of Object.keys(AllServers)) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete AllServers[member];
}
AllServers = {};
}
export function loadAllServers(saveString: string): void {
AllServers = JSON.parse(saveString, Reviver);
const allServersData: unknown = JSON.parse(saveString, Reviver);
objectAssert(allServersData);
for (const [serverName, server] of Object.entries(allServersData)) {
if (!(server instanceof Server) && !(server instanceof HacknetServer)) {
throw new Error(`Server ${serverName} is not an instance of Server or HacknetServer.`);
}
}
// We validated the data above, so it's safe to typecast here.
AllServers = allServersData as typeof AllServers;
}
export function saveAllServers(): string {
+7 -2
View File
@@ -24,6 +24,7 @@ import lodash from "lodash";
import { Settings } from "../Settings/Settings";
import type { ScriptKey } from "../utils/helpers/scriptKey";
import { objectAssert } from "../utils/helpers/typeAssertion";
interface IConstructorParams {
adminRights?: boolean;
@@ -293,6 +294,7 @@ export abstract class BaseServer implements IServer {
// RunningScripts are stored as a simple array, both for backward compatibility,
// compactness, and ease of filtering them here.
const result = Generic_toJSON(ctorName, this, keys);
objectAssert(result.data);
if (Settings.ExcludeRunningScriptsFromSave) {
result.data.runningScripts = [];
return result;
@@ -313,8 +315,11 @@ export abstract class BaseServer implements IServer {
// Initializes a Server Object from a JSON save state
// Called by subclasses, not Reviver.
static fromJSONBase<T extends BaseServer>(value: IReviverValue, ctor: new () => T, keys: readonly (keyof T)[]): T {
objectAssert(value.data);
const server = Generic_fromJSON(ctor, value.data, keys);
server.savedScripts = value.data.runningScripts;
if (value.data.runningScripts != null && Array.isArray(value.data.runningScripts)) {
server.savedScripts = value.data.runningScripts;
}
// If textFiles is not an array, we've already done the 2.3 migration to textFiles and scripts as maps + path changes.
if (!Array.isArray(server.textFiles)) return server;
@@ -333,7 +338,7 @@ export abstract class BaseServer implements IServer {
// In case somehow there are previously valid filenames that can't be sanitized, they will go in a new directory with a note.
for (const script of oldScripts) {
// We're about to do type validation on the filename anyway.
if (script.filename.endsWith(".ns")) script.filename = (script.filename + ".js") as any;
if (script.filename.endsWith(".ns")) script.filename = (script.filename + ".js") as ScriptFilePath;
let newFilePath = resolveScriptFilePath(script.filename);
if (!newFilePath) {
newFilePath = `${newDirectory}script${++invalidScriptCount}.js` as ScriptFilePath;