NETSCRIPT: Revert FormattedCode type (#500)

This commit is contained in:
David Walker 2023-05-03 04:13:35 -07:00 committed by GitHub
parent 6e028a0744
commit ad5a1c4bac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 11 deletions

@ -6,13 +6,11 @@ import { roundToTwo } from "../utils/helpers/roundToTwo";
import { RamCostConstants } from "../Netscript/RamCostGenerator"; import { RamCostConstants } from "../Netscript/RamCostGenerator";
import { ScriptFilePath } from "../Paths/ScriptFilePath"; import { ScriptFilePath } from "../Paths/ScriptFilePath";
import { ContentFile } from "../Paths/ContentFile"; import { ContentFile } from "../Paths/ContentFile";
/** Type for ensuring script code is always trimmed */
export type FormattedCode = string & { __type: "FormattedCode" };
/** A script file as a file on a server. /** A script file as a file on a server.
* For the execution of a script, see RunningScript and WorkerScript */ * For the execution of a script, see RunningScript and WorkerScript */
export class Script implements ContentFile { export class Script implements ContentFile {
code: FormattedCode; code: string;
filename: ScriptFilePath; filename: ScriptFilePath;
server: string; server: string;
@ -34,8 +32,7 @@ export class Script implements ContentFile {
get content() { get content() {
return this.code; return this.code;
} }
set content(code: string) { set content(newCode: string) {
const newCode = Script.formatCode(code);
if (this.code === newCode) return; if (this.code === newCode) return;
this.code = newCode; this.code = newCode;
this.invalidateModule(); this.invalidateModule();
@ -43,7 +40,7 @@ export class Script implements ContentFile {
constructor(fn = "default.js" as ScriptFilePath, code = "", server = "") { constructor(fn = "default.js" as ScriptFilePath, code = "", server = "") {
this.filename = fn; this.filename = fn;
this.code = Script.formatCode(code); this.code = code;
this.server = server; // hostname of server this script is on this.server = server; // hostname of server this script is on
} }
@ -138,8 +135,8 @@ export class Script implements ContentFile {
* @param {string} code - The code to format * @param {string} code - The code to format
* @returns The formatted code * @returns The formatted code
*/ */
static formatCode(code: string): FormattedCode { static formatCode(code: string): string {
return code.replace(/^\s+|\s+$/g, "") as FormattedCode; return code.replace(/^\s+|\s+$/g, "");
} }
} }

@ -252,7 +252,7 @@ export abstract class BaseServer implements IServer {
// Check if the script already exists, and overwrite it if it does // Check if the script already exists, and overwrite it if it does
const script = this.scripts.get(filename); const script = this.scripts.get(filename);
if (script) { if (script) {
// content setter handles module invalidation and code formatting // content setter handles module invalidation
script.content = code; script.content = code;
return { overwritten: true }; return { overwritten: true };
} }

@ -1,7 +1,7 @@
import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
import { PlayerOwnedAugmentation } from "../Augmentation/PlayerOwnedAugmentation"; import { PlayerOwnedAugmentation } from "../Augmentation/PlayerOwnedAugmentation";
import { Player } from "@player"; import { Player } from "@player";
import { FormattedCode, Script } from "../Script/Script"; import { Script } from "../Script/Script";
import { GetAllServers } from "../Server/AllServers"; import { GetAllServers } from "../Server/AllServers";
import { resolveTextFilePath } from "../Paths/TextFilePath"; import { resolveTextFilePath } from "../Paths/TextFilePath";
import { resolveScriptFilePath } from "../Paths/ScriptFilePath"; import { resolveScriptFilePath } from "../Paths/ScriptFilePath";
@ -54,7 +54,7 @@ function hasChanges(code: string): boolean {
return false; return false;
} }
function convert(code: string): FormattedCode { function convert(code: string): string {
const lines = code.split("\n"); const lines = code.split("\n");
const out: string[] = []; const out: string[] = [];
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {