From 527bcaa83d9d31e848ca1bea1a5b8532ab361527 Mon Sep 17 00:00:00 2001 From: mofeiZ <34200447+mofeiZ@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:27:39 -0500 Subject: [PATCH] [compiler] patch: rewrite scope dep/decl in inlineJsxTransform (#31431) This bugfix is needed to land #31199 PropagateScopeDepsHIR infers scope declarations for the `inline-jsx-transform` test fixture (the non-hir version does not). These declarations must get the rewritten phi identifiers --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31431). * #31204 * #31202 * #31203 * #31201 * #31200 * #31346 * #31199 * __->__ #31431 * #31345 * #31197 --- .../src/Optimization/InlineJsxTransform.ts | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts index 50822e78d977b..d97a4da1eccc4 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts @@ -383,6 +383,30 @@ export function inlineJsxTransform( mapTerminalOperands(block.terminal, place => handlePlace(place, blockId, inlinedJsxDeclarations), ); + + if (block.terminal.kind === 'scope') { + const scope = block.terminal.scope; + for (const dep of scope.dependencies) { + dep.identifier = handleIdentifier( + dep.identifier, + inlinedJsxDeclarations, + ); + } + + for (const [origId, decl] of [...scope.declarations]) { + const newDecl = handleIdentifier( + decl.identifier, + inlinedJsxDeclarations, + ); + if (newDecl.id !== origId) { + scope.declarations.delete(origId); + scope.declarations.set(decl.identifier.id, { + identifier: newDecl, + scope: decl.scope, + }); + } + } + } } /** @@ -697,10 +721,10 @@ function handlePlace( inlinedJsxDeclaration == null || inlinedJsxDeclaration.blockIdsToIgnore.has(blockId) ) { - return {...place}; + return place; } - return {...place, identifier: {...inlinedJsxDeclaration.identifier}}; + return {...place, identifier: inlinedJsxDeclaration.identifier}; } function handlelValue( @@ -715,8 +739,20 @@ function handlelValue( inlinedJsxDeclaration == null || inlinedJsxDeclaration.blockIdsToIgnore.has(blockId) ) { - return {...lvalue}; + return lvalue; } - return {...lvalue, identifier: {...inlinedJsxDeclaration.identifier}}; + return {...lvalue, identifier: inlinedJsxDeclaration.identifier}; +} + +function handleIdentifier( + identifier: Identifier, + inlinedJsxDeclarations: InlinedJsxDeclarationMap, +): Identifier { + const inlinedJsxDeclaration = inlinedJsxDeclarations.get( + identifier.declarationId, + ); + return inlinedJsxDeclaration == null + ? identifier + : inlinedJsxDeclaration.identifier; }