Skip to content

Commit 3a357d1

Browse files
committed
Update on "compiler: distinguish globals/imports/module-locals"
We currently use `LoadGlobal` and `StoreGlobal` to represent any read (or write) of a variable defined outside the component or hook that is being compiled. This is mostly fine, but for a lot of things we want to do going forward (resolving types across modules, for example) it helps to understand the actual source of a variable. This PR is an incremental step in that direction. We continue to use LoadGlobal/StoreGlobal, but LoadGlobal now has a `binding:NonLocalBinding` instead of just the name of the global. The NonLocalBinding type tells us whether it was an import (and which kind, the source module name etc), a module-local binding, or a true global. By keeping the LoadGlobal/StoreGlobal instructions, most code that deals with "anything not declared locally" doesn't have to care about the difference. However, code that _does_ want to know the source of the value can figure it out. [ghstack-poisoned]
1 parent 7c14232 commit 3a357d1

File tree

1 file changed

+8
-1
lines changed
  • compiler/packages/babel-plugin-react-compiler/src/HIR

1 file changed

+8
-1
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export default class HIRBuilder {
255255
return { kind: "Global", name: originalName };
256256
}
257257

258-
// Check if the binding is from module scope, if so return null
258+
// Check if the binding is from module scope
259259
const outerBinding =
260260
this.parentFunction.scope.parent.getBinding(originalName);
261261
if (babelBinding === outerBinding) {
@@ -306,6 +306,13 @@ export default class HIRBuilder {
306306
isContextIdentifier(path: NodePath<t.Identifier | t.JSXIdentifier>): boolean {
307307
const binding = this.#resolveBabelBinding(path);
308308
if (binding) {
309+
// Check if the binding is from module scope, if so return null
310+
const outerBinding = this.parentFunction.scope.parent.getBinding(
311+
path.node.name
312+
);
313+
if (binding === outerBinding) {
314+
return false;
315+
}
309316
return this.#env.isContextIdentifier(binding.identifier);
310317
} else {
311318
return false;

0 commit comments

Comments
 (0)