[compiler] Store babel scope on Environment

Stores the Babel `Scope` object for the current function on the Environment, allowing access later for generating new globally unique names. The idea is to expose a small subset of the capabilities of the Scope API via Environment, so that the rest of the compiler remains decoupled from Babel. Ideally we'd use our own Scope implementation too, but we can punt on that for now since the parts we're using (global id generation) seem pretty reliable.

ghstack-source-id: 37f7113b11fe980688dae423883cf6b8890e77be
Pull Request resolved: https://github.com/facebook/react/pull/30328
This commit is contained in:
Joe Savona 2024-07-15 12:28:05 +09:00
parent 6c0386e976
commit 547c3c45ab
2 changed files with 5 additions and 0 deletions

View File

@ -117,6 +117,7 @@ export function* run(
): Generator<CompilerPipelineValue, CodegenFunction> {
const contextIdentifiers = findContextIdentifiers(func);
const env = new Environment(
func.scope,
fnType,
config,
contextIdentifiers,

View File

@ -41,6 +41,7 @@ import {
ShapeRegistry,
addHook,
} from "./ObjectShape";
import { Scope as BabelScope } from "@babel/traverse";
export const ExternalFunctionSchema = z.object({
// Source for the imported module that exports the `importSpecifierName` functions
@ -504,6 +505,7 @@ export class Environment {
#nextIdentifer: number = 0;
#nextBlock: number = 0;
#nextScope: number = 0;
#scope: BabelScope;
logger: Logger | null;
filename: string | null;
code: string | null;
@ -515,6 +517,7 @@ export class Environment {
#hoistedIdentifiers: Set<t.Identifier>;
constructor(
scope: BabelScope,
fnType: ReactFunctionType,
config: EnvironmentConfig,
contextIdentifiers: Set<t.Identifier>,
@ -523,6 +526,7 @@ export class Environment {
code: string | null,
useMemoCacheIdentifier: string
) {
this.#scope = scope;
this.fnType = fnType;
this.config = config;
this.filename = filename;