[compiler] avoid outer-scope identifier conflicts in synthesizeName#36194
Open
sleitor wants to merge 1 commit intofacebook:mainfrom
Open
[compiler] avoid outer-scope identifier conflicts in synthesizeName#36194sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor wants to merge 1 commit intofacebook:mainfrom
Conversation
When a component uses an outer-scope binding named `$` (e.g. a helper
function or imported component), the compiler's synthesizeName('$') would
not detect the conflict and would generate a memo cache variable also
named `$`, overwriting the outer-scope binding at runtime.
The fix extends the collision check in `Context.synthesizeName` to also
check `env.programContext.hasReference()`, which queries the Babel scope
for existing bindings (including outer-scope declarations and imports).
If a conflict is found, the synthesized name is incremented until unique.
Fixes facebook#36167
Add two regression test fixtures:
- jsx-dollar-sign-component.tsx — outer function named $
- jsx-dollar-sign-component-imported.tsx — $ imported from a module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a component uses an outer-scope binding named
$(e.g. a utility helper function or an imported component alias), the compiler'ssynthesizeName('$')would not detect the conflict and would generate a memo cache variable also named$, overwriting the outer-scope binding at runtime inside Client Components.Root cause
Context.synthesizeNamechecksuniqueIdentifiers(local variable names + referenced globals inside the compiled function) but does not check the Babel program scope for outer-scope bindings. A function or import named$that is used inside a component but not recursively referenced inside the same function body is invisible to the collision check.Fix
Extend the collision-avoidance loop in
Context.synthesizeNameto also callenv.programContext.hasReference(validated), which queries the Babel scope for existing bindings (imports, outer declarations). If a conflict is found, the synthesized name is incremented ($0,$1, …) until a unique name is found.Before fix:
After fix:
How did you test this change?
Added two regression test fixtures in
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/:jsx-dollar-sign-component.tsx— outer helper function named$jsx-dollar-sign-component-imported.tsx—$imported from an external moduleAll 1721 compiler tests pass (
yarn workspace babel-plugin-react-compiler test).Fixes #36167