[compiler] Fix JSX tags prefixed with _ or $ incorrectly treated as host elements#36688
Open
sleitor wants to merge 1 commit into
Open
[compiler] Fix JSX tags prefixed with _ or $ incorrectly treated as host elements#36688sleitor wants to merge 1 commit into
_ or $ incorrectly treated as host elements#36688sleitor wants to merge 1 commit into
Conversation
…, `_Bar`, `$Foo`) treated as host elements Previously, `lowerJsxElementName` only checked `if (tag.match(/^[A-Z]/)))` to identify component references. This means tags starting with `_`, `$`, a digit, or any character other than an ASCII letter were incorrectly treated as BuiltinTag (host/intrinsic elements) rather than component references. JSX semantics mirror Babel's JSX transform: a tag is a host element if and only if it starts with a **lowercase** letter (a-z). Everything else (uppercase, `_`, `$`, etc.) refers to an in-scope identifier and should be loaded as a component. Fix: change the guard from `tag.match(/^[A-Z]/)` to `!tag.match(/^[a-z]/)`, which correctly classifies identifiers like `_Bar` and `$Foo` as component references. Fixes facebook#36601
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
Fixes #36601
Problem
In
lowerJsxElementName(BuildHIR.ts), the conditionif (tag.match(/^[A-Z]/))only treats JSX tags starting with an uppercase letter as component references. Tags starting with_,$, or any other non-letter character fell through to theelsebranch and were incorrectly classified asBuiltinTag(host/intrinsic elements).This meant that
<_Bar />or<$Foo />was treated like<div />, causing the compiler to skip memoization of the component and potentially producing incorrect output.Root Cause
JSX semantics (as implemented by Babel's JSX transform) are:
The original code only handled the first half of that rule ("starts with uppercase → component") while ignoring identifiers like
_Barand$Foo.Fix
Change:
To:
This correctly classifies any JSX identifier that does NOT start with a lowercase letter as a component reference, matching JSX spec semantics.
Test
Added fixture
jsx-underscore-prefix-componentthat renders<_Bar />and verifies the compiler correctly memoizes it as a component reference.How did you test this change?
jsx-underscore-prefix-componentyarn workspace babel-plugin-react-compiler lint✅