-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[No QA] ai-review: rule for composition over configuration #80222
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
Merged
AndrewGable
merged 7 commits into
Expensify:main
from
callstack-internal:clean-react-patterns-1
Jan 26, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef7e882
feat: add react-clean-patterns-1 rule for favoring composition over c…
adhorodyski 8b79f99
fix god->mega components and a typo
adhorodyski 030854c
clarify CLEAN-REACT-PATTERNS-1 condition for new features vs components
adhorodyski 6ae8ac7
add cross-reference to AI reviewer rules in React Coding Standards se…
adhorodyski 0dc257e
add Table component counter-example to CLEAN-REACT-PATTERNS-1
adhorodyski 2c0ff0e
rm a redundant duplicated line
adhorodyski c33c119
fix wording around horizontal/vertical growth over time
adhorodyski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -825,6 +825,156 @@ async function submitForm(data: FormData) { | |
|
|
||
| --- | ||
|
|
||
| ### [CLEAN-REACT-PATTERNS-1] Favor composition over configuration | ||
|
|
||
| - **Search patterns**: `shouldShow`, `shouldEnable`, `canSelect`, `enable`, `disable`, configuration props patterns | ||
|
|
||
| - **Condition**: Flag ONLY when ALL of these are true: | ||
|
|
||
| - A **new feature** is being introduced OR an **existing component's API is being expanded with new props** | ||
|
mountiny marked this conversation as resolved.
|
||
| - The change adds configuration properties (flags, conditional logic) | ||
| - These configuration options control feature presence or behavior within the component | ||
| - These features could instead be expressed as composable child components | ||
|
|
||
| **Features that should be expressed as child components:** | ||
| - Optional UI elements that could be composed in | ||
| - New behavior that could be introduced as new children | ||
| - Features that currently require parent component code changes | ||
|
|
||
| **DO NOT flag if:** | ||
| - Props are narrow, stable values needed for coordination between composed parts (e.g., `reportID`, `data`, `columns`) | ||
| - The component uses composition and child components for features | ||
| - Parent components stay stable as features are added | ||
|
|
||
| - **Reasoning**: When new features are implemented by adding configuration (props, flags, conditional logic) to existing components, if requirements change, then those components must be repeatedly modified, increasing coupling, surface area, and regression risk. Composition ensures features scale horizontally, limits the scope of changes, and prevents components from becoming configuration-driven "mega components". | ||
|
|
||
| Good (composition): | ||
|
|
||
| - Features expressed as composable children | ||
| - Parent stays stable; add features by adding children | ||
|
|
||
| ```tsx | ||
| <Table data={items} columns={columns}> | ||
| <Table.SearchBar /> | ||
| <Table.Header /> | ||
| <Table.Body /> | ||
| </Table> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <SelectionList data={items}> | ||
| <SelectionList.TextInput /> | ||
| <SelectionList.Body /> | ||
| </SelectionList> | ||
| ``` | ||
|
|
||
| Bad (configuration): | ||
|
|
||
| - Features controlled by boolean flags | ||
| - Adding a new feature requires modifying the Table component's API | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it make sense to add sth like "When configuration is fine" with examples? For example
|
||
|
|
||
| ```tsx | ||
| <Table | ||
| data={items} | ||
| columns={columns} | ||
| shouldShowSearchBar | ||
| shouldShowHeader | ||
| shouldEnableSorting | ||
| shouldShowPagination | ||
| shouldHighlightOnHover | ||
| /> | ||
|
|
||
| type TableProps = { | ||
| data: Item[]; | ||
| columns: Column[]; | ||
| shouldShowSearchBar?: boolean; // ❌ Could be <Table.SearchBar /> | ||
| shouldShowHeader?: boolean; // ❌ Could be <Table.Header /> | ||
| shouldEnableSorting?: boolean; // ❌ Configuration for header behavior | ||
| shouldShowPagination?: boolean; // ❌ Could be <Table.Pagination /> | ||
| shouldHighlightOnHover?: boolean; // ❌ Configuration for styling behavior | ||
| }; | ||
| ``` | ||
|
|
||
| ```tsx | ||
| <SelectionList | ||
| data={items} | ||
| shouldShowTextInput | ||
| shouldShowTooltips | ||
| shouldScrollToFocusedIndex | ||
| shouldDebounceScrolling | ||
| shouldUpdateFocusedIndex | ||
| canSelectMultiple | ||
| disableKeyboardShortcuts | ||
| /> | ||
|
|
||
| type SelectionListProps = { | ||
| shouldShowTextInput?: boolean; // ❌ Could be <SelectionList.TextInput /> | ||
| shouldShowConfirmButton?: boolean; // ❌ Could be <SelectionList.ConfirmButton /> | ||
| textInputOptions?: {...}; // ❌ Configuration object for the above | ||
| }; | ||
| ``` | ||
|
adhorodyski marked this conversation as resolved.
|
||
|
|
||
| Good (children manage their own state): | ||
|
|
||
| ```tsx | ||
| // Children are self-contained and manage their own state | ||
| // Parent only passes minimal data (IDs) | ||
| // Adding new features doesn't require changing the parent | ||
| function ReportScreen({ params: { reportID }}) { | ||
| return ( | ||
| <> | ||
| <ReportActionsView reportID={reportID} /> | ||
| // other features | ||
| <Composer /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| // Component accesses stores and calculates its own state | ||
| // Parent doesn't know the internals | ||
| function ReportActionsView({ reportID }) { | ||
| const [reportOnyx] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`); | ||
| const reportActions = getFilteredReportActionsForReportView(unfilteredReportActions); | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| Bad (parent manages child state): | ||
|
|
||
| ```tsx | ||
| // Parent fetches and manages state for its children | ||
| // Parent has to know child implementation details | ||
| function ReportScreen({ params: { reportID }}) { | ||
| const [reportOnyx] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {allowStaleData: true, canBeMissing: true}); | ||
| const reportActions = useMemo(() => getFilteredReportActionsForReportView(unfilteredReportActions), [unfilteredReportActions]); | ||
| const [reportMetadata = defaultReportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportIDFromRoute}`, {canBeMissing: true, allowStaleData: true}); | ||
| const {reportActions: unfilteredReportActions, linkedAction, sortedAllReportActions, hasNewerActions, hasOlderActions} = usePaginatedReportActions(reportID, reportActionIDFromRoute); | ||
| const parentReportAction = useParentReportAction(reportOnyx); | ||
| const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActions ?? [], isOffline, reportTransactionIDs); | ||
| const isTransactionThreadView = isReportTransactionThread(report); | ||
| // other onyx connections etc | ||
|
|
||
| return ( | ||
| <> | ||
| <ReportActionsView | ||
| report={report} | ||
| reportActions={reportActions} | ||
| isLoadingInitialReportActions={reportMetadata?.isLoadingInitialReportActions} | ||
| hasNewerActions={hasNewerActions} | ||
| hasOlderActions={hasOlderActions} | ||
| parentReportAction={parentReportAction} | ||
| transactionThreadReportID={transactionThreadReportID} | ||
| isReportTransactionThread={isTransactionThreadView} | ||
| /> | ||
| // other features | ||
| <Composer /> | ||
| </> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Instructions | ||
|
|
||
| 1. **First, get the list of changed files and their diffs:** | ||
|
|
||
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
Oops, something went wrong.
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.
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.
I am not 100% sure how AI reviewer works in this area, but I think we should be specific here that all we care about are props. For example,
shouldShowcan beconst shouldShowSpinner = ...,user.canSelectSomethingorfunction shouldShow2FAModal(). This probably forces to determine if it's a prop definition or not for each matchThere 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.
In general I think we could define them as
should*,can*etc