Skip to content

Commit 8162b6a

Browse files
committed
fix(reactivity-transform): unwrap wrapper node
1 parent d8f131a commit 8162b6a

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,14 @@ const props = defineProps<{msg: string; ids?: string[]}>()
281281
`;
282282
283283
exports[`should unwrap TS node 1`] = `
284-
"import { ref as _ref } from 'vue'
284+
"import { ref as _ref, toRef as _toRef } from 'vue'
285285
286286
const bar = (ref(1))! as number
287287
const baz = (bar)! as Ref<number>
288288
const qux = (<number>_ref(10)!)
289+
const __$temp_1 = ({ a: 'a', b: 'b' })! as any,
290+
a = _toRef(__$temp_1, 'a'),
291+
b = _toRef(__$temp_1, 'b');
289292
"
290293
`;
291294

packages/reactivity-transform/__tests__/reactivityTransform.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ test('should unwrap TS node', () => {
495495
const bar = $(ref(1))! as number
496496
const baz = $$(bar)! as Ref<number>
497497
const qux = (<number>$ref(10)!)
498+
const { a, b } = $({ a: 'a', b: 'b' })! as any
498499
`,
499500
{ filename: 'foo.ts' }
500501
)

packages/reactivity-transform/src/reactivityTransform.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,13 @@ export function transformAST(
312312
init.type === 'CallExpression' &&
313313
init.callee.type === 'Identifier'
314314
if (isCall && (refCall = isRefCreationCall((init.callee as any).name))) {
315-
processRefDeclaration(refCall, decl.id, init, stmt.kind === 'const')
315+
processRefDeclaration(
316+
refCall,
317+
decl.id,
318+
decl.init!,
319+
init,
320+
stmt.kind === 'const'
321+
)
316322
} else {
317323
const isProps =
318324
isRoot && isCall && (init.callee as Identifier).name === 'defineProps'
@@ -332,6 +338,7 @@ export function transformAST(
332338
function processRefDeclaration(
333339
method: string,
334340
id: VariableDeclarator['id'],
341+
init: Node,
335342
call: CallExpression,
336343
isConst: boolean
337344
) {
@@ -344,9 +351,9 @@ export function transformAST(
344351
// single variable
345352
registerRefBinding(id, isConst)
346353
} else if (id.type === 'ObjectPattern') {
347-
processRefObjectPattern(id, call, isConst)
354+
processRefObjectPattern(id, init, isConst)
348355
} else if (id.type === 'ArrayPattern') {
349-
processRefArrayPattern(id, call, isConst)
356+
processRefArrayPattern(id, init, isConst)
350357
}
351358
} else {
352359
// shorthands
@@ -366,7 +373,7 @@ export function transformAST(
366373

367374
function processRefObjectPattern(
368375
pattern: ObjectPattern,
369-
call: CallExpression,
376+
value: Node,
370377
isConst: boolean,
371378
tempVar?: string,
372379
path: PathSegment[] = []
@@ -402,12 +409,12 @@ export function transformAST(
402409
// { foo: bar }
403410
nameId = p.value
404411
} else if (p.value.type === 'ObjectPattern') {
405-
processRefObjectPattern(p.value, call, isConst, tempVar, [
412+
processRefObjectPattern(p.value, value, isConst, tempVar, [
406413
...path,
407414
key
408415
])
409416
} else if (p.value.type === 'ArrayPattern') {
410-
processRefArrayPattern(p.value, call, isConst, tempVar, [
417+
processRefArrayPattern(p.value, value, isConst, tempVar, [
411418
...path,
412419
key
413420
])
@@ -417,12 +424,12 @@ export function transformAST(
417424
nameId = p.value.left
418425
defaultValue = p.value.right
419426
} else if (p.value.left.type === 'ObjectPattern') {
420-
processRefObjectPattern(p.value.left, call, isConst, tempVar, [
427+
processRefObjectPattern(p.value.left, value, isConst, tempVar, [
421428
...path,
422429
[key, p.value.right]
423430
])
424431
} else if (p.value.left.type === 'ArrayPattern') {
425-
processRefArrayPattern(p.value.left, call, isConst, tempVar, [
432+
processRefArrayPattern(p.value.left, value, isConst, tempVar, [
426433
...path,
427434
[key, p.value.right]
428435
])
@@ -446,21 +453,21 @@ export function transformAST(
446453
: `'${nameId.name}'`
447454
const defaultStr = defaultValue ? `, ${snip(defaultValue)}` : ``
448455
s.appendLeft(
449-
call.end! + offset,
456+
value.end! + offset,
450457
`,\n ${nameId.name} = ${helper(
451458
'toRef'
452459
)}(${source}, ${keyStr}${defaultStr})`
453460
)
454461
}
455462
}
456463
if (nameId) {
457-
s.appendLeft(call.end! + offset, ';')
464+
s.appendLeft(value.end! + offset, ';')
458465
}
459466
}
460467

461468
function processRefArrayPattern(
462469
pattern: ArrayPattern,
463-
call: CallExpression,
470+
value: Node,
464471
isConst: boolean,
465472
tempVar?: string,
466473
path: PathSegment[] = []
@@ -487,25 +494,25 @@ export function transformAST(
487494
// [...a]
488495
error(`reactivity destructure does not support rest elements.`, e)
489496
} else if (e.type === 'ObjectPattern') {
490-
processRefObjectPattern(e, call, isConst, tempVar, [...path, i])
497+
processRefObjectPattern(e, value, isConst, tempVar, [...path, i])
491498
} else if (e.type === 'ArrayPattern') {
492-
processRefArrayPattern(e, call, isConst, tempVar, [...path, i])
499+
processRefArrayPattern(e, value, isConst, tempVar, [...path, i])
493500
}
494501
if (nameId) {
495502
registerRefBinding(nameId, isConst)
496503
// inject toRef() after original replaced pattern
497504
const source = pathToString(tempVar, path)
498505
const defaultStr = defaultValue ? `, ${snip(defaultValue)}` : ``
499506
s.appendLeft(
500-
call.end! + offset,
507+
value.end! + offset,
501508
`,\n ${nameId.name} = ${helper(
502509
'toRef'
503510
)}(${source}, ${i}${defaultStr})`
504511
)
505512
}
506513
}
507514
if (nameId) {
508-
s.appendLeft(call.end! + offset, ';')
515+
s.appendLeft(value.end! + offset, ';')
509516
}
510517
}
511518

0 commit comments

Comments
 (0)