Repository navigation
fix(ts-optimizer): keep outer loop items in scope for nested loop handlers - #9231
Merged
Varixo merged 3 commits intoOct 22, 2026
Merged
Conversation
…dlers A handler inside an inner `.map()` that captured the outer loop's item got its `.w([...])` binding hoisted to the component body, where the outer item does not exist, so SSR threw `ReferenceError: facet is not defined`. Place the binding in the innermost scope that declares one of its captures, converting an expression-bodied outer callback into a block body, as the Rust optimizer does. The segment path also resolves typed loop params and `for...of` heads now, and looks at every capture instead of the first. Fixes #9190
🦋 Changeset detectedLatest commit: 983a74b The changes in this PR will be included in the next version bump. This PR includes changesets to release 8 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Varixo
requested changes
Oct 20, 2026
Member
|
Hey, thanks for the PR! Added some comments. Also worth adding snapshots for rust -> ts optimizer compability |
… nested loop bindings Wrap expression-bodied callbacks at the parsed body start so comments after => no longer drop the JSX or break the build, and group hoisted .w() bindings per enclosing scope so sibling nested loops don't share one callback. Adds regression tests for both cases.
Contributor
Author
|
Thanks for the thorough review, @Varixo! Both issues are addressed in f066d87:
I added regression tests for all three cases in |
@qwik.dev/core
@qwik.dev/router
@qwik.dev/utils
eslint-plugin-qwik
create-qwik
@qwik.dev/optimizer
@qwik.dev/devtools
@qwik.dev/mcp
commit: |
tuurbo
added a commit
to tuurbo/qwik
that referenced
this pull request
Oct 26, 2026
A loop event handler's `.w([...])` binding must be declared where all of its captures are in scope. Both optimizers missed that in some shapes: - Rust treated every hoisting function as having a body-block scope, so in a component that returns its JSX directly the outer loop item matched the component and the binding was hoisted out of both loops (SSR ReferenceError). It also resolved captures by name to their outermost declaration, so a handler in `function f(results)` captured a shadowed component `results`. - The TS optimizer dropped these bindings in hoist builds of such components, and segment builds found scopes by scanning source text, which missed loop callbacks without parentheses and `function` scopes. Segment builds now share the hoist path's parsed-scope placement. Fixes QwikDev#9306 Refs QwikDev#9190, QwikDev#9231 Co-Authored-By: Claude Opus 5.5 <[email protected]>
tuurbo
added a commit
to tuurbo/qwik
that referenced
this pull request
Oct 26, 2026
A loop event handler's `.w([...])` binding must be declared where all of its captures are in scope. Both optimizers missed that in some shapes: - Rust treated every hoisting function as having a body-block scope, so in a component that returns its JSX directly the outer loop item matched the component and the binding was hoisted out of both loops (SSR ReferenceError). It also resolved captures by name to their outermost declaration, so a handler in `function f(results)` captured a shadowed component `results`. - The TS optimizer dropped these bindings in hoist builds of such components, and segment builds found scopes by scanning source text, which missed loop callbacks without parentheses and `function` scopes. Segment builds now share the hoist path's parsed-scope placement, which throws instead of dropping a binding it cannot place. Fixes QwikDev#9306 Refs QwikDev#9190, QwikDev#9231 Co-Authored-By: Claude Opus 5.5 <[email protected]>
tuurbo
added a commit
to tuurbo/qwik
that referenced
this pull request
Oct 27, 2026
A loop event handler's `.w([...])` binding must be declared where all of its captures are in scope. Both optimizers missed that in some shapes: - Rust treated every hoisting function as having a body-block scope, so in a component that returns its JSX directly the outer loop item matched the component and the binding was hoisted out of both loops (SSR ReferenceError). It also resolved captures by name to their outermost declaration, so a handler in `function f(results)` captured a shadowed component `results`. - The TS optimizer dropped these bindings in hoist builds of such components, and segment builds found scopes by scanning source text, which missed loop callbacks without parentheses and `function` scopes. Segment builds now share the hoist path's parsed-scope placement, which throws instead of dropping a binding it cannot place. Fixes QwikDev#9306 Refs QwikDev#9190, QwikDev#9231 Co-Authored-By: Claude Opus 5.5 <[email protected]>
tuurbo
added a commit
to tuurbo/qwik
that referenced
this pull request
Oct 28, 2026
A loop event handler's `.w([...])` binding must be declared where all of its captures are in scope, before its first use. Both optimizers missed that in some shapes: - Rust treated every hoisting function as having a body-block scope, so in a component that returns its JSX directly the outer loop item matched the component and the binding was hoisted out of both loops (SSR ReferenceError). It also resolved captures by name to their outermost declaration, so a handler in `function f(results)` captured a shadowed component `results`. - The TS optimizer dropped these bindings in hoist builds of such components, and segment builds found scopes by scanning source text, which missed loop callbacks without parentheses and `function` scopes. Segment builds now share the hoist path's parsed-scope placement, which throws instead of dropping a binding it cannot place. - Both declared the binding after the last `const`/`let` in its scope, so it read a local before a later reassignment, and JSX built into a `const` used it before its declaration (ReferenceError). It now goes right before the first statement that uses it; calls to a hoisted function declaration, or JSX tags rendering it, count as uses. Fixes QwikDev#9306 Refs QwikDev#9190, QwikDev#9231 Co-Authored-By: Claude Opus 5.5 <[email protected]>
tuurbo
added a commit
to tuurbo/qwik
that referenced
this pull request
Oct 28, 2026
A loop event handler's `.w([...])` binding must be declared where all of its captures are in scope, before its first use. Both optimizers missed that in some shapes: - Rust treated every hoisting function as having a body-block scope, so in a component that returns its JSX directly the outer loop item matched the component and the binding was hoisted out of both loops (SSR ReferenceError). It also resolved captures by name to their outermost declaration, so a handler in `function f(results)` captured a shadowed component `results`. - The TS optimizer dropped these bindings in hoist builds of such components, and segment builds found scopes by scanning source text, which missed loop callbacks without parentheses and `function` scopes. Segment builds now share the hoist path's parsed-scope placement, which throws instead of dropping a binding it cannot place. - Both declared the binding after the last `const`/`let` in its scope, so it read a local before a later reassignment, and JSX built into a `const` used it before its declaration (ReferenceError). It now goes right before the first statement that uses it; calls to a hoisted function declaration, or JSX tags rendering it, count as uses. TS also keeps it below its captures' declarations; Rust never captures a local declared later. Fixes QwikDev#9306 Refs QwikDev#9190, QwikDev#9231 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What is it?
Description
Fixes #9190.
What was wrong. A handler inside a nested loop that captures the outer loop's item crashes SSR:
The TypeScript optimizer emitted the handler's
.w([facet, props])binding at the top of the component body, outside both loops, so the server build threwReferenceError: facet is not defined. In hoist builds it also renamed the loop param tofacet1, because a freefacetnow existed at component scope.Root cause. Loop handlers whose remaining captures need a
.w()binding go through two placement paths, and neither looked past the component body:rewrite/inline-body.ts). Every binding was inserted before the component's top-level statement that uses it. That's correct only when all captures are declared in the component body. The regression came from 42323eb.segment/body-transforms.ts).hoistedCaptureNames[0].,, so a typed param like(facet: Facet)never matched.for…ofheads..map((facet) => …)worked by luck. The issue's TSX code didn't.Fix. I followed the Rust optimizer, which hoists the binding to the deepest scope among its captures' declarations and turns an expression-bodied callback into a block body to make room for it.
inline-body.ts.{ const s_X = q_X.w([…]); return …; }. The original parens are kept andreturnstays on the same line.body-transforms.ts.for (const x of …) {heads.Generated SSR output for the snippet above now looks like this:
How it was tested.
packages/ts-optimizer/tests/optimizer/jsx/nested-loop-capture-scope.test.tscovers:smartsegments with typed paramsconstfor…ofloopspackages/qwik/src/core/tests/loops.spec.tsx(nested expression-bodied loops, outer item and props captured).dist/ts-optimizer.mjs:ssrRenderToDomfails withReferenceError: facet is not definedanddomRenderlogs nothing.packages/ts-optimizer:vitest rungives 1543 passed / 1 skipped, including theconvergenceandrust-behavior-paritysuites.tsc --noEmitis clean.vitest run packages/qwik/src(afterpnpm build --qwik --optimizer --dev): 2649 passed / 18 skipped.eslintandprettier --checkon the changed files: clean.Checklist
pnpm changeSummary by CodeRabbit