Commit 36d60c5
Fix: Prevent map transformation when inside derive and exclude untransformed map parameters (commontoolsinc#1960)
* Fix: Skip map transformation when map will be inside derive
When a map is nested in an expression with opaque refs (e.g.,
`list.length > 0 && list.map(...)`), the OpaqueRefJSXTransformer wraps
the entire expression in `derive(list, list => ...)`, which unwraps the
array to a plain array. If ClosureTransformer had already transformed
the map to `mapWithPattern`, this causes runtime errors since plain
arrays don't have `mapWithPattern`.
This fix adds `shouldTransformMap()` which uses dataflow analysis to
detect when a map will be inside a derive and skips the transformation,
keeping it as plain `.map()` which works correctly on unwrapped arrays.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix: Exclude untransformed map callback parameters from outer derive capture
When a plain .map() call is inside an expression that gets wrapped in derive(),
the map callback parameters should not be treated as opaque parameters for the
purposes of the outer derive. This is because:
1. The map is NOT being transformed to mapWithPattern (stays as plain .map)
2. The callback runs on unwrapped array elements inside the derive
3. The callback parameters are local to that scope, not outer scope variables
This fix modifies getOpaqueCallKindForParameter() to check if a callback was
actually transformed (using context.isMapCallback) before treating its parameters
as opaque. Untransformed callbacks have regular parameters that should not be
captured in outer derives.
Example that's now fixed:
```tsx
{items.map((item) => <div>{item.name && <span>{item.name}</span>}</div>)}
```
Before: derive({ items, item_name: item.name }, ...) ❌
After: derive({ items }, ...) ✅
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>1 parent b587a4a commit 36d60c5
File tree
7 files changed
+134
-79
lines changed- packages/ts-transformers
- src
- closures
- transformers/opaque-ref
- test/fixtures/jsx-expressions
7 files changed
+134
-79
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
715 | 715 | | |
716 | 716 | | |
717 | 717 | | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
718 | 784 | | |
719 | 785 | | |
720 | 786 | | |
| |||
734 | 800 | | |
735 | 801 | | |
736 | 802 | | |
737 | | - | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
738 | 808 | | |
739 | 809 | | |
740 | 810 | | |
| |||
Lines changed: 18 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | | - | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| 74 | + | |
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
87 | | - | |
88 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
89 | 101 | | |
90 | 102 | | |
91 | 103 | | |
| |||
156 | 168 | | |
157 | 169 | | |
158 | 170 | | |
| 171 | + | |
159 | 172 | | |
160 | 173 | | |
161 | 174 | | |
| |||
195 | 208 | | |
196 | 209 | | |
197 | 210 | | |
| 211 | + | |
198 | 212 | | |
199 | 213 | | |
200 | 214 | | |
| |||
216 | 230 | | |
217 | 231 | | |
218 | 232 | | |
| 233 | + | |
219 | 234 | | |
220 | 235 | | |
221 | 236 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 3 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
36 | 12 | | |
37 | 13 | | |
38 | 14 | | |
| |||
Lines changed: 1 addition & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 11 | + | |
39 | 12 | | |
40 | 13 | | |
41 | 14 | | |
| |||
Lines changed: 4 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 104 | + | |
| 105 | + | |
120 | 106 | | |
121 | 107 | | |
122 | | - | |
123 | | - | |
| 108 | + | |
| 109 | + | |
124 | 110 | | |
125 | 111 | | |
126 | 112 | | |
| |||
134 | 120 | | |
135 | 121 | | |
136 | 122 | | |
137 | | - | |
0 commit comments