fix(react-transform): preserve children prop callback scope#2524
Conversation
🦋 Changeset detectedLatest commit: 50dcd1f The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
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 |
📝 WalkthroughWalkthroughThis pull request fixes a JSX transformation bug where Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
Comparing Footnotes
|
React External#743 Bundle Size — 680.2KiB (0%).50dcd1f(current) vs 989f345 main#736(baseline) Bundle metrics
|
| Current #743 |
Baseline #736 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
3 |
3 |
|
17 |
17 |
|
5 |
5 |
|
8.59% |
8.59% |
|
0 |
0 |
|
0 |
0 |
Bundle analysis report Branch colinaaa:codex/fix-react-transfo... Project dashboard
Generated by RelativeCI Documentation Report issue
React MTF Example#759 Bundle Size — 196.47KiB (0%).50dcd1f(current) vs 989f345 main#751(baseline) Bundle metrics
|
| Current #759 |
Baseline #751 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
3 |
3 |
|
173 |
173 |
|
66 |
66 |
|
44.07% |
44.07% |
|
2 |
2 |
|
0 |
0 |
Bundle size by type no changes
| Current #759 |
Baseline #751 |
|
|---|---|---|
111.23KiB |
111.23KiB |
|
85.24KiB |
85.24KiB |
Bundle analysis report Branch colinaaa:codex/fix-react-transfo... Project dashboard
Generated by RelativeCI Documentation Report issue
Web Explorer#9200 Bundle Size — 900.02KiB (0%).50dcd1f(current) vs 989f345 main#9192(baseline) Bundle metrics
Bundle size by type
|
| Current #9200 |
Baseline #9192 |
|
|---|---|---|
495.88KiB |
495.88KiB |
|
401.92KiB |
401.92KiB |
|
2.22KiB |
2.22KiB |
Bundle analysis report Branch colinaaa:codex/fix-react-transfo... Project dashboard
Generated by RelativeCI Documentation Report issue
React Example#7627 Bundle Size — 225.31KiB (0%).50dcd1f(current) vs 989f345 main#7619(baseline) Bundle metrics
|
| Current #7627 |
Baseline #7619 |
|
|---|---|---|
0B |
0B |
|
0B |
0B |
|
0% |
0% |
|
0 |
0 |
|
4 |
4 |
|
179 |
179 |
|
69 |
69 |
|
44.57% |
44.57% |
|
2 |
2 |
|
0 |
0 |
Bundle size by type no changes
| Current #7627 |
Baseline #7619 |
|
|---|---|---|
145.76KiB |
145.76KiB |
|
79.55KiB |
79.55KiB |
Bundle analysis report Branch colinaaa:codex/fix-react-transfo... Project dashboard
Generated by RelativeCI Documentation Report issue
d38dfa6 to
50dcd1f
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/react/transform/crates/swc_plugin_snapshot/lib.rs (1)
1730-1738: Assertions are string-fragile – consider an__swc_snapshots__based test.The positive assertion relies on swc's codegen emitting
(item, index123, array123)=>verbatim (no space around=>), and the negative assertion matches the literal slot-prop form$0: array123[index123]. Both are tight couplings to the current printer output and to thereact::reactlowering shape; a future swc upgrade tweaking arrow-fn formatting or prop ordering could make this test fail without a real regression.Given that the rest of this module uses the
test!macro backed by__swc_snapshots__, converting this regression case to a snapshot test (and checking the snapshot for absence ofarray123/index123references in the createSnapshot dynamic parts) would be more robust. Optional – keep as-is if you prefer the explicit behavioral asserts.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react/transform/crates/swc_plugin_snapshot/lib.rs` around lines 1730 - 1738, Replace the fragile string assertions around the generated code (the two assert! checks that inspect output for the exact "(item, index123, array123)=>" and for the literal "$0: array123[index123]") with a snapshot-based test using the existing test! / __swc_snapshots__ harness used elsewhere in this module: produce a snapshot of the full transformed output (the same value currently in output) and then in the snapshot assertion verify the snapshot contents do not contain references to the map-local names in createSnapshot dynamic parts (i.e., ensure no occurrences of "array123" or "index123" leak outside the map callback), keeping the intent but removing dependency on exact printer spacing or prop ordering; update the test that builds output (the variable named output and the surrounding test case) to use test! / __swc_snapshots__ instead of the two string assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/react/transform/crates/swc_plugin_snapshot/lib.rs`:
- Around line 1730-1738: Replace the fragile string assertions around the
generated code (the two assert! checks that inspect output for the exact "(item,
index123, array123)=>" and for the literal "$0: array123[index123]") with a
snapshot-based test using the existing test! / __swc_snapshots__ harness used
elsewhere in this module: produce a snapshot of the full transformed output (the
same value currently in output) and then in the snapshot assertion verify the
snapshot contents do not contain references to the map-local names in
createSnapshot dynamic parts (i.e., ensure no occurrences of "array123" or
"index123" leak outside the map callback), keeping the intent but removing
dependency on exact printer spacing or prop ordering; update the test that
builds output (the variable named output and the surrounding test case) to use
test! / __swc_snapshots__ instead of the two string assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 8920fe8d-a395-4d83-9470-02f33a5b91be
📒 Files selected for processing (3)
.changeset/fix-react-children-map-scope.md.github/react-transform.instructions.mdpackages/react/transform/crates/swc_plugin_snapshot/lib.rs
Summary
children={array.map(...)}so JSX inside the prop expression stays inside the map callback scope.github/react-transform.instructions.mdRoot cause
DynamicPartExtractormanually extracted native JSX opening attributes, then calledn.visit_mut_children_with(...). That SWC traversal also revisited opening attrs, so JSX nested inside a prop expression such aschildren={items.map(...)}could be extracted into a parent snapshot slot. The extracted slot expression then referenced map callback locals likearray123andindex123outside their lexical scope.Fixes #2523
Validation
pnpm turbo build --filter @lynx-js/react-transformcargo fmt -p swc_plugin_snapshot -- --checkpnpm dprint check .github/react-transform.instructions.md packages/react/transform/crates/swc_plugin_snapshot/lib.rscargo test -p swc_plugin_snapshot -- --nocapturecargo test -p react-transformSummary by CodeRabbit
childrenprop callbacks—particularly with array methods likemap()—were incorrectly handling variable scope. This patch ensures proper variable references during code generation.