From 03e8d02b7913c49745abe32e097010094b4be5a9 Mon Sep 17 00:00:00 2001 From: stirante Date: Wed, 27 Mar 2024 10:11:32 +0100 Subject: [PATCH] Fix issue when stringify function would throw an error when stringifying unloaded entity --- .eslintrc.json | 4 +++- src/ColorJSON.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 1b7b4e4..9b050b1 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -25,6 +25,8 @@ "minecraft-linting/avoid-unnecessary-command": "error", "prefer-const": "warn", "@typescript-eslint/no-explicit-any": "off", - "eslint-disable no-unused-labels": "off" + "eslint-disable no-unused-labels": "off", + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": "warn" } } \ No newline at end of file diff --git a/src/ColorJSON.ts b/src/ColorJSON.ts index def035e..8904b10 100644 --- a/src/ColorJSON.ts +++ b/src/ColorJSON.ts @@ -85,6 +85,7 @@ export default class ColorJSON { * Transforms a function into a JSON representation. * @param value - The function to transform. */ + // eslint-disable-next-line @typescript-eslint/ban-types protected stringifyFunction(value: Function): string { // Functions are transformed to predefined function token return this.FunctionColor + this.FunctionValue + ChatColor.RESET; @@ -244,7 +245,13 @@ export default class ColorJSON { // Sort the keys const allKeys = [...keySet].sort(); // Get all entries - const entries = allKeys.map((key: string) => [key, (value as any)[key] ?? undefined]).filter(([key, val]) => typeof val !== 'function' && val !== void 0); + const entries = allKeys.map((key: string) => { + try { + return [key, (value as any)[key] ?? void 0]; + } catch(e) { + return [key, void 0]; + } + }).filter(([, val]) => typeof val !== 'function' && val !== void 0); const result = this.stringifyObject(value, name, entries, indentLevel); this.clearCycle(value); return result;