Skip to content

fix: preserve nested JS return expressions#341

Open
honor2030 wants to merge 1 commit into
browser-use:mainfrom
honor2030:fix/js-nested-return
Open

fix: preserve nested JS return expressions#341
honor2030 wants to merge 1 commit into
browser-use:mainfrom
honor2030:fix/js-nested-return

Conversation

@honor2030
Copy link
Copy Markdown

@honor2030 honor2030 commented May 12, 2026

Summary

  • Evaluate JS snippets as-is first instead of lexically scanning for return
  • Retry with the existing IIFE wrapper only when Chrome reports SyntaxError: Illegal return statement
  • Add regression coverage so nested function/arrow returns keep normal JavaScript completion-value semantics

Root cause

js() used a lightweight string scanner to find return before evaluation. That scanner could not distinguish top-level returns from returns nested inside functions/arrow functions, so expressions like const f = () => { return 1 }; f() were wrapped as an IIFE and lost their completion value.

Test Plan

  • RED: uv run --with pytest --with-editable . python -m pytest tests/integration/test_js.py::test_nested_arrow_return_is_not_wrapped -q failed before the fix because the expression was wrapped as (function(){...})().
  • GREEN: uv run --with pytest --with-editable . python -m pytest tests/integration/test_js.py -q18 passed
  • uv run --with pytest --with-editable . python -m pytest tests -q95 passed
  • uv run --with-editable . python -m compileall -q src tests
  • uv run --with pip --with-editable . python -m pip checkNo broken requirements found.
  • uv run --with pip-audit --with-editable . pip-auditNo known vulnerabilities found (local package skipped because it is not on PyPI)
  • uv run --with ruff --with-editable . ruff check --select B src/browser_harness/helpers.py tests/integration/test_js.pyAll checks passed!

Summary by cubic

Evaluate JS snippets as-is and only wrap them in an IIFE when Chrome reports “Illegal return statement.” This preserves completion values and stops wrapping returns inside nested functions or arrows.

  • Bug Fixes
    • Remove the string-based return scanner; detect top-level return via runtime error and retry with (function(){...})().
    • Do not wrap when input already starts with ( (avoid double-wrapping).
    • Add integration tests for illegal-return retry, nested arrow returns, and whitespace after return.

Written for commit 987c55f. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant