Repository navigation
fix(compiler): leave optional-chain arguments unread in inferred dependencies - #1813
Merged
Merged
Conversation
…ndencies `run?.(options.label)` and `value?.[options.key]` evaluate their argument or computed key only once the receiver exists. Inferred hook dependency lists (`collectDependencies`) and compiler-memoized `use()` and SSR prop creations (`collectDepPaths`) read them unconditionally, so a missing receiver with an undefined `options` threw from the dependency array, and an `options.label` getter ran before the authored code would. Both collectors now walk arguments and computed keys after an optional link under a guard. Skipped one-level member reads use the existing guarded `__methodDep(root, name, true)` descriptor probe; receiver reads stay precise, and a non-skipped read of the same path keeps its plain member dependency. Fixes #1799 Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
marked this pull request as ready for review
October 6, 2026 11:55
Contributor
Benchmark report🟢 No budget breaches, no added work, and no timing changes beyond ±3%. Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
All 64 work counters are unchanged. Application bundle size (bytes)No changes across 60 measured values. Public-import reachability bundles (bytes)No changes across 105 measured values. |
8 of 11 tasks
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
collectDependencies) and compiler-memoizeduse()arguments and SSR prop creations (collectDepPaths).runandoptionsboth undefined,run?.(options.label) ?? 'idle'andvalue?.[options.key]now renderidleinstead of throwing. Anoptions.labelgetter is no longer called before the authored code would call it.Fixes #1799
Why
An optional chain evaluates the arguments and computed keys after its optional link only once the receiver is non-nullish. Both collectors walked them as unconditional reads. On
main, the issue's example compiles to these dependency arrays:mainrun?.(options.label) ?? 'idle'[run, options.label][run, __methodDep(options, "label", true)]useMemo(() => value?.[options.key])[value, options.key][value, __methodDep(options, "key", true)]useMemo(() => value?.list.at(options.index))[value?.list, options.index][value?.list, __methodDep(options, "index", true)]Each
mainarray throwsCannot read properties of undefined (reading 'label')whenoptionsis undefined, whether during SSR or in a client or plain-module hook.Changes
hook-deps.js: a newfollowsOptionalLink(node)checks whether a call or member comes after an optional link. It walks the callee/object spine throughTSNonNullExpressionand stops at a parenthesized chain.collectDependencieswalks those arguments and computed keys under the existing guard, so skipped one-level reads become the guarded descriptor probe from fix: preserve guarded property reads in inferred hook dependencies #1123. Receiver reads stay precise. The existing promotion still turns a guarded probe back into a plain dependency when the same path is also read unconditionally.compile.js,collectDepPaths:__methodDep(root, name, true), through a collision-safe private alias (requireRuntimeForContextnow allocates__methodDeplike the other private helpers).root?.propform.run?.(…)and after??keeps its value dependency.docs/differences-from-react.md: the guarded-read paragraph now names optional-chain arguments and keys, and notes thatuse()and SSR prop creations follow the same rule.Validation
pnpm format:check: not run repo-wide.pnpm format:files:checkpasses on every changed file.pnpm typecheck: not run repo-wide. CI's typecheck excludespackages/octane/tests.pnpm typecheck:fileson the three changed test files reports only implicit-anybindings in the.tsrxfixture, which its existing components onmainalso report.pnpm test: not run in full; CI runs it.octaneproject: 12 files, 2,075 tests pass. They areguarded-hook-dependencies,hydration/guarded-hook-dependencies,creation-dependencies,auto-hook-deps-behavior,signal-local-selectors,ssr-prop-flow-promises,ssr-parallel-use, and the compiler suitesauto-hook-deps,plain-hook-memo-fidelity,strong-mode,strong-state, andinline-hook-memo-codegen. After rebasing onto4b46332e26, the 3 owning files (664 tests) pass again.TypeError, and 20 of the 24 new creation cases fail. The 4 that pass are.tsxclientuse()arguments, which are not memoized.options?.labelinstead of the probe fails the throwing-getter SSR test (Error: skipped optional-chain read).expected 'first' to be 'second')..tsrxin client and server, dev and prod. Compared withmain, only the new fixture's 4 outputs changed. Compile time was dominated by machine load, so this PR makes no timing claim. The added work is a short walk down the chain, and only for calls with arguments and computed members.New coverage:
options.labelgetter, transitions to and from a receiver, and a field change on the sameoptionsobject. They run against plugin-compiled, dev and prod compiled, and plain-hook builds.use()-argument and prop cases across client and server,.tsrxand.tsx, and dev and prod.Risk / follow-ups
format?.(user.name)with a getter-backednameon a stableuserobject no longer refreshes when only that getter's value changes. Before this PR it was precise, but it threw wheneveruserwas missing.use()arguments are not covered. These are.ts/.jshook modules, handled bycollectParallelUseDependenciesinslot-hooks.js. That collector has no skip or defer policy at all: optional chains, callback bodies and unreplayable conditionals are all read eagerly. It's flagged as a separate follow-up rather than widened into this PR.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Changes compiler-emitted dependency arrays for optional calls and computed members; behavior is well-tested but trades getter precision on skipped paths for correctness when receivers are absent.
Overview
Inferred hook dependencies,
use()memo arguments, and SSR prop creations no longer evaluate arguments or computed keys that optional chaining would skip at render time. Patterns likerun?.(options.label)andvalue?.[options.key]stop throwing when the receiver oroptionsis missing, and no longer invoke getters on skipped paths during dependency evaluation.The compiler adds
followsOptionalLinkand routes those skipped member reads through guarded__methodDep(..., true)probes (same policy as conditional reads).collectDepPathscan still promote a probe to a plain dependency when the same path is read unconditionally elsewhere (e.g. after??). Documentation now states that optional-chain skipping applies to inferred deps and compiler-memoized creation sites.Reviewed by Cursor Bugbot for commit 9c7035b. Bugbot is set up for automated code reviews on this repo. Configure here.