[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
Joe Savona committed
Jul 15, 2024 at 12:28 UTC
547c3c45abff04fe5e63b49b0ce98ef807d5110f
2 files changed
+5
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
+1
@@ -117,6 +117,7 @@ export function* run(
117
): Generator<CompilerPipelineValue, CodegenFunction> {
118
const contextIdentifiers = findContextIdentifiers(func);
119
const env = new Environment(
120
+ func.scope,
121
fnType,
122
config,
123
contextIdentifiers,
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
+4
@@ -41,6 +41,7 @@ import {
41
ShapeRegistry,
42
addHook,
43
} from "./ObjectShape";
44
+import { Scope as BabelScope } from "@babel/traverse";
45
46
export const ExternalFunctionSchema = z.object({
47
// Source for the imported module that exports the `importSpecifierName` functions
@@ -504,6 +505,7 @@ export class Environment {
505
#nextIdentifer: number = 0;
506
#nextBlock: number = 0;
507
#nextScope: number = 0;
508
+ #scope: BabelScope;
509
logger: Logger | null;
510
filename: string | null;
511
code: string | null;
@@ -515,6 +517,7 @@ export class Environment {
517
#hoistedIdentifiers: Set<t.Identifier>;
518
519
constructor(
520
+ scope: BabelScope,
521
fnType: ReactFunctionType,
522
config: EnvironmentConfig,
523
contextIdentifiers: Set<t.Identifier>,
@@ -523,6 +526,7 @@ export class Environment {
526
code: string | null,
527
useMemoCacheIdentifier: string
528
) {
529
+ this.#scope = scope;
530
this.fnType = fnType;
531
this.config = config;
532
this.filename = filename;