Repository navigation
[compiler] Allow ref access in ResizeObserver / MutationObserver callbacks - #37895
Merged
Merged
Conversation
willbyers1
approved these changes
Oct 20, 2026
javache
approved these changes
Oct 21, 2026
javache
requested changes
Oct 21, 2026
zhoubiao188
force-pushed
the
scan-2026-10-19
branch
from
October 22, 2026 14:35
6b9d6a3 to
61d472d
Compare
…backs
Both constructors only retain the callback and invoke it after render, so
reading a ref inside the callback is safe. Without a modeled signature they
fall back to the default effects for unknown constructors, which report
Cannot access refs during render
for the common `new ResizeObserver(() => ref.current)` pattern. That is the
same false positive that react#37764 fixed for `IntersectionObserver`.
Model both observers in the TypeScript and Rust compilers. Each takes a
single constructor argument — unlike `IntersectionObserver`, whose options
object is a second constructor parameter, `ResizeObserver` and
`MutationObserver` receive their options in `observe(target, options)` — so
both are modeled with a single `Freeze`d callback.
The fixtures cover the accepted patterns (including `observe` options) plus
one negative case asserting that mutating a hook value inside the callback
is still rejected.
zhoubiao188
force-pushed
the
scan-2026-10-19
branch
from
October 22, 2026 14:42
61d472d to
8267383
Compare
javache
approved these changes
Oct 23, 2026
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 23, 2026
…backs (#37895) ## Summary `new ResizeObserver(() => ref.current)` and `new MutationObserver(() => ref.current)` currently report a false positive: ``` Error: Cannot access refs during render > 6 | return new ResizeObserver(() => ref.current); | ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to `IntersectionObserver` in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors. This PR models both the same way in the TypeScript and Rust compilers: - `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike `IntersectionObserver`, whose options object is a second constructor argument, the `{box}` option of `ResizeObserver` belongs to `observe(target, options)`.) - `MutationObserver(callback)`: single `Freeze`d callback. ## Validation - `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and `observe(target, {box})` options do not trip the validation. - `allow-ref-access-in-mutation-observer.ts`: callback compiles. - `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a `useMemo` value inside the callback is **still** rejected (`This value cannot be modified`), i.e. the `Freeze` effect is preserved and only the ref false positive is removed. - Unknown constructors are unaffected: `new Observer(() => ref.current)` still errors. - Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (`fbt/*`, `debugger*`, `conflict-codegen-instrument-forget`), so no new snapshot deltas. - `tsc --noEmit` and `prettier check-changed` pass. ## Notes The observers' instance methods (`observe`/`unobserve`/`disconnect`) are intentionally left unmodeled, matching `IntersectionObserver`. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [23cd68d](23cd68d)
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 23, 2026
…backs (#37895) ## Summary `new ResizeObserver(() => ref.current)` and `new MutationObserver(() => ref.current)` currently report a false positive: ``` Error: Cannot access refs during render > 6 | return new ResizeObserver(() => ref.current); | ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to `IntersectionObserver` in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors. This PR models both the same way in the TypeScript and Rust compilers: - `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike `IntersectionObserver`, whose options object is a second constructor argument, the `{box}` option of `ResizeObserver` belongs to `observe(target, options)`.) - `MutationObserver(callback)`: single `Freeze`d callback. ## Validation - `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and `observe(target, {box})` options do not trip the validation. - `allow-ref-access-in-mutation-observer.ts`: callback compiles. - `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a `useMemo` value inside the callback is **still** rejected (`This value cannot be modified`), i.e. the `Freeze` effect is preserved and only the ref false positive is removed. - Unknown constructors are unaffected: `new Observer(() => ref.current)` still errors. - Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (`fbt/*`, `debugger*`, `conflict-codegen-instrument-forget`), so no new snapshot deltas. - `tsc --noEmit` and `prettier check-changed` pass. ## Notes The observers' instance methods (`observe`/`unobserve`/`disconnect`) are intentionally left unmodeled, matching `IntersectionObserver`. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [23cd68d](23cd68d)
javache
pushed a commit
that referenced
this pull request
Oct 26, 2026
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]>
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 26, 2026
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 26, 2026
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
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
new ResizeObserver(() => ref.current)andnew MutationObserver(() => ref.current)currently report a false positive:Both constructors only retain the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to
IntersectionObserverin #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors.This PR models both the same way in the TypeScript and Rust compilers:
ResizeObserver(callback): singleFreezed callback. (UnlikeIntersectionObserver, whose options object is a second constructor argument, the{box}option ofResizeObserverbelongs toobserve(target, options).)MutationObserver(callback): singleFreezed callback.Validation
allow-ref-access-in-resize-observer.ts: plain callback compiles, andobserve(target, {box})options do not trip the validation.allow-ref-access-in-mutation-observer.ts: callback compiles.error.invalid-mutate-hook-value-in-resize-observer.js: mutating auseMemovalue inside the callback is still rejected (This value cannot be modified), i.e. theFreezeeffect is preserved and only the ref false positive is removed.new Observer(() => ref.current)still errors.fbt/*,debugger*,conflict-codegen-instrument-forget), so no new snapshot deltas.tsc --noEmitandprettier check-changedpass.Notes
The observers' instance methods (
observe/unobserve/disconnect) are intentionally left unmodeled, matchingIntersectionObserver.