Skip to content

Commit e06d3b6

Browse files
committedNov 14, 2022
fix(reactivity-transform): fix $$ escape edge cases
fix #6312 close #6944
1 parent 6524805 commit e06d3b6

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
 

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

+29
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ exports[`$$ 1`] = `
2424
"
2525
`;
2626

27+
exports[`$$ with some edge cases 1`] = `
28+
"import { ref as _ref } from 'vue'
29+
30+
;( /* 2 */ count /* 2 */ )
31+
;( count /* 2 */, /**/ a )
32+
;( (count /* 2 */, /**/ a) /**/ )
33+
{
34+
a:(count,a)
35+
}
36+
;((count) + 1)
37+
;([count])
38+
; (count )
39+
console.log(((a)))
40+
;(a,b)
41+
;(((a++,b)))
42+
count = ( a++ ,b)
43+
count = ()=>(a++,b)
44+
let r1 = _ref(a, (a++,b))
45+
let r2 = { a:(a++,b),b: (a) }
46+
switch((c)){
47+
case d:
48+
;(a)
49+
;((h,f))
50+
break
51+
}
52+
((count++,(count),(count,a)))
53+
"
54+
`;
55+
2756
exports[`$computed declaration 1`] = `
2857
"import { computed as _computed } from 'vue'
2958

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

+44
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,50 @@ test('$$', () => {
305305
assertCode(code)
306306
})
307307

308+
test('$$ with some edge cases', () => {
309+
const { code } = transform(`
310+
$$( /* 2 */ count /* 2 */ )
311+
$$( count /* 2 */, /**/ a )
312+
$$( (count /* 2 */, /**/ a) /**/ )
313+
{
314+
a:$$(count,a)
315+
}
316+
$$((count) + 1)
317+
$$([count])
318+
$$ (count )
319+
console.log($$($$(a)))
320+
$$(a,b)
321+
$$($$((a++,b)))
322+
count = $$( a++ ,b)
323+
count = ()=>$$(a++,b)
324+
let r1 = $ref(a, $$(a++,b))
325+
let r2 = { a:$$(a++,b),b:$$ (a) }
326+
switch($$(c)){
327+
case d:
328+
$$(a)
329+
$$($$(h,f))
330+
break
331+
}
332+
($$(count++,$$(count),$$(count,a)))
333+
`)
334+
expect(code).toMatch(`/* 2 */ count /* 2 */`)
335+
expect(code).toMatch(`;( count /* 2 */, /**/ a )`)
336+
expect(code).toMatch(`;( (count /* 2 */, /**/ a) /**/ )`)
337+
expect(code).toMatch(`a:(count,a)`)
338+
expect(code).toMatch(`;((count) + 1)`)
339+
expect(code).toMatch(`;([count])`)
340+
expect(code).toMatch(`;(a,b)`)
341+
expect(code).toMatch(`log(((a)))`)
342+
expect(code).toMatch(`count = ( a++ ,b)`)
343+
expect(code).toMatch(`()=>(a++,b)`)
344+
expect(code).toMatch(`_ref(a, (a++,b))`)
345+
expect(code).toMatch(`{ a:(a++,b),b: (a) }`)
346+
expect(code).toMatch(`switch((c))`)
347+
expect(code).toMatch(`;((h,f))`)
348+
expect(code).toMatch(`((count++,(count),(count,a)))`)
349+
assertCode(code)
350+
})
351+
308352
test('nested scopes', () => {
309353
const { code, rootRefs } = transform(`
310354
let a = $ref(0)

‎packages/reactivity-transform/src/reactivityTransform.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,29 @@ export function transformAST(
671671
currentScope[escapeSymbol] === undefined &&
672672
callee === escapeSymbol
673673
) {
674-
s.remove(node.callee.start! + offset, node.callee.end! + offset)
675674
escapeScope = node
675+
s.remove(node.callee.start! + offset, node.callee.end! + offset)
676+
677+
if (parent?.type === 'ExpressionStatement') {
678+
// edge case where the call expression is an expression statement
679+
// if its own - prepend semicolon to avoid it being parsed as
680+
// function invocation of previous line
681+
let i =
682+
(node.leadingComments
683+
? node.leadingComments[0].start
684+
: node.start)! + offset
685+
while (i--) {
686+
const char = s.original.charAt(i)
687+
if (char === '\n') {
688+
// only insert semi if it's actually the fisrt thign after
689+
// newline
690+
s.prependRight(node.start! + offset, ';')
691+
break
692+
} else if (!/\s/.test(char)) {
693+
break
694+
}
695+
}
696+
}
676697
}
677698

678699
// TODO remove when out of experimental

0 commit comments

Comments
 (0)
Please sign in to comment.