Skip to content

Commit 3d97408

Browse files
committed
fix(reactivity-transform): edge case for comments
1 parent 276f986 commit 3d97408

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ exports[`$$ 1`] = `
2727
exports[`$$ with some edge cases 1`] = `
2828
"import { ref as _ref } from 'vue'
2929
30+
/* 2 */ count /* 2 */
31+
;( count /* 2 */, /**/ a )
32+
; (count /* 2 */, /**/ a /**/ )
3033
{
3134
a:(count,a)
3235
}

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ test('$$', () => {
307307

308308
test('$$ with some edge cases',()=>{
309309
const { code } = transform(`
310+
$$( /* 2 */ count /* 2 */ )
311+
$$( count /* 2 */, /**/ a )
312+
$$( (count /* 2 */, /**/ a) /**/ )
310313
{
311314
a:$$(count,a)
312315
}
@@ -315,22 +318,25 @@ test('$$ with some edge cases',()=>{
315318
$$ (count )
316319
console.log($$($$(a)))
317320
$$(a,b)
318-
$$($$((a,b)))
321+
$$($$((a++,b)))
319322
count = $$( a++ ,b)
320323
count = ()=>$$(a++,b)
321324
let r1 = $ref(a, $$(a++,b))
322325
let r2 = { a:$$(a++,b),b:$$ (a) }
323-
switch(c){
326+
switch($$(c)){
324327
case d:
325328
$$(a)
326329
$$($$(h,f))
327330
break
328331
}
329332
($$(count++,$$(count),$$(count,a)))
330333
`)
331-
expect(code).toMatch("a:(count,a)")
332-
expect(code).toMatch(";((count) + 1)")
333-
expect(code).toMatch(";([count])")
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])`)
334340
expect(code).toMatch(`;(a,b)`)
335341
expect(code).toMatch(`log(a)`)
336342
expect(code).toMatch(`count = ( a++ ,b)`)

packages/reactivity-transform/src/reactivityTransform.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -554,23 +554,28 @@ export function transformAST(
554554
* unwrap the code form the macro($、$$), fix #6312 and keep the ideally behavior with the RFC#369
555555
*/
556556
function unwrapMacro(node: CallExpression) {
557-
const argsLength = node.arguments.length
558557
const bracketStart = node.callee.end! + offset
559-
560558
s.remove(node.callee.start! + offset, bracketStart)
561559

562-
if (argsLength === 1) {
563-
const bracketEnd = node.arguments[argsLength - 1].end! + offset
560+
if (node.arguments.length === 1) {
561+
const bracketEnd = node.arguments.at(-1)!.end! + offset
564562
// remove brackets of macros
565-
const argsType = node.arguments[0].type
563+
const firstArg = node.arguments[0]
564+
const argsType = firstArg.type
566565
if (
567566
argsType === 'CallExpression' ||
568567
argsType === 'Identifier' ||
569568
argsType === 'ObjectExpression'
570569
) {
571-
// fix space place
572-
s.remove(node.start! + offset, node.arguments[0].start! + offset)
573-
s.remove(bracketEnd, node.end! + offset)
570+
const leading = firstArg.leadingComments?.at(0)!.start ?? Infinity
571+
const trailing = firstArg.trailingComments?.at(-1)!.end ?? 0
572+
console.log(leading, trailing)
573+
// fix space & comments place
574+
s.remove(
575+
node.start! + offset,
576+
Math.min(firstArg.start!, leading) + offset
577+
)
578+
s.remove(Math.max(bracketEnd, trailing + offset), node.end! + offset)
574579

575580
// avoid traversal of like `$$(a)`
576581
if (argsType === 'Identifier') {

0 commit comments

Comments
 (0)