Skip to content

Commit 4056ce6

Browse files
committed
Preserve derive callback destructuring and refresh fixtures
1 parent 67f8c76 commit 4056ce6

28 files changed

+175
-170
lines changed

packages/ts-transformers/src/transformers/builtins/derive.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,23 @@ function createParameterForPlan(
140140
);
141141
}
142142

143-
if (bindings.length === 1) {
144-
const sole = bindings[0]!;
145-
if (!sole.propertyName && !sole.dotDotDotToken && !sole.initializer) {
146-
return factory.createParameterDeclaration(
147-
undefined,
148-
undefined,
149-
sole.name,
150-
undefined,
151-
undefined,
152-
undefined,
153-
);
154-
}
143+
const shouldInlineSoleBinding =
144+
bindings.length === 1 &&
145+
captureTree.size === 0 &&
146+
fallbackEntries.length === 1 &&
147+
!bindings[0]!.propertyName &&
148+
!bindings[0]!.dotDotDotToken &&
149+
!bindings[0]!.initializer;
150+
151+
if (shouldInlineSoleBinding) {
152+
return factory.createParameterDeclaration(
153+
undefined,
154+
undefined,
155+
bindings[0]!.name,
156+
undefined,
157+
undefined,
158+
undefined,
159+
);
155160
}
156161

157162
return factory.createParameterDeclaration(

packages/ts-transformers/test/fixtures/ast-transform/builder-conditional.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default recipe({
2121
return {
2222
[NAME]: state.label,
2323
[UI]: (<section>
24-
{__ctHelpers.ifElse(__ctHelpers.derive(state, state => state && state.count > 0), <p>Positive</p>, <p>Non-positive</p>)}
24+
{__ctHelpers.ifElse(__ctHelpers.derive(state, ({ state }) => state && state.count > 0), <p>Positive</p>, <p>Non-positive</p>)}
2525
</section>),
2626
};
2727
});

packages/ts-transformers/test/fixtures/ast-transform/counter-recipe.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default recipe({
4949
<ul>
5050
<li>next number: {__ctHelpers.ifElse(state.value, __ctHelpers.derive({ state: {
5151
value: state.value
52-
} }, state => state.value + 1), "unknown")}</li>
52+
} }, ({ state }) => state.value + 1), "unknown")}</li>
5353
</ul>
5454
<ct-button onClick={increment({ value: state.value })}>+</ct-button>
5555
</div>),

packages/ts-transformers/test/fixtures/ast-transform/event-handler-no-derive.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default recipe({
3232
return {
3333
[UI]: (<div>
3434
{/* Regular JSX expression - should be wrapped in derive */}
35-
<span>Count: {__ctHelpers.derive(count, count => count + 1)}</span>
35+
<span>Count: {__ctHelpers.derive(count, ({ count }) => count + 1)}</span>
3636

3737
{/* Event handler with OpaqueRef - should NOT be wrapped in derive */}
3838
<ct-button onClick={handleClick({ count })}>

packages/ts-transformers/test/fixtures/ast-transform/ternary_derive.expected.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default recipe({
1818
[UI]: (<div>
1919
{__ctHelpers.ifElse(__ctHelpers.derive({ state: {
2020
value: state.value
21-
} }, state => state.value + 1), __ctHelpers.derive({ state: {
21+
} }, ({ state }) => state.value + 1), __ctHelpers.derive({ state: {
2222
value: state.value
23-
} }, state => state.value + 2), "undefined")}
23+
} }, ({ state }) => state.value + 2), "undefined")}
2424
</div>),
2525
};
2626
});

packages/ts-transformers/test/fixtures/closures/filter-map-chain.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default recipe({
4747
{/* Method chain: filter then map, both with captures */}
4848
{__ctHelpers.derive({ state: {
4949
items: state.items
50-
} }, state => state.items
50+
} }, ({ state }) => state.items
5151
.filter((item) => item.active)).mapWithPattern(__ctHelpers.recipe({
5252
$schema: "https://json-schema.org/draft/2020-12/schema",
5353
type: "object",

packages/ts-transformers/test/fixtures/closures/map-computed-alias-strict.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default recipe({
6666
}
6767
} as const satisfies __ctHelpers.JSONSchema, ({ element: { [dynamicKey]: val }, params: {} }) => {
6868
"use strict";
69-
return <span key={val}>{__ctHelpers.derive(val, val => val * 2)}</span>;
69+
return <span key={val}>{__ctHelpers.derive(val, ({ val }) => val * 2)}</span>;
7070
}), {})}
7171
</div>),
7272
};

packages/ts-transformers/test/fixtures/closures/map-import-reference.expected.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default recipe({
7373
} as const satisfies __ctHelpers.JSONSchema, ({ element: item, params: {} }) => (<div>
7474
Item: {__ctHelpers.derive({ item: {
7575
price: item.price
76-
} }, item => formatPrice(item.price * (1 + TAX_RATE)))}
76+
} }, ({ item }) => formatPrice(item.price * (1 + TAX_RATE)))}
7777
</div>)), {})}
7878
</div>),
7979
};

packages/ts-transformers/test/fixtures/jsx-expressions/element-access-complex.expected.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export default recipe({
115115
matrix: state.matrix,
116116
row: state.row,
117117
col: state.col
118-
} }, state => state.matrix[state.row][state.col])}</p>
118+
} }, ({ state }) => state.matrix[state.row][state.col])}</p>
119119

120120
{/* Triple nested access */}
121121
<p>Deep nested: {__ctHelpers.derive({ state: {
@@ -124,41 +124,41 @@ export default recipe({
124124
index: state.nested.index
125125
},
126126
row: state.row
127-
} }, state => state.nested.arrays[state.nested.index][state.row])}</p>
127+
} }, ({ state }) => state.nested.arrays[state.nested.index][state.row])}</p>
128128

129129
<h3>Multiple References to Same Array</h3>
130130
{/* Same array accessed multiple times with different indices */}
131131
<p>
132132
First and last: {state.items[0]} and{" "}
133133
{__ctHelpers.derive({ state: {
134134
items: state.items
135-
} }, state => state.items[state.items.length - 1])}
135+
} }, ({ state }) => state.items[state.items.length - 1])}
136136
</p>
137137

138138
{/* Array used in computation and access */}
139139
<p>Sum of ends: {__ctHelpers.derive({ state: {
140140
arr: state.arr
141-
} }, state => state.arr[0] + state.arr[state.arr.length - 1])}</p>
141+
} }, ({ state }) => state.arr[0] + state.arr[state.arr.length - 1])}</p>
142142

143143
<h3>Computed Indices</h3>
144144
{/* Index from multiple state values */}
145145
<p>Computed index: {__ctHelpers.derive({ state: {
146146
arr: state.arr,
147147
a: state.a,
148148
b: state.b
149-
} }, state => state.arr[state.a + state.b])}</p>
149+
} }, ({ state }) => state.arr[state.a + state.b])}</p>
150150

151151
{/* Index from computation involving array */}
152152
<p>Modulo index: {__ctHelpers.derive({ state: {
153153
items: state.items,
154154
row: state.row
155-
} }, state => state.items[state.row % state.items.length])}</p>
155+
} }, ({ state }) => state.items[state.row % state.items.length])}</p>
156156

157157
{/* Complex index expression */}
158158
<p>Complex: {__ctHelpers.derive({ state: {
159159
arr: state.arr,
160160
a: state.a
161-
} }, state => state.arr[Math.min(state.a * 2, state.arr.length - 1)])}</p>
161+
} }, ({ state }) => state.arr[Math.min(state.a * 2, state.arr.length - 1)])}</p>
162162

163163
<h3>Chained Element Access</h3>
164164
{/* Element access returning array, then accessing that */}
@@ -168,19 +168,19 @@ export default recipe({
168168
users: state.users,
169169
selectedUser: state.selectedUser,
170170
selectedScore: state.selectedScore
171-
} }, state => state.users[state.selectedUser].scores[state.selectedScore])}
171+
} }, ({ state }) => state.users[state.selectedUser].scores[state.selectedScore])}
172172
</p>
173173

174174
{/* Using one array element as index for another */}
175175
<p>Indirect: {__ctHelpers.derive({ state: {
176176
items: state.items,
177177
indices: state.indices
178-
} }, state => state.items[state.indices[0]])}</p>
178+
} }, ({ state }) => state.items[state.indices[0]])}</p>
179179

180180
{/* Array element used as index for same array */}
181181
<p>Self reference: {__ctHelpers.derive({ state: {
182182
arr: state.arr
183-
} }, state => state.arr[state.arr[0]])}</p>
183+
} }, ({ state }) => state.arr[state.arr[0]])}</p>
184184

185185
<h3>Mixed Property and Element Access</h3>
186186
{/* Property access followed by element access with computed index */}
@@ -189,13 +189,13 @@ export default recipe({
189189
arrays: state.nested.arrays,
190190
index: state.nested.index
191191
}
192-
} }, state => state.nested.arrays[state.nested.index].length)}</p>
192+
} }, ({ state }) => state.nested.arrays[state.nested.index].length)}</p>
193193

194194
{/* Element access followed by property access */}
195195
<p>User name length: {__ctHelpers.derive({ state: {
196196
users: state.users,
197197
selectedUser: state.selectedUser
198-
} }, state => state.users[state.selectedUser].name.length)}</p>
198+
} }, ({ state }) => state.users[state.selectedUser].name.length)}</p>
199199

200200
<h3>Element Access in Conditions</h3>
201201
{/* Element access in ternary */}
@@ -204,10 +204,10 @@ export default recipe({
204204
{__ctHelpers.ifElse(__ctHelpers.derive({ state: {
205205
arr: state.arr,
206206
a: state.a
207-
} }, state => state.arr[state.a] > 10), __ctHelpers.derive({ state: {
207+
} }, ({ state }) => state.arr[state.a] > 10), __ctHelpers.derive({ state: {
208208
items: state.items,
209209
b: state.b
210-
} }, state => state.items[state.b]), state.items[0])}
210+
} }, ({ state }) => state.items[state.b]), state.items[0])}
211211
</p>
212212

213213
{/* Element access in boolean expression */}
@@ -216,7 +216,7 @@ export default recipe({
216216
matrix: state.matrix,
217217
row: state.row,
218218
col: state.col
219-
} }, state => state.matrix[state.row][state.col] > 0), "positive", "non-positive")}
219+
} }, ({ state }) => state.matrix[state.row][state.col] > 0), "positive", "non-positive")}
220220
</p>
221221

222222
<h3>Element Access with Operators</h3>
@@ -225,18 +225,18 @@ export default recipe({
225225
arr: state.arr,
226226
a: state.a,
227227
b: state.b
228-
} }, state => state.arr[state.a] * state.arr[state.b])}</p>
228+
} }, ({ state }) => state.arr[state.a] * state.arr[state.b])}</p>
229229

230230
{/* Element access with string concatenation */}
231231
<p>Concat: {__ctHelpers.derive({ state: {
232232
items: state.items,
233233
indices: state.indices
234-
} }, state => state.items[0] + " - " + state.items[state.indices[0]])}</p>
234+
} }, ({ state }) => state.items[0] + " - " + state.items[state.indices[0]])}</p>
235235

236236
{/* Multiple element accesses in single expression */}
237237
<p>Sum: {__ctHelpers.derive({ state: {
238238
arr: state.arr
239-
} }, state => state.arr[0] + state.arr[1] + state.arr[2])}</p>
239+
} }, ({ state }) => state.arr[0] + state.arr[1] + state.arr[2])}</p>
240240
</div>),
241241
};
242242
});

packages/ts-transformers/test/fixtures/jsx-expressions/element-access-simple.expected.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ export default recipe({
4444
<p>Item: {__ctHelpers.derive({ state: {
4545
items: state.items,
4646
index: state.index
47-
} }, state => state.items[state.index])}</p>
47+
} }, ({ state }) => state.items[state.index])}</p>
4848

4949
{/* Computed index */}
5050
<p>Last: {__ctHelpers.derive({ state: {
5151
items: state.items
52-
} }, state => state.items[state.items.length - 1])}</p>
52+
} }, ({ state }) => state.items[state.items.length - 1])}</p>
5353

5454
{/* Double indexing */}
5555
<p>Matrix: {__ctHelpers.derive({ state: {
5656
matrix: state.matrix,
5757
row: state.row,
5858
col: state.col
59-
} }, state => state.matrix[state.row][state.col])}</p>
59+
} }, ({ state }) => state.matrix[state.row][state.col])}</p>
6060
</div>),
6161
};
6262
});

0 commit comments

Comments
 (0)