Repository: facebook/react
File: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts (TypeScript)

Code before:
     * assumptions elsewhere in the plugin.
     *
     * To get around this, we synthesize a new VariableDeclaration holding the compiled function
     * expression and insert it as a true sibling (ie within the Program's block statements).
     */
    case 'ArrowFunctionExpression':
    case 'FunctionExpression': {
      const funcExpr = createNewFunctionNode(originalFn, compiledFn);
      CompilerError.invariant(
        t.isArrowFunctionExpression(funcExpr) ||
          t.isFunctionExpression(funcExpr),
        {
          reason: 'Expected an (arrow) function expression to be created',
          description: `Got: ${funcExpr.type}`,
          loc: funcExpr.loc ?? null,
        },
      );
      CompilerError.invariant(compiledFn.id != null, {
        reason: 'Expected compiled function to have an identifier',
        loc: compiledFn.loc,
      });
      CompilerError.invariant(
        originalFn.parentPath.isVariableDeclarator() &&
          originalFn.parentPath.parentPath.isVariableDeclaration(),
        {
          reason: 'Expected a variable declarator parent',
          loc: originalFn.node.loc ?? null,
        },
      );
      const varDecl = originalFn.parentPath.parentPath;
      varDecl.insertAfter(
        t.variableDeclaration('const', [
          t.variableDeclarator(compiledFn.id, funcExpr),
        ]),
      );
      const insertedFuncExpr = varDecl.get('declarations')[0]!.get('init')!;
      CompilerError.invariant(
        insertedFuncExpr.isArrowFunctionExpression() ||
          insertedFuncExpr.isFunctionExpression(),
        {
          reason: 'Expected inserted (arrow) function expression',
          description: `Got: ${insertedFuncExpr}`,
          loc: insertedFuncExpr.node?.loc ?? null,
        },
      );
      return insertedFuncExpr;
    }
    default: {
      assertExhaustive(
        originalFn,
        `Inserting unhandled function: ${originalFn}`,

Review comment (refactor):
~~Since we're not using the `createNewFunctionNode` helper here, let's manually exclude from re-compilation (`ALREADY_COMPILED.add(transformedFn);`)~~

ohh nvm, I see we're already doing this after calling `insertNewOutlinedFunctionNode`. please disregard!

Code after:
     * plugin.
     *
     * To get around this, we always synthesize a new FunctionDeclaration for the outlined function
     * and insert it as a true sibling to the original function.
     */
    case 'ArrowFunctionExpression':
    case 'FunctionExpression': {
      CompilerError.invariant(
        originalFn.parentPath.isVariableDeclarator() &&
          originalFn.parentPath.parentPath.isVariableDeclaration(),
        {
          reason: 'Expected a variable declarator parent',
          loc: originalFn.node.loc ?? null,
        },
      );
      const fn: t.FunctionDeclaration = {
        type: 'FunctionDeclaration',
        id: compiledFn.id,
        loc: originalFn.node.loc ?? null,
        async: compiledFn.async,
        generator: compiledFn.generator,
        params: compiledFn.params,
        body: compiledFn.body,
      };
      const varDecl = originalFn.parentPath.parentPath;
      const insertedFuncDecl = varDecl.insertAfter(fn)[0]!;
      CompilerError.invariant(insertedFuncDecl.isFunctionDeclaration(), {
        reason: 'Expected inserted function declaration',
        description: `Got: ${insertedFuncDecl}`,
        loc: insertedFuncDecl.node?.loc ?? null,
      });
      return insertedFuncDecl;
    }
    default: {
      assertExhaustive(
        originalFn,
        `Inserting unhandled function: ${originalFn}`,
      );
    }
  }
}

/*
 * This is a hack to work around what seems to be a Babel bug. Babel doesn't
 * consistently respect the `skip()` function to avoid revisiting a node within
 * a pass, so we use this set to track nodes that we have compiled.
 */
const ALREADY_COMPILED: WeakSet<object> | Set<object> = new (WeakSet ?? Set)();

const DEFAULT_ESLINT_SUPPRESSIONS = [
  'react-hooks/exhaustive-deps',