Skip to content

Conversation

pritjasani08
Copy link

Summary

This PR fixes issue #33689 where the React Compiler wasn't properly handling cases where named functions call other named functions when they're defined in a "weird order" (i.e., when a function is called before it's declared).

Problem

The getFunctionReferencedBeforeDeclarationAtTopLevel function only checked for function references at the top level scope, but didn't account for nested function calls where one named function calls another named function that's defined later in the code.

Solution

Enhanced the function to:

  1. Check if a function call is within another function that's being compiled
  2. Detect when a named function calls another named function
  3. Properly handle the ordering by adding the called function to referencedBeforeDeclaration when appropriate

Changes

  • Modified: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

    • Enhanced getFunctionReferencedBeforeDeclarationAtTopLevel function to handle nested function calls
    • Added logic to detect and handle named functions calling other named functions defined later
  • Added: Test fixtures to verify the fix

    • compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/named-functions-order.js
    • compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/named-functions-order.expect.md

Test Cases

The fix handles these scenarios:

  1. Named function calling another named function defined later in the same component
  2. Nested function calls where inner functions call functions defined later
  3. Complex scenarios with multiple levels of function nesting

Example

function Component(props) {
  function firstFunction() {
    return secondFunction(); // Calls secondFunction before it's declared
  }
  
  function secondFunction() {
    return props.message || "Hello";
  }
  
  return firstFunction();
}

This now works correctly with the React Compiler.

##Checklist

  • I have read the Contributing Guide
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

Fixes #33689

Copy link

meta-cla bot commented Sep 20, 2025

Hi @pritjasani08!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Copy link

meta-cla bot commented Sep 20, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Compiler Bug]: Not handling named functions calling named functions when defined in a weird order
1 participant