Comfy-Org / Comfy-Org/ComfyUI_frontend
LGraphCanvas: add type guard for widget.getContextMenuOptions() and validate returned value
- Dominant language
- TypeScript
- Stars
- 2k
- Forks
- 699
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 490
Description
Summary
The context-menu code path in LGraphCanvas.processContextMenu currently calls widget.getContextMenuOptions via an inline/structural assertion and unconditionally uses .length/spread on the return value. Because getContextMenuOptions is not part of IBaseWidget’s contract, custom widgets could return undefined or a non-array value, causing runtime errors when opening the context menu.
Why
- Improves type safety and runtime robustness for third-party/custom widgets.
- Aligns with our repository guidance to prefer user-defined type guards over inline type assertions within litegraph code.
- Prevents crashes if a widget’s getContextMenuOptions returns undefined or a non-array.
Scope
- File: src/lib/litegraph/src/LGraphCanvas.ts
- Area: processContextMenu() where a widget is retrieved via node.getWidgetOnPos(...) and optional getContextMenuOptions is invoked.
Proposed change (high-level, no code attached)
- Introduce a user-defined type guard (e.g., hasWidgetContextMenuOptions) to check for a callable getContextMenuOptions on the widget.
- Validate the return value with Array.isArray(...) before using .length or spreading into menu_info.
- Keep behavior identical when a well-formed array is returned.
Acceptance criteria
- The guard is used before invoking getContextMenuOptions in processContextMenu.
- The result is validated with Array.isArray before accessing .length or spreading.
- No runtime regressions for existing widgets; context menus render as expected.
- Unit tests cover:
- A widget that returns a valid non-empty array.
- A widget that returns an empty array.
- A widget that returns undefined or non-array (should be safely ignored).
References
- PR: https://github.com/Comfy-Org/ComfyUI_frontend/pull/8985
- Discussion prompting this issue: https://github.com/Comfy-Org/ComfyUI_frontend/pull/8985#discussion_r2876836653
Requested by
@christian-byrne
┆Issue is synchronized with this [Notion page](https://www.notion.so/Issue-9390-LGraphCanvas-add-type-guard-for-widget-getContextMenuOptions-and-validate-returned--3196d73d36508117b3c5c5ec745c7a9a) by [Unito](https://www.unito.io)
Contributor guide
Assessment
This issue has not been assessed yet.