Skip to content

Commit b773c2b

Browse files
committed
fix(reactivity-transform): ensure macro comply with the original semantics
1 parent b0b74a1 commit b773c2b

7 files changed

+74
-62
lines changed

Diff for: packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap

+21-21
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export default {
99
const __props_bar = _toRef(__props, 'bar')
1010
const __props_foo = _toRef(__props, 'foo')
1111
12-
13-
console.log((__props_foo))
14-
console.log((__props_bar))
15-
({ foo: __props_foo, baz: __props_bar })
16-
12+
13+
console.log(__props_foo)
14+
console.log(__props_bar)
15+
{ foo: __props_foo, baz: __props_bar }
16+
1717
return () => {}
1818
}
1919
@@ -28,10 +28,10 @@ export default {
2828
props: ['foo'],
2929
setup(__props) {
3030
31-
31+
3232
let x = foo
3333
let y = __props.foo
34-
34+
3535
return (_ctx, _cache) => {
3636
return _toDisplayString(__props.foo + __props.foo)
3737
}
@@ -48,9 +48,9 @@ export default {
4848
props: ['foo'],
4949
setup(__props) {
5050
51-
51+
5252
console.log(__props.foo)
53-
53+
5454
return (_ctx, _cache) => {
5555
return _toDisplayString(__props.foo)
5656
}
@@ -69,8 +69,8 @@ export default {
6969
}),
7070
setup(__props) {
7171
72-
73-
72+
73+
7474
return () => {}
7575
}
7676
@@ -87,8 +87,8 @@ export default /*#__PURE__*/_defineComponent({
8787
},
8888
setup(__props: any) {
8989
90-
91-
90+
91+
9292
return () => {}
9393
}
9494
@@ -109,8 +109,8 @@ export default /*#__PURE__*/_defineComponent({
109109
},
110110
setup(__props: any) {
111111
112-
113-
112+
113+
114114
return () => {}
115115
}
116116
@@ -122,12 +122,12 @@ exports[`sfc props transform nested scope 1`] = `
122122
props: ['foo', 'bar'],
123123
setup(__props) {
124124
125-
125+
126126
function test(foo) {
127127
console.log(foo)
128128
console.log(__props.bar)
129129
}
130-
130+
131131
return () => {}
132132
}
133133
@@ -142,9 +142,9 @@ export default {
142142
props: { 'foo.bar': Function },
143143
setup(__props) {
144144
145-
145+
146146
let x = __props[\\"foo.bar\\"]
147-
147+
148148
return (_ctx, _cache) => {
149149
return _toDisplayString(__props[\\"foo.bar\\"])
150150
}
@@ -162,8 +162,8 @@ export default {
162162
163163
const rest = _createPropsRestProxy(__props, [\\"foo\\",\\"bar\\"])
164164
165-
166-
165+
166+
167167
return () => {}
168168
}
169169

Diff for: packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export default {
77
setup(__props, { expose }) {
88
expose();
99
10-
let foo = (ref())
11-
let a = (ref(1))
12-
let b = (shallowRef({
10+
let foo = ref()
11+
let a = ref(1)
12+
let b = shallowRef({
1313
count: 0
14-
}))
14+
})
1515
let c = () => {}
1616
let d
1717
@@ -65,7 +65,7 @@ exports[`sfc ref transform usage in normal <script> 1`] = `
6565
setup() {
6666
let count = _ref(0)
6767
const inc = () => count.value++
68-
return ({ count })
68+
return { count }
6969
}
7070
}
7171
"

Diff for: packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ describe('sfc props transform', () => {
178178
`)
179179
expect(content).toMatch(`const __props_foo = _toRef(__props, 'foo')`)
180180
expect(content).toMatch(`const __props_bar = _toRef(__props, 'bar')`)
181-
expect(content).toMatch(`console.log((__props_foo))`)
182-
expect(content).toMatch(`console.log((__props_bar))`)
183-
expect(content).toMatch(`({ foo: __props_foo, baz: __props_bar })`)
181+
expect(content).toMatch(`console.log(__props_foo)`)
182+
expect(content).toMatch(`console.log(__props_bar)`)
183+
expect(content).toMatch(`{ foo: __props_foo, baz: __props_bar }`)
184184
assertCode(content)
185185
})
186186

Diff for: packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ describe('sfc ref transform', () => {
2222
expect(content).not.toMatch(`$(ref())`)
2323
expect(content).not.toMatch(`$(ref(1))`)
2424
expect(content).not.toMatch(`$(shallowRef({`)
25-
expect(content).toMatch(`let foo = (ref())`)
26-
expect(content).toMatch(`let a = (ref(1))`)
25+
expect(content).toMatch(`let foo = ref()`)
26+
expect(content).toMatch(`let a = ref(1)`)
2727
expect(content).toMatch(`
28-
let b = (shallowRef({
28+
let b = shallowRef({
2929
count: 0
30-
}))
30+
})
3131
`)
3232
// normal declarations left untouched
3333
expect(content).toMatch(`let c = () => {}`)
@@ -95,7 +95,7 @@ describe('sfc ref transform', () => {
9595
expect(content).toMatch(`import { ref as _ref } from 'vue'`)
9696
expect(content).toMatch(`let count = _ref(0)`)
9797
expect(content).toMatch(`count.value++`)
98-
expect(content).toMatch(`return ({ count })`)
98+
expect(content).toMatch(`return { count }`)
9999
assertCode(content)
100100
})
101101

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
exports[`$ unwrapping 1`] = `
44
"
55
import { ref, shallowRef } from 'vue'
6-
let foo = (ref())
7-
export let a = (ref(1))
8-
let b = (shallowRef({
6+
let foo = ref()
7+
export let a = ref(1)
8+
let b = shallowRef({
99
count: 0
10-
}))
10+
})
1111
let c = () => {}
1212
let d
13-
label: var e = (ref())
13+
label: var e = ref()
1414
"
1515
`;
1616

1717
exports[`$$ 1`] = `
1818
"import { ref as _ref } from 'vue'
1919
2020
let a = _ref(1)
21-
const b = (a)
22-
const c = ({ a })
23-
callExternal((a))
21+
const b = a
22+
const c = { a }
23+
callExternal(a)
2424
"
2525
`;
2626

@@ -59,7 +59,7 @@ exports[`accessing ref binding 1`] = `
5959
exports[`array destructure 1`] = `
6060
"import { ref as _ref, toRef as _toRef } from 'vue'
6161
62-
let n = _ref(1), __$temp_1 = (useFoo()),
62+
let n = _ref(1), __$temp_1 = useFoo(),
6363
a = _toRef(__$temp_1, 0),
6464
b = _toRef(__$temp_1, 1, 1)
6565
console.log(n.value, a.value, b.value)
@@ -88,7 +88,7 @@ exports[`macro import alias and removal 1`] = `
8888
8989
9090
let a = _ref(1)
91-
const __$temp_1 = (useMouse()),
91+
const __$temp_1 = useMouse(),
9292
x = _toRef(__$temp_1, 'x'),
9393
y = _toRef(__$temp_1, 'y')
9494
"
@@ -129,9 +129,9 @@ exports[`mutating ref binding 1`] = `
129129
exports[`nested destructure 1`] = `
130130
"import { toRef as _toRef } from 'vue'
131131
132-
let __$temp_1 = (useFoo()),
132+
let __$temp_1 = useFoo(),
133133
b = _toRef(__$temp_1[0].a, 'b')
134-
let __$temp_2 = (useBar()),
134+
let __$temp_2 = useBar(),
135135
d = _toRef(__$temp_2.c, 0),
136136
e = _toRef(__$temp_2.c, 1)
137137
console.log(b.value, d.value, e.value)
@@ -170,21 +170,21 @@ exports[`nested scopes 1`] = `
170170
a.value++ // if block a
171171
}
172172
173-
return ({ a, b, c, d })
173+
return { a, b, c, d }
174174
}
175175
"
176176
`;
177177
178178
exports[`object destructure 1`] = `
179179
"import { ref as _ref, toRef as _toRef } from 'vue'
180180
181-
let n = _ref(1), __$temp_1 = (useFoo()),
181+
let n = _ref(1), __$temp_1 = useFoo(),
182182
a = _toRef(__$temp_1, 'a'),
183183
c = _toRef(__$temp_1, 'b'),
184184
d = _toRef(__$temp_1, 'd', 1),
185185
f = _toRef(__$temp_1, 'e', 2),
186186
h = _toRef(__$temp_1, g)
187-
let __$temp_2 = (useSomething(() => 1)),
187+
let __$temp_2 = useSomething(() => 1),
188188
foo = _toRef(__$temp_2, 'foo');
189189
console.log(n.value, a.value, c.value, d.value, f.value, h.value, foo.value)
190190
"
@@ -193,7 +193,7 @@ exports[`object destructure 1`] = `
193193
exports[`object destructure w/ mid-path default values 1`] = `
194194
"import { toRef as _toRef } from 'vue'
195195
196-
const __$temp_1 = (useFoo()),
196+
const __$temp_1 = useFoo(),
197197
b = _toRef((__$temp_1.a || { b: 123 }), 'b')
198198
console.log(b.value)
199199
"

Diff for: packages/reactivity-transform/__tests__/reactivityTransform.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ test('$ unwrapping', () => {
3030
expect(code).not.toMatch(`$(ref())`)
3131
expect(code).not.toMatch(`$(ref(1))`)
3232
expect(code).not.toMatch(`$(shallowRef({`)
33-
expect(code).toMatch(`let foo = (ref())`)
34-
expect(code).toMatch(`export let a = (ref(1))`)
33+
expect(code).toMatch(`let foo = ref()`)
34+
expect(code).toMatch(`export let a = ref(1)`)
3535
expect(code).toMatch(`
36-
let b = (shallowRef({
36+
let b = shallowRef({
3737
count: 0
38-
}))
38+
})
3939
`)
4040
// normal declarations left untouched
4141
expect(code).toMatch(`let c = () => {}`)
4242
expect(code).toMatch(`let d`)
43-
expect(code).toMatch(`label: var e = (ref())`)
43+
expect(code).toMatch(`label: var e = ref()`)
4444
expect(rootRefs).toStrictEqual(['foo', 'a', 'b', 'e'])
4545
assertCode(code)
4646
})
@@ -299,9 +299,9 @@ test('$$', () => {
299299
const c = $$({ a })
300300
callExternal($$(a))
301301
`)
302-
expect(code).toMatch(`const b = (a)`)
303-
expect(code).toMatch(`const c = ({ a })`)
304-
expect(code).toMatch(`callExternal((a))`)
302+
expect(code).toMatch(`const b = a`)
303+
expect(code).toMatch(`const c = { a }`)
304+
expect(code).toMatch(`callExternal(a)`)
305305
assertCode(code)
306306
})
307307

@@ -358,7 +358,7 @@ test('nested scopes', () => {
358358
// inner bar shadowed by function declaration
359359
expect(code).toMatch(`bar() // inner bar`)
360360

361-
expect(code).toMatch(`return ({ a, b, c, d })`)
361+
expect(code).toMatch(`return { a, b, c, d }`)
362362
assertCode(code)
363363
})
364364

@@ -412,7 +412,7 @@ test('macro import alias and removal', () => {
412412
// should remove imports
413413
expect(code).not.toMatch(`from 'vue/macros'`)
414414
expect(code).toMatch(`let a = _ref(1)`)
415-
expect(code).toMatch(`const __$temp_1 = (useMouse())`)
415+
expect(code).toMatch(`const __$temp_1 = useMouse()`)
416416
assertCode(code)
417417
})
418418

Diff for: packages/reactivity-transform/src/reactivityTransform.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export function transformAST(
284284
if (method === convertSymbol) {
285285
// $
286286
// remove macro
287-
s.remove(call.callee.start! + offset, call.callee.end! + offset)
287+
unwrapMacro(call)
288288
if (id.type === 'Identifier') {
289289
// single variable
290290
registerRefBinding(id)
@@ -550,6 +550,18 @@ export function transformAST(
550550
}
551551
}
552552

553+
/**
554+
* unwrap the code form the macro($、$$), fix #6312 and keep the ideally behavior with the RFC#369
555+
*/
556+
function unwrapMacro(node: CallExpression) {
557+
if (node.arguments.length) {
558+
// remove macro $( and $$(
559+
s.remove(node.callee.start! + offset, node.arguments[0].start! + offset)
560+
// remove the end of macro
561+
s.remove(node.arguments[0].end! + offset, node.end! + offset)
562+
}
563+
}
564+
553565
// check root scope first
554566
walkScope(ast, true)
555567
;(walk as any)(ast, {
@@ -623,7 +635,7 @@ export function transformAST(
623635
}
624636

625637
if (callee === escapeSymbol) {
626-
s.remove(node.callee.start! + offset, node.callee.end! + offset)
638+
unwrapMacro(node)
627639
escapeScope = node
628640
}
629641

0 commit comments

Comments
 (0)