Comfy-Org / Comfy-Org/ComfyUI_frontend

Refactor: Extract shared parenthesis-finding utilities from editAttention into src/base with unit/permutation tests

Open
#11,351 0 comments 0 reactions 1 assignee Claimed by @kaili-yang View on GitHub
area:testing developer experience
Dominant language
TypeScript
Stars
2k
Forks
699
Avg merge
1d 7h
Merged PRs (30d)
490

Description

## Summary

The `findNearestEnclosure` function in `src/extensions/core/editAttention.ts` contains inline logic for scanning backwards/forwards to find opening and closing parentheses. This logic could be generalized into pure shared utility functions (e.g., `findOpeningParen`, `findClosingParen`) and moved to `src/base` so they are independently testable and reusable across the codebase.

## Motivation

This was flagged as a nit during PR #11301 (https://github.com/Comfy-Org/ComfyUI_frontend/pull/11301#discussion_r3103497250) by @christian-byrne.

Currently the scanning logic is embedded directly in `findNearestEnclosure`, which makes it harder to test edge cases in isolation. Extracting these into small, pure, module-level functions in `src/base` would:

- Improve reusability across other extensions or utilities that need to parse bracket structures
- Enable thorough unit testing and permutation testing of the low-level bracket-finding logic independently
- Make `findNearestEnclosure` (and similar helpers) easier to read and reason about

## Proposed Work

1. **Extract** pure helper functions from `findNearestEnclosure` in `src/extensions/core/editAttention.ts`:
- e.g. `findOpeningParen(text: string, fromIndex: number): number | null`
- e.g. `findClosingParen(text: string, fromIndex: number): number | null`
2. **Move** these utilities to an appropriate module under `src/base` (e.g., `src/base/stringUtils.ts` or `src/base/parensUtils.ts`)
3. **Refactor** `findNearestEnclosure` (and any related helpers in `editAttention.ts`) to use the new shared functions
4. **Add** unit tests and permutation tests for the new shared utilities (balanced/unbalanced brackets, empty strings, nested parens, cursor at various positions, etc.)

## References

- PR: https://github.com/Comfy-Org/ComfyUI_frontend/pull/11301
- Comment: https://github.com/Comfy-Org/ComfyUI_frontend/pull/11301#discussion_r3103497250
- Requested by: @christian-byrne

┆Issue is synchronized with this [Notion page](https://www.notion.so/Issue-11351-Refactor-Extract-shared-parenthesis-finding-utilities-from-editAttention-into-src-ba-3456d73d365081e186ead1439a6c68b2) by [Unito](https://www.unito.io)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.