Repository navigation
fix(compiler): skip slot boundaries for hook calls in module-scope IIFEs - #1574
Merged
Merged
Conversation
The plain .ts/.js hook pass wrapped every `obj.useX()` call in `withSlot(_h$N, () => obj.useX())`, but declares its slot constants after the module body to keep authored line numbers. A hook method evaluated while the module initializes, e.g. `export const initial = store.useValue()`, therefore threw `ReferenceError: Cannot access '_h$0' before initialization`. Such a call runs outside every render and needs no boundary. The scope analysis now stamps `_octaneModuleInitCall` on a hook-method call with no enclosing function (plain pass only; the full compiler declares its slots before the body) and `hookMethodName` skips it, so the surgical and inline emitters and optional-chain lowering all leave it authored. Static fields, static blocks, computed keys and decorators of a module-level class run at module init too. An instance field initializer runs with each construction, possibly during render, so it gets an `initializer` scope that keeps its boundary, as a constructor body does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…methods # Conflicts: # packages/octane/src/compiler/hook-deps.js
The merge with #1546 shares the instance-field `initializer` scope with its withinFunction, so a module-declared custom hook called from an instance field also keeps its own slot. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ule init A module-scope IIFE runs during module initialization, but its body is a function scope, so its hook-method and custom-hook calls still got a withSlot(_h$N, ...) boundary and the module threw a TDZ ReferenceError on load. A call whose callee is an arrow or function expression (directly, through TypeScript or paren wrappers, or via .call/.apply) now marks that function's scope invokedInPlace, and withinFunction walks through it. An IIFE inside a component or hook still has that function above it and keeps its boundary. Generator bodies run on .next(), possibly during a render, so they do not count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
marked this pull request as ready for review
October 2, 2026 15:14
Contributor
Benchmark report🟢 No size increases and no timing regressions outside noise. Compares js-framework: 1k/10k row operations (ms, lower is better)All 20 timed operations (20 within noise)
Application bundle size (bytes)No changes across 45 measured values. Public-import reachability bundles (bytes)No changes across 105 measured values. CI job durations
Compares the CI run for
All 26 timed jobs
|
…hooks # Conflicts: # packages/octane/src/compiler/hook-deps.js # packages/octane/tests/transitive-hook-ownership.test.ts
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
Follows #1572 (merged), which skips the boundary for hook calls evaluated at module init. This PR extends that to functions the module invokes in place.
.ts/.jsmodule that calls a hook-named method or custom hook inside a function it invokes in place now evaluates, for exampleexport const value = (() => store.useValue())(). Before, it threwReferenceError: Cannot access '_h$0' before initializationon load.withSlotboundary. This covers generator bodies and IIFEs inside a component or hook.Why
The plain hook pass appends its
_h$Nslot constants after the module body. #1572 skips the boundary for calls evaluated while the module initializes, usingwithinFunction(scope)inhook-deps.js. #1546's custom-hook calls already go through the same check. A module-scope IIFE also runs during module initialization, but its body is afunctionscope, so its hook calls still got a boundary and hit the TDZ.Changes
immediatelyInvokedFunction(call)returns the arrow or function expression a call runs in place: directly, through TypeScript or paren wrappers (viaunwrapValue), or via.call/.apply.buildScopesmarks that function's scopeinvokedInPlacebefore the walk reaches its body. This follows the IIFE detection used bycompile.jswhen it rejects hooks in plain JS loops.withinFunctionwalks through aninvokedInPlacescope, because its body runs with the code around it. A module-scope IIFE therefore counts as module init, and so do its nested IIFEs and parameter defaults. An IIFE inside a component or hook still has that function above it and keeps its boundary, so it needs no separate "is the IIFE itself within a function" check..next(), which a render may call. Async functions count: the body before the firstawaitruns in place, and the rest runs in a microtask outside every render.withinFunctionis the only reader of the flag, and only the plain pass consults it, so full-compiler output is unchanged.Validation
pnpm exec vitest run packages/octane/tests/transitive-hook-ownership.test.ts(octaneandoctane-prodprojects)evaluates hook methods in functions the module invokes in place({client, server} × {surgical,inlineHookMemo: true}). It covers arrow, function expression, unparenthesized.call,.apply, anas-wrapped callee, an optional call, a nested IIFE, a parameter default, imported/local/aliased custom hooks, and async before and afterawait. With fix(compiler): leave hook methods called during module init unslotted #1572'shook-deps.js, all 4 configurations fail with the TDZReferenceError.keeps a boundary for hook methods in a {generator, nested} IIFE body that runs during render(both emitters, SSR + client mount + update). Each fails with'firstfirst'when its guard is removed: the generator exclusion, or walking through an in-place scope rather than stopping at it.pnpm typecheck:filesandpnpm format:files:checkon the changed files,pnpm synccleanpnpm test/pnpm typecheck/pnpm format:checkrepo-wide: left to CI. Local full runs were load-bound: load average about 60 on 18 cores, with timeouts in unrelated files that pass in isolation.Performance: compile-time only. The pass adds one shape check per
CallExpressionand one boolean per scope record. Emitted code changes only for module-init IIFEs, which previously crashed on load. No benchmark can measure this.Risk / follow-ups
items.map((x) => store.useLabel(x))),.bind(...)(), andnew (function () { … })(). The receiver of.mapis not provably an array, and treating a deferred callback as module init would silently share slots, which is worse than a loud load error.compile.jsIIFE precedent.canPrintProgramwants a visitor forTemplateElement, but esrap prints those insideTemplateLiteral. This is being handled in a separate session, and the fixture avoids template literals.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Compiler-only scope analysis change affects when plain modules wrap hook calls; incorrect IIFE detection could mis-slot hooks at load time or during render, but behavior is covered by new SSR/client tests.
Overview
Fixes module-load crashes in the plain
.ts/.jshook pass when hooks run inside immediately invoked arrow or function expressions at module scope (e.g.export const x = (() => store.useValue())()), which previously threw a TDZReferenceErroron_h$Nbecause slot constants are emitted after the module body.hook-deps.jsnow detects in-place invocations (direct(),.call,.apply, with TS/paren unwrapping) viaimmediatelyInvokedFunction, marks those function scopes asinvokedInPlace, and treats them like module initialization inwithinFunctionso custom hooks and hook-named methods skipwithSlotboundaries there. Generators are excluded so bodies that run on.next()during render still get boundaries; nested IIFEs inside components/hooks are unchanged.Adds regression tests for many module-init IIFE shapes (async, nested, custom hooks) and for render-time generator/nested-return cases that must keep boundaries.
Reviewed by Cursor Bugbot for commit a0d44dc. Bugbot is set up for automated code reviews on this repo. Configure here.