Skip to content

Commit 572e3ee

Browse files
committed
fix(reactivity-transform): more edge cases of $$ scenario
1 parent b732a4f commit 572e3ee

File tree

3 files changed

+82
-55
lines changed

3 files changed

+82
-55
lines changed

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ exports[`$$ 1`] = `
2727
exports[`$$ with some edge cases 1`] = `
2828
"import { ref as _ref } from 'vue'
2929
30-
let a = _ref(1), e = _ref()
31-
()
32-
;(count++,count)
33-
((count++,count))
34-
count = (count++,count)
35-
count = ()=>(count++,count)
36-
const af=()=>((count++,count))
37-
let c = _ref(a.value, (count++,count))
38-
let r1 = (count++,count)
39-
let r2 = _ref(af((count++,count)))
40-
let r3 = { a:(count++,count) }
30+
a
31+
console.log(a)
32+
;(a,b)
33+
;(a,b)
34+
count = (a++,b)
35+
count = ()=>(a++,b)
36+
let r1 = _ref(a, (a++,b))
37+
let r2 = { a:(a++,b),b:a }
38+
switch(c){
39+
case d:
40+
a
41+
;(h,f)
42+
break
43+
}
44+
((count++,count,(count,a)))
4145
"
4246
`;
4347

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

+24-19
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,31 @@ test('$$', () => {
307307

308308
test('$$ with some edge cases',()=>{
309309
const { code } = transform(`
310-
let a = $ref(1), e = $ref()
311-
$$()
312-
$$(count++,count)
313-
($$(count++,count))
314-
count = $$(count++,count)
315-
count = ()=>$$(count++,count)
316-
const af=()=>($$(count++,count))
317-
let c = $ref(a, $$(count++,count))
318-
let r1 = $$(count++,count)
319-
let r2 = $ref(af($$(count++,count)))
320-
let r3 = { a:$$(count++,count) }
310+
$$(a)
311+
console.log($$($$(a)))
312+
$$(a,b)
313+
$$($$((a,b)))
314+
count = $$(a++,b)
315+
count = ()=>$$(a++,b)
316+
let r1 = $ref(a, $$(a++,b))
317+
let r2 = { a:$$(a++,b),b:$$(a) }
318+
switch(c){
319+
case d:
320+
$$(a)
321+
$$($$(h,f))
322+
break
323+
}
324+
($$(count++,$$(count),$$(count,a)))
321325
`)
322-
expect(code).toMatch(`;(count++,count)`)
323-
expect(code).toMatch(`((count++,count))`)
324-
expect(code).toMatch(`count = (count++,count)`)
325-
expect(code).toMatch(`()=>(count++,count)`)
326-
expect(code).toMatch(`()=>((count++,count))`)
327-
expect(code).toMatch(`let r1 = (count++,count)`)
328-
expect(code).toMatch(`let r2 = _ref(af((count++,count)))`)
329-
expect(code).toMatch(`let r3 = { a:(count++,count) }`)
326+
expect(code).toMatch(`((count++,count,(count,a)))`)
327+
expect(code).toMatch(`;(a,b)`)
328+
expect(code).toMatch(`log(a)`)
329+
expect(code).toMatch(`count = (a++,b)`)
330+
expect(code).toMatch(`()=>(a++,b)`)
331+
expect(code).toMatch(`_ref(a, (a++,b))`)
332+
expect(code).toMatch(`{ a:(a++,b),b:a }`)
333+
expect(code).toMatch(`switch(c)`)
334+
expect(code).toMatch(`;(h,f)`)
330335
assertCode(code)
331336
})
332337

packages/reactivity-transform/src/reactivityTransform.ts

+43-25
Original file line numberDiff line numberDiff line change
@@ -555,35 +555,53 @@ export function transformAST(
555555
*/
556556
function unwrapMacro(node: CallExpression) {
557557
const argsLength = node.arguments.length
558-
s.remove(node.callee.start! + offset, node.callee.end! + offset)
559-
if (!argsLength) {
558+
const bracketStart = node.callee.end! + offset
559+
560+
s.remove(node.callee.start! + offset, bracketStart)
561+
562+
if (argsLength === 1) {
563+
const bracketEnd = node.arguments[argsLength - 1].end! + offset
564+
565+
// remove brackets of macros
566+
s.remove(bracketStart, bracketStart + 1)
567+
s.remove(bracketEnd, bracketEnd + 1)
568+
569+
// avoid traversal of like `$$(a)`
570+
if(node.arguments[0].type!=="CallExpression"){
571+
return;
572+
}
573+
}
574+
575+
// avoid invalid traversal for $
576+
if (!escapeScope|| node.extra) {
560577
return
561-
} else if (argsLength === 1) {
562-
// remove brackets of macro
563-
s.remove(node.callee.end! + offset, node.arguments[0].start! + offset)
564-
s.remove(node.arguments[argsLength - 1].end! + offset, node.end! + offset)
565-
} else {
566-
// handle some edge cases for macro $$
567-
// resolve nested
568-
let i = parentStack.length - 1
569-
while (i >= 0) {
570-
const curParent = parentStack[i--]
571-
// see @__tests__:$$ with some edge cases
572-
if (
573-
curParent.type === 'VariableDeclarator' ||
574-
curParent.type === 'AssignmentExpression' ||
575-
(curParent.type === 'ExpressionStatement' &&
576-
(curParent.expression.extra ||
577-
(curParent.expression.type === 'CallExpression' &&
578-
curParent.expression.arguments.includes(node))))
579-
) {
580-
return
581-
}
578+
}
579+
580+
// fix some edge cases for macro $$, eg. $$(a,b)
581+
// resolve nested
582+
let i = parentStack.length - 1
583+
while (i >= 0) {
584+
const curParent = parentStack[i--]
585+
if (
586+
curParent.type === 'ExpressionStatement' &&
587+
isIsolateExpression(curParent.expression, node)
588+
) {
589+
// split the unwrapped code `()()` to `();()`
590+
s.appendLeft(node.callee.start! + offset, ';')
591+
return
582592
}
583-
s.appendLeft(node.callee.start! + offset, ';')
584593
}
585594
}
586595

596+
function isIsolateExpression(expression: Expression, node: CallExpression) {
597+
return (
598+
expression.type === 'CallExpression' &&
599+
expression.callee.type === 'Identifier' &&
600+
expression.callee.name === escapeSymbol &&
601+
!expression.arguments.includes(node)
602+
)
603+
}
604+
587605
// check root scope first
588606
walkScope(ast, true)
589607
;(walk as any)(ast, {
@@ -657,8 +675,8 @@ export function transformAST(
657675
}
658676

659677
if (callee === escapeSymbol) {
660-
unwrapMacro(node)
661678
escapeScope = node
679+
unwrapMacro(node)
662680
}
663681

664682
// TODO remove when out of experimental

0 commit comments

Comments
 (0)