@@ -201,40 +201,43 @@ test('should not rewrite scope variable', () => {
201
201
202
202
test ( 'object destructure' , ( ) => {
203
203
const { code, rootRefs } = transform ( `
204
- let n = $ref(1), { a, b: c, d = 1, e: f = 2, ...g } = $(useFoo())
204
+ let n = $ref(1), { a, b: c, d = 1, e: f = 2, [g]: h } = $(useFoo())
205
205
let { foo } = $(useSomthing(() => 1));
206
- console.log(n, a, c, d, f, g , foo)
206
+ console.log(n, a, c, d, f, h , foo)
207
207
` )
208
+ expect ( code ) . toMatch ( `a = _toRef(__$temp_1, 'a')` )
209
+ expect ( code ) . toMatch ( `c = _toRef(__$temp_1, 'b')` )
210
+ expect ( code ) . toMatch ( `d = _toRef(__$temp_1, 'd', 1)` )
211
+ expect ( code ) . toMatch ( `f = _toRef(__$temp_1, 'e', 2)` )
212
+ expect ( code ) . toMatch ( `h = _toRef(__$temp_1, g)` )
213
+ expect ( code ) . toMatch ( `foo = _toRef(__$temp_2, 'foo')` )
208
214
expect ( code ) . toMatch (
209
- `let n = _ref(1), { a: __a, b: __c , d: __d = 1, e: __f = 2, ...__g } = (useFoo() )`
215
+ `console.log(n.value, a.value, c.value , d.value, f.value, h.value, foo.value )`
210
216
)
211
- expect ( code ) . toMatch ( `let { foo: __foo } = (useSomthing(() => 1))` )
212
- expect ( code ) . toMatch ( `\nconst a = _shallowRef(__a);` )
213
- expect ( code ) . not . toMatch ( `\nconst b = _shallowRef(__b);` )
214
- expect ( code ) . toMatch ( `\nconst c = _shallowRef(__c);` )
215
- expect ( code ) . toMatch ( `\nconst d = _shallowRef(__d);` )
216
- expect ( code ) . not . toMatch ( `\nconst e = _shallowRef(__e);` )
217
- expect ( code ) . toMatch ( `\nconst f = _shallowRef(__f);` )
218
- expect ( code ) . toMatch ( `\nconst g = _shallowRef(__g);` )
219
- expect ( code ) . toMatch ( `\nconst foo = _shallowRef(__foo);` )
220
- expect ( code ) . toMatch (
221
- `console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)`
222
- )
223
- expect ( rootRefs ) . toStrictEqual ( [ 'n' , 'a' , 'c' , 'd' , 'f' , 'g' , 'foo' ] )
217
+ expect ( rootRefs ) . toStrictEqual ( [ 'n' , 'a' , 'c' , 'd' , 'f' , 'h' , 'foo' ] )
218
+ assertCode ( code )
219
+ } )
220
+
221
+ test ( 'object destructure w/ mid-path default values' , ( ) => {
222
+ const { code, rootRefs } = transform ( `
223
+ const { a: { b } = { b: 123 }} = $(useFoo())
224
+ console.log(b)
225
+ ` )
226
+ expect ( code ) . toMatch ( `b = _toRef((__$temp_1.a || { b: 123 }), 'b')` )
227
+ expect ( code ) . toMatch ( `console.log(b.value)` )
228
+ expect ( rootRefs ) . toStrictEqual ( [ 'b' ] )
224
229
assertCode ( code )
225
230
} )
226
231
227
232
test ( 'array destructure' , ( ) => {
228
233
const { code, rootRefs } = transform ( `
229
- let n = $ref(1), [a, b = 1, ...c ] = $(useFoo())
230
- console.log(n, a, b, c )
234
+ let n = $ref(1), [a, b = 1] = $(useFoo())
235
+ console.log(n, a, b)
231
236
` )
232
- expect ( code ) . toMatch ( `let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())` )
233
- expect ( code ) . toMatch ( `\nconst a = _shallowRef(__a);` )
234
- expect ( code ) . toMatch ( `\nconst b = _shallowRef(__b);` )
235
- expect ( code ) . toMatch ( `\nconst c = _shallowRef(__c);` )
236
- expect ( code ) . toMatch ( `console.log(n.value, a.value, b.value, c.value)` )
237
- expect ( rootRefs ) . toStrictEqual ( [ 'n' , 'a' , 'b' , 'c' ] )
237
+ expect ( code ) . toMatch ( `a = _toRef(__$temp_1, 0)` )
238
+ expect ( code ) . toMatch ( `b = _toRef(__$temp_1, 1, 1)` )
239
+ expect ( code ) . toMatch ( `console.log(n.value, a.value, b.value)` )
240
+ expect ( rootRefs ) . toStrictEqual ( [ 'n' , 'a' , 'b' ] )
238
241
assertCode ( code )
239
242
} )
240
243
@@ -244,13 +247,9 @@ test('nested destructure', () => {
244
247
let { c: [d, e] } = $(useBar())
245
248
console.log(b, d, e)
246
249
` )
247
- expect ( code ) . toMatch ( `let [{ a: { b: __b }}] = (useFoo())` )
248
- expect ( code ) . toMatch ( `let { c: [__d, __e] } = (useBar())` )
249
- expect ( code ) . not . toMatch ( `\nconst a = _shallowRef(__a);` )
250
- expect ( code ) . not . toMatch ( `\nconst c = _shallowRef(__c);` )
251
- expect ( code ) . toMatch ( `\nconst b = _shallowRef(__b);` )
252
- expect ( code ) . toMatch ( `\nconst d = _shallowRef(__d);` )
253
- expect ( code ) . toMatch ( `\nconst e = _shallowRef(__e);` )
250
+ expect ( code ) . toMatch ( `b = _toRef(__$temp_1[0].a, 'b')` )
251
+ expect ( code ) . toMatch ( `d = _toRef(__$temp_2.c, 0)` )
252
+ expect ( code ) . toMatch ( `e = _toRef(__$temp_2.c, 1)` )
254
253
expect ( rootRefs ) . toStrictEqual ( [ 'b' , 'd' , 'e' ] )
255
254
assertCode ( code )
256
255
} )
@@ -396,4 +395,13 @@ describe('errors', () => {
396
395
` )
397
396
expect ( code ) . not . toMatch ( '.value' )
398
397
} )
398
+
399
+ test ( 'rest element in $() destructure' , ( ) => {
400
+ expect ( ( ) => transform ( `let { a, ...b } = $(foo())` ) ) . toThrow (
401
+ `does not support rest element`
402
+ )
403
+ expect ( ( ) => transform ( `let [a, ...b] = $(foo())` ) ) . toThrow (
404
+ `does not support rest element`
405
+ )
406
+ } )
399
407
} )
0 commit comments