Unblock React Compiler for 18 components with value blocks inside try

React Compiler cannot lower a conditional expression - `&&`, `||`, `??`, `?.`,
a ternary - inside a try block. Three techniques, picked per site:

- split `if (a && b)` into nested ifs, where there is no `else` to break
- hoist the expression into a const above the try, where it does not depend on
  anything the try produces
- move it into a module-scope helper, where it does

Optional calls become `if (f) f()`, which keeps the arguments unevaluated when
the callback is absent, exactly as `f?.()` does.

Skipped components: 125 -> 107.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tomek Zawadzki
2026-08-25 16:57:00 +02:00
parent f87fdd2ea2
commit 2d313d806f
18 changed files with 163 additions and 85 deletions
@@ -5,6 +5,15 @@ import {type CaptchaWebViewProps} from './CaptchaWebView.shared'
const REDIRECT_HOST = new URL(window.location.href).host
/**
* Module scope because React Compiler cannot lower an optional chain inside a
* `try`, and this one has to stay in the `try` - reading `location` on a
* cross-origin frame throws.
*/
function getFrameHref(frame: HTMLIFrameElement | null): string | undefined {
return frame?.contentWindow?.location.href
}
export function CaptchaWebView({
url,
stateParam,
@@ -29,7 +38,7 @@ export function CaptchaWebView({
) as HTMLIFrameElement
try {
const href = frame?.contentWindow?.location.href
const href = getFrameHref(frame)
if (!href) return
const urlp = new URL(href)
@@ -37,7 +46,12 @@ export function CaptchaWebView({
if (urlp.host !== REDIRECT_HOST) return
const code = urlp.searchParams.get('code')
if (urlp.searchParams.get('state') !== stateParam || !code) {
const stateMismatch = urlp.searchParams.get('state') !== stateParam
if (stateMismatch) {
onError({error: 'Invalid state or code'})
return
}
if (!code) {
onError({error: 'Invalid state or code'})
return
}