Skip to content

Commit

Permalink
[compiler] Store babel scope on Environment
Browse files Browse the repository at this point in the history
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: #30328
  • Loading branch information
josephsavona committed Jul 17, 2024
1 parent 6c0386e commit 547c3c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function* run(
): Generator<CompilerPipelineValue, CodegenFunction> {
const contextIdentifiers = findContextIdentifiers(func);
const env = new Environment(
func.scope,
fnType,
config,
contextIdentifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -515,6 +517,7 @@ export class Environment {
#hoistedIdentifiers: Set<t.Identifier>;

constructor(
scope: BabelScope,
fnType: ReactFunctionType,
config: EnvironmentConfig,
contextIdentifiers: Set<t.Identifier>,
Expand All @@ -523,6 +526,7 @@ export class Environment {
code: string | null,
useMemoCacheIdentifier: string
) {
this.#scope = scope;
this.fnType = fnType;
this.config = config;
this.filename = filename;
Expand Down

0 comments on commit 547c3c4

Please sign in to comment.