diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts index 702eaf0f692..0ae338f5c78 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -3081,6 +3081,12 @@ function isReorderableExpression( return true; } } + case 'TSInstantiationExpression': { + const innerExpr = (expr as NodePath).get( + 'expression', + ) as NodePath; + return isReorderableExpression(builder, innerExpr, allowLocalIdentifiers); + } case 'RegExpLiteral': case 'StringLiteral': case 'NumericLiteral': diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.expect.md new file mode 100644 index 00000000000..b80c0370f79 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function id(x: T): T { + return x; +} + +export function Component({fn = id}: {fn?: (x: T) => T}) { + const value = fn('hi' as T); + return
{String(value)}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function id(x) { + return x; +} + +export function Component(t0) { + const $ = _c(4); + const { fn: t1 } = t0; + const fn = t1 === undefined ? id : t1; + let t2; + if ($[0] !== fn) { + t2 = fn("hi" as T); + $[0] = fn; + $[1] = t2; + } else { + t2 = $[1]; + } + const value = t2; + const t3 = String(value); + let t4; + if ($[2] !== t3) { + t4 =
{t3}
; + $[2] = t3; + $[3] = t4; + } else { + t4 = $[3]; + } + return t4; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
hi
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.tsx new file mode 100644 index 00000000000..6382962edda --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ts-instantiation-default-param.tsx @@ -0,0 +1,13 @@ +function id(x: T): T { + return x; +} + +export function Component({fn = id}: {fn?: (x: T) => T}) { + const value = fn('hi' as T); + return
{String(value)}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +};