[Refactor] Standardise code style - modern JS (#636)

Uses unicorn rules to auto-refactor code.  All verified.
This commit is contained in:
filtered
2025-02-28 00:13:32 +11:00
committed by GitHub
parent 6737828a2b
commit 97bf9de83a
8 changed files with 50 additions and 70 deletions

View File

@@ -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",

View File

@@ -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) {

View File

@@ -426,9 +426,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
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<SerialisableGraph> {
): 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) {

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -25,7 +25,7 @@ export class MapProxyHandler<V> implements ProxyHandler<Map<number | string, V>>
}
ownKeys(target: Map<number | string, V>): ArrayLike<string | symbol> {
return [...target.keys()].map(x => String(x))
return [...target.keys()].map(String)
}
get(target: Map<number | string, V>, p: string | symbol): any {

View File

@@ -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,
}
}