Skip to content

Commit

Permalink
fix(reactivity-transform): fix $$ escape edge cases
Browse files Browse the repository at this point in the history
fix #6312
close #6944
  • Loading branch information
yyx990803 committed Nov 14, 2022
1 parent 6524805 commit e06d3b6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ exports[`$$ 1`] = `
"
`;

exports[`$$ with some edge cases 1`] = `
"import { ref as _ref } from 'vue'
;( /* 2 */ count /* 2 */ )
;( count /* 2 */, /**/ a )
;( (count /* 2 */, /**/ a) /**/ )
{
a:(count,a)
}
;((count) + 1)
;([count])
; (count )
console.log(((a)))
;(a,b)
;(((a++,b)))
count = ( a++ ,b)
count = ()=>(a++,b)
let r1 = _ref(a, (a++,b))
let r2 = { a:(a++,b),b: (a) }
switch((c)){
case d:
;(a)
;((h,f))
break
}
((count++,(count),(count,a)))
"
`;

exports[`$computed declaration 1`] = `
"import { computed as _computed } from 'vue'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,50 @@ test('$$', () => {
assertCode(code)
})

test('$$ with some edge cases', () => {
const { code } = transform(`
$$( /* 2 */ count /* 2 */ )
$$( count /* 2 */, /**/ a )
$$( (count /* 2 */, /**/ a) /**/ )
{
a:$$(count,a)
}
$$((count) + 1)
$$([count])
$$ (count )
console.log($$($$(a)))
$$(a,b)
$$($$((a++,b)))
count = $$( a++ ,b)
count = ()=>$$(a++,b)
let r1 = $ref(a, $$(a++,b))
let r2 = { a:$$(a++,b),b:$$ (a) }
switch($$(c)){
case d:
$$(a)
$$($$(h,f))
break
}
($$(count++,$$(count),$$(count,a)))
`)
expect(code).toMatch(`/* 2 */ count /* 2 */`)
expect(code).toMatch(`;( count /* 2 */, /**/ a )`)
expect(code).toMatch(`;( (count /* 2 */, /**/ a) /**/ )`)
expect(code).toMatch(`a:(count,a)`)
expect(code).toMatch(`;((count) + 1)`)
expect(code).toMatch(`;([count])`)
expect(code).toMatch(`;(a,b)`)
expect(code).toMatch(`log(((a)))`)
expect(code).toMatch(`count = ( a++ ,b)`)
expect(code).toMatch(`()=>(a++,b)`)
expect(code).toMatch(`_ref(a, (a++,b))`)
expect(code).toMatch(`{ a:(a++,b),b: (a) }`)
expect(code).toMatch(`switch((c))`)
expect(code).toMatch(`;((h,f))`)
expect(code).toMatch(`((count++,(count),(count,a)))`)
assertCode(code)
})

test('nested scopes', () => {
const { code, rootRefs } = transform(`
let a = $ref(0)
Expand Down
23 changes: 22 additions & 1 deletion packages/reactivity-transform/src/reactivityTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,29 @@ export function transformAST(
currentScope[escapeSymbol] === undefined &&
callee === escapeSymbol
) {
s.remove(node.callee.start! + offset, node.callee.end! + offset)
escapeScope = node
s.remove(node.callee.start! + offset, node.callee.end! + offset)

if (parent?.type === 'ExpressionStatement') {
// edge case where the call expression is an expression statement
// if its own - prepend semicolon to avoid it being parsed as
// function invocation of previous line
let i =
(node.leadingComments
? node.leadingComments[0].start
: node.start)! + offset
while (i--) {
const char = s.original.charAt(i)
if (char === '\n') {
// only insert semi if it's actually the fisrt thign after
// newline
s.prependRight(node.start! + offset, ';')
break
} else if (!/\s/.test(char)) {
break
}
}
}
}

// TODO remove when out of experimental
Expand Down

0 comments on commit e06d3b6

Please sign in to comment.