-
Notifications
You must be signed in to change notification settings - Fork 10
Fixes for use-styled-react-import rule for compound components
#406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 8b4f659 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes two critical issues in the use-styled-react-import ESLint rule when handling compound components (components with dot notation like ActionList.Item, FormControl.Label). The rule now correctly identifies parent components from compound notation and properly handles aliasing across all component instances within JSX elements.
- Enhanced compound component recognition by extracting parent component names from dot notation
- Improved aliasing logic using regex-based replacement for complete compound component updates
- Added comprehensive test coverage for compound component scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/rules/use-styled-react-import.js | Enhanced JSX element handler to recognize parent components in compound notation and improved aliasing with regex-based replacement |
| src/rules/tests/use-styled-react-import.test.js | Added test cases for compound component handling and complex aliasing scenarios |
| fixes.push(fixer.replaceText(jsxNode.closingElement.name, aliasName)) | ||
| } | ||
| // Replace all instances of the component name (both main component and compound components) | ||
| const componentPattern = new RegExp(`\\b${componentName}(?=\\.|\\s|>)`, 'g') |
Copilot
AI
Sep 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The componentName variable is being used directly in a RegExp constructor without escaping special regex characters. If componentName contains regex metacharacters like '.', '[', or '*', it could cause unexpected matching behavior or regex injection. Use a regex escape function or escape the componentName before constructing the pattern.
🐛 Problem
The
use-styled-react-importrule had two major issues when handling compound components (components with dot notation likeActionList.Item,FormControl.Label, etc.):Compound components were not recognized: When using
<ActionList.Item sx={{...}}>, the rule failed to detect thatActionListshould be imported from@primer/styled-reactbecause it was looking for"ActionList.Item"in the styled components list instead of"ActionList".Incomplete aliasing of compound components: When a parent component required aliasing (e.g.,
FormControl→StyledFormControl), compound components inside the same JSX element were not being updated. For example:✅ Solution
1. Enhanced Compound Component Recognition
Modified the JSX element handler to extract the parent component name from compound components:
This ensures that
<ActionList.Item sx={{...}}>correctly identifies thatActionListneeds to be moved to@primer/styled-react.2. Improved Aliasing with Regex-Based Fix
Replaced the individual tag replacement approach with a comprehensive regex-based solution that handles all component instances within a JSX element:
This ensures that when
FormControlgets aliased toStyledFormControl, all compound components within the same JSX element are also updated:<FormControl sx={{...}}>→<StyledFormControl sx={{...}}><FormControl.Label>→<StyledFormControl.Label><FormControl.Caption>→<StyledFormControl.Caption>📋 Test Cases Added
Added comprehensive test coverage for the new functionality:
<ActionList.Item sx={{...}}>properly movesActionListimportFormControlused both with and withoutsxprop, requiring aliasing with proper compound component updates🧪 Testing
📝 Examples
Before Fix
After Fix
🎯 Impact
This fix ensures that the
use-styled-react-importrule now properly handles all Primer React compound components, making it more reliable for teams using components like:ActionList.ItemFormControl.Label,FormControl.Caption,FormControl.ValidationPageLayout.Header,PageLayout.Content,PageLayout.PaneThe rule now provides complete and consistent enforcement of the styled-react import patterns across all component usage scenarios.