From 97bf9de83a124b5098c07d38da472ffb5611b271 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Fri, 28 Feb 2025 00:13:32 +1100 Subject: [PATCH] [Refactor] Standardise code style - modern JS (#636) Uses unicorn rules to auto-refactor code. All verified. --- eslint.config.js | 11 ------ src/CurveEditor.ts | 4 +-- src/LGraph.ts | 6 ++-- src/LGraphCanvas.ts | 73 ++++++++++++++++++--------------------- src/LGraphNode.ts | 8 ++--- src/LiteGraphGlobal.ts | 12 +++---- src/MapProxyHandler.ts | 2 +- src/widgets/KnobWidget.ts | 4 +-- 8 files changed, 50 insertions(+), 70 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 7d7ad7eef..7455b1bda 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -59,25 +59,14 @@ export default tseslint.config( rules: { // Temporarily disabled // See https://github.com/Comfy-Org/litegraph.js/issues/629 - "unicorn/catch-error-name": "off", - "unicorn/consistent-existence-index-check": "off", - "unicorn/no-array-callback-reference": "off", - "unicorn/no-array-push-push": "off", - "unicorn/no-console-spaces": "off", "unicorn/no-lonely-if": "off", "unicorn/no-this-assignment": "off", - "unicorn/no-typeof-undefined": "off", "unicorn/no-useless-switch-case": "off", "unicorn/no-zero-fractions": "off", - "unicorn/numeric-separators-style": "off", "unicorn/prefer-add-event-listener": "off", "unicorn/prefer-blob-reading-methods": "off", - "unicorn/prefer-date-now": "off", "unicorn/prefer-default-parameters": "off", - "unicorn/prefer-keyboard-event-key": "off", - "unicorn/prefer-logical-operator-over-ternary": "off", "unicorn/prefer-math-min-max": "off", - "unicorn/prefer-native-coercion-functions": "off", "unicorn/prefer-query-selector": "off", "unicorn/prefer-spread": "off", "unicorn/prefer-structured-clone": "off", diff --git a/src/CurveEditor.ts b/src/CurveEditor.ts index ae3a38225..f9395e71e 100644 --- a/src/CurveEditor.ts +++ b/src/CurveEditor.ts @@ -33,7 +33,7 @@ export class CurveEditor { if (pn[0] < f) continue const r = pn[0] - p[0] - if (Math.abs(r) < 0.00001) return p[1] + if (Math.abs(r) < 0.000_01) return p[1] const local_f = (f - p[0]) / r return p[1] * (1.0 - local_f) + pn[1] * local_f @@ -177,7 +177,7 @@ export class CurveEditor { const h = this.size[1] - this.margin * 2 const num = points.length const p2: Point = [0, 0] - let min_dist = 1000000 + let min_dist = 1_000_000 let closest = -1 for (let i = 0; i < num; ++i) { diff --git a/src/LGraph.ts b/src/LGraph.ts index e03770346..d371a47af 100644 --- a/src/LGraph.ts +++ b/src/LGraph.ts @@ -426,9 +426,7 @@ export class LGraph implements LinkNetwork, Serialisable { const start = LiteGraph.getTime() this.globaltime = 0.001 * (start - this.starttime) - const nodes = this._nodes_executable - ? this._nodes_executable - : this._nodes + const nodes = this._nodes_executable || this._nodes if (!nodes) return limit = limit || nodes.length @@ -750,7 +748,7 @@ export class LGraph implements LinkNetwork, Serialisable { ): void { mode = mode || LGraphEventMode.ALWAYS - const nodes = this._nodes_in_order ? this._nodes_in_order : this._nodes + const nodes = this._nodes_in_order || this._nodes if (!nodes) return for (const node of nodes) { diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index e86c5c9d2..275fcf6bb 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -1225,14 +1225,14 @@ export class LGraphCanvas implements ConnectionColorContext { }) input.addEventListener("keydown", function (e: KeyboardEvent) { dialog.is_modified = true - if (e.keyCode == 27) { + if (e.key == "Escape") { // ESC dialog.close() - } else if (e.keyCode == 13) { + } else if (e.key == "Enter") { // save inner() // @ts-expect-error Intentional - undefined if not present - } else if (e.keyCode != 13 && e.target.localName != "textarea") { + } else if (e.target.localName != "textarea") { return } e.preventDefault() @@ -1384,7 +1384,7 @@ export class LGraphCanvas implements ConnectionColorContext { const kV = Object.values(LiteGraph.NODE_MODES).indexOf(v) const fApplyMultiNode = function (node) { - if (kV >= 0 && LiteGraph.NODE_MODES[kV]) { + if (kV !== -1 && LiteGraph.NODE_MODES[kV]) { node.changeMode(kV) } else { console.warn("unexpected mode: " + v) @@ -5841,8 +5841,7 @@ export class LGraphCanvas implements ConnectionColorContext { const options = ["Add Node", null] if (opts.allow_searchbox) { - options.push("Search") - options.push(null) + options.push("Search", null) } // get defaults nodes for this slottype @@ -5979,11 +5978,11 @@ export class LGraphCanvas implements ConnectionColorContext { const input = value_element input.addEventListener("keydown", function (e: KeyboardEvent) { dialog.is_modified = true - if (e.keyCode == 27) { + if (e.key == "Escape") { // ESC dialog.close() } else if ( - e.keyCode == 13 && + e.key == "Enter" && (e.target as Element).localName != "textarea" ) { if (callback) { @@ -6169,17 +6168,17 @@ export class LGraphCanvas implements ConnectionColorContext { this.focus() }) input.addEventListener("keydown", function (e) { - if (e.keyCode == 38) { + if (e.key == "ArrowUp") { // UP changeSelection(false) - } else if (e.keyCode == 40) { + } else if (e.key == "ArrowDown") { // DOWN changeSelection(true) - } else if (e.keyCode == 27) { + } else if (e.key == "Escape") { // ESC // @ts-expect-error Panel? dialog.close() - } else if (e.keyCode == 13) { + } else if (e.key == "Enter") { if (selected) { select(unescape(selected.dataset["type"])) } else if (first) { @@ -6316,7 +6315,7 @@ export class LGraphCanvas implements ConnectionColorContext { ? options.node_from.findOutputSlot(options.slot_from.name) : -1 // @ts-expect-error change interface check - if (iS == -1 && typeof options.slot_from.slot_index !== "undefined") iS = options.slot_from.slot_index + if (iS == -1 && options.slot_from.slot_index !== undefined) iS = options.slot_from.slot_index break case "number": iS = options.slot_from @@ -6325,7 +6324,7 @@ export class LGraphCanvas implements ConnectionColorContext { // try with first if no name set iS = 0 } - if (typeof options.node_from.outputs[iS] !== "undefined") { + if (options.node_from.outputs[iS] !== undefined) { if (iS !== false && iS > -1) { options.node_from.connectByType(iS, node, options.node_from.outputs[iS].type) } @@ -6345,7 +6344,7 @@ export class LGraphCanvas implements ConnectionColorContext { ? options.node_to.findInputSlot(options.slot_from.name) : -1 // @ts-expect-error change interface check - if (iS == -1 && typeof options.slot_from.slot_index !== "undefined") iS = options.slot_from.slot_index + if (iS == -1 && options.slot_from.slot_index !== undefined) iS = options.slot_from.slot_index break case "number": iS = options.slot_from @@ -6354,7 +6353,7 @@ export class LGraphCanvas implements ConnectionColorContext { // try with first if no name set iS = 0 } - if (typeof options.node_to.inputs[iS] !== "undefined") { + if (options.node_to.inputs[iS] !== undefined) { if (iS !== false && iS > -1) { // try connection options.node_to.connectByTypeOutput(iS, node, options.node_to.inputs[iS].type) @@ -6424,7 +6423,7 @@ export class LGraphCanvas implements ConnectionColorContext { // filter supported // types const keys = Object.keys(LiteGraph.registered_node_types) - filtered = keys.filter(inner_test_filter) + filtered = keys.filter(x => inner_test_filter(x)) } else { filtered = [] for (const i in LiteGraph.registered_node_types) { @@ -6488,13 +6487,11 @@ export class LGraphCanvas implements ConnectionColorContext { function inner_test_filter( type: string, - optsIn?: - | number - | { - inTypeOverride?: string | boolean - outTypeOverride?: string | boolean - skipFilter?: boolean - }, + optsIn?: { + inTypeOverride?: string | boolean + outTypeOverride?: string | boolean + skipFilter?: boolean + }, ): boolean { optsIn = optsIn || {} const optsDef = { @@ -6654,14 +6651,14 @@ export class LGraphCanvas implements ConnectionColorContext { // @ts-expect-error input.value = v input.addEventListener("keydown", function (e) { - if (e.keyCode == 27) { + if (e.key == "Escape") { // ESC dialog.close() - } else if (e.keyCode == 13) { + } else if (e.key == "Enter") { // ENTER // save inner() - } else if (e.keyCode != 13) { + } else { dialog.modified() return } @@ -6754,9 +6751,9 @@ export class LGraphCanvas implements ConnectionColorContext { for (const iX of aI) { iX.addEventListener("keydown", function (e) { dialog.modified() - if (e.keyCode == 27) { + if (e.key == "Escape") { dialog.close() - } else if (e.keyCode != 13) { + } else if (e.key != "Enter") { return } // set value ? @@ -6850,7 +6847,7 @@ export class LGraphCanvas implements ConnectionColorContext { root.toggleAltContent = function (force: unknown) { let vTo: string let vAlt: string - if (typeof force != "undefined") { + if (force !== undefined) { vTo = force ? "block" : "none" vAlt = force ? "none" : "block" } else { @@ -6863,7 +6860,7 @@ export class LGraphCanvas implements ConnectionColorContext { root.toggleFooterVisibility = function (force: unknown) { let vTo: string - if (typeof force != "undefined") { + if (force !== undefined) { vTo = force ? "block" : "none" } else { vTo = root.footer.style.display != "block" ? "block" : "none" @@ -7035,7 +7032,7 @@ export class LGraphCanvas implements ConnectionColorContext { break case "Mode": { const kV = Object.values(LiteGraph.NODE_MODES).indexOf(value) - if (kV >= 0 && LiteGraph.NODE_MODES[kV]) { + if (kV !== -1 && LiteGraph.NODE_MODES[kV]) { node.changeMode(kV) } else { console.warn("unexpected mode: " + value) @@ -7284,8 +7281,7 @@ export class LGraphCanvas implements ConnectionColorContext { content: "Align Selected To", has_submenu: true, callback: LGraphCanvas.onNodeAlign, - }) - options.push({ + }, { content: "Distribute Nodes", has_submenu: true, callback: LGraphCanvas.createDistributeMenu, @@ -7455,16 +7451,13 @@ export class LGraphCanvas implements ConnectionColorContext { dialog.querySelector("button").addEventListener("click", inner) input.addEventListener("keydown", function (e) { dialog.is_modified = true - if (e.keyCode == 27) { + if (e.key == "Escape") { // ESC dialog.close() - } else if (e.keyCode == 13) { + } else if (e.key == "Enter") { // save inner() - } else if ( - e.keyCode != 13 && - (e.target as Element).localName != "textarea" - ) { + } else if ((e.target as Element).localName != "textarea") { return } e.preventDefault() diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index c8e844907..463e9b83f 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -1172,9 +1172,9 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { options = options || {} if (this.onAction) { // enable this to give the event an ID - options.action_call ||= this.id + "_" + (action ? action : "action") + "_" + Math.floor(Math.random() * 9999) + options.action_call ||= this.id + "_" + (action || "action") + "_" + Math.floor(Math.random() * 9999) - this.graph.nodes_actioning[this.id] = action ? action : "actioning" + this.graph.nodes_actioning[this.id] = action || "actioning" this.onAction(action, param, options) this.graph.nodes_actioning[this.id] = false @@ -2255,7 +2255,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { if (slotIndex !== null) return this.connect(slot, target_node, slotIndex, optsIn?.afterRerouteId) - console.debug("[connectByType]: no way to connect type: ", target_slotType, " to node: ", target_node) + console.debug("[connectByType]: no way to connect type:", target_slotType, "to node:", target_node) return null } @@ -2286,7 +2286,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { if (slotIndex !== null) return source_node.connect(slotIndex, this, slot, optsIn?.afterRerouteId) - console.debug("[connectByType]: no way to connect type: ", source_slotType, " to node: ", source_node) + console.debug("[connectByType]: no way to connect type:", source_slotType, "to node:", source_node) return null } diff --git a/src/LiteGraphGlobal.ts b/src/LiteGraphGlobal.ts index c3d76b34f..abbe9af92 100644 --- a/src/LiteGraphGlobal.ts +++ b/src/LiteGraphGlobal.ts @@ -72,7 +72,7 @@ export class LiteGraphGlobal { CONNECTING_LINK_COLOR = "#AFA" /** avoid infinite loops */ - MAX_NUMBER_OF_NODES = 10000 + MAX_NUMBER_OF_NODES = 10_000 /** default node position */ DEFAULT_POSITION = [100, 100] /** ,"circle" */ @@ -297,7 +297,7 @@ export class LiteGraphGlobal { } } else { this.getTime = function () { - return new Date().getTime() + return Date.now() } } } @@ -446,8 +446,8 @@ export class LiteGraphGlobal { if (this.catch_exceptions) { try { node = new base_class(title) - } catch (err) { - console.error(err) + } catch (error) { + console.error(error) return null } } else { @@ -554,8 +554,8 @@ export class LiteGraphGlobal { dynamicScript.src = src docHeadObj.append(dynamicScript) script_file.remove() - } catch (err) { - if (this.throw_errors) throw err + } catch (error) { + if (this.throw_errors) throw error if (this.debug) console.log("Error while reloading " + src) } } diff --git a/src/MapProxyHandler.ts b/src/MapProxyHandler.ts index f4c649645..65e249cab 100644 --- a/src/MapProxyHandler.ts +++ b/src/MapProxyHandler.ts @@ -25,7 +25,7 @@ export class MapProxyHandler implements ProxyHandler> } ownKeys(target: Map): ArrayLike { - return [...target.keys()].map(x => String(x)) + return [...target.keys()].map(String) } get(target: Map, p: string | symbol): any { diff --git a/src/widgets/KnobWidget.ts b/src/widgets/KnobWidget.ts index b084e3924..c1e9a4b08 100644 --- a/src/widgets/KnobWidget.ts +++ b/src/widgets/KnobWidget.ts @@ -33,8 +33,8 @@ export class KnobWidget extends BaseWidget implements IKnobWidget { return { minHeight: 60, minWidth: 20, - maxHeight: 1000000, - maxWidth: 1000000, + maxHeight: 1_000_000, + maxWidth: 1_000_000, } }