fix: preserve nested JS return expressions#341
Open
honor2030 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
returnSyntaxError: Illegal return statementRoot cause
js()used a lightweight string scanner to findreturnbefore evaluation. That scanner could not distinguish top-level returns from returns nested inside functions/arrow functions, so expressions likeconst f = () => { return 1 }; f()were wrapped as an IIFE and lost their completion value.Test Plan
uv run --with pytest --with-editable . python -m pytest tests/integration/test_js.py::test_nested_arrow_return_is_not_wrapped -qfailed before the fix because the expression was wrapped as(function(){...})().uv run --with pytest --with-editable . python -m pytest tests/integration/test_js.py -q→18 passeduv run --with pytest --with-editable . python -m pytest tests -q→95 passeduv run --with-editable . python -m compileall -q src testsuv run --with pip --with-editable . python -m pip check→No broken requirements found.uv run --with pip-audit --with-editable . pip-audit→No 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.py→All 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.
returnscanner; detect top-levelreturnvia runtime error and retry with(function(){...})().((avoid double-wrapping).return.Written for commit 987c55f. Summary will update on new commits.