@samitouri / QOS-React / commits / 527bcaa83d

[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

mofeiZ committed Nov 5, 2024 at 15:27 UTC 527bcaa83d9d31e848ca1bea1a5b8532ab361527
1 file changed +40 -4
compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts
+40 -4
@@ -383,6 +383,30 @@ export function inlineJsxTransform(
383 mapTerminalOperands(block.terminal, place =>
384 handlePlace(place, blockId, inlinedJsxDeclarations),
385 );
386 +
387 + if (block.terminal.kind === 'scope') {
388 + const scope = block.terminal.scope;
389 + for (const dep of scope.dependencies) {
390 + dep.identifier = handleIdentifier(
391 + dep.identifier,
392 + inlinedJsxDeclarations,
393 + );
394 + }
395 +
396 + for (const [origId, decl] of [...scope.declarations]) {
397 + const newDecl = handleIdentifier(
398 + decl.identifier,
399 + inlinedJsxDeclarations,
400 + );
401 + if (newDecl.id !== origId) {
402 + scope.declarations.delete(origId);
403 + scope.declarations.set(decl.identifier.id, {
404 + identifier: newDecl,
405 + scope: decl.scope,
406 + });
407 + }
408 + }
409 + }
410 }
411
412 /**
@@ -697,10 +721,10 @@ function handlePlace(
721 inlinedJsxDeclaration == null ||
722 inlinedJsxDeclaration.blockIdsToIgnore.has(blockId)
723 ) {
700 - return {...place};
724 + return place;
725 }
726
703 - return {...place, identifier: {...inlinedJsxDeclaration.identifier}};
727 + return {...place, identifier: inlinedJsxDeclaration.identifier};
728 }
729
730 function handlelValue(
@@ -715,8 +739,20 @@ function handlelValue(
739 inlinedJsxDeclaration == null ||
740 inlinedJsxDeclaration.blockIdsToIgnore.has(blockId)
741 ) {
718 - return {...lvalue};
742 + return lvalue;
743 }
744
721 - return {...lvalue, identifier: {...inlinedJsxDeclaration.identifier}};
745 + return {...lvalue, identifier: inlinedJsxDeclaration.identifier};
746 +}
747 +
748 +function handleIdentifier(
749 + identifier: Identifier,
750 + inlinedJsxDeclarations: InlinedJsxDeclarationMap,
751 +): Identifier {
752 + const inlinedJsxDeclaration = inlinedJsxDeclarations.get(
753 + identifier.declarationId,
754 + );
755 + return inlinedJsxDeclaration == null
756 + ? identifier
757 + : inlinedJsxDeclaration.identifier;
758 }