Skip to content

Commit 562bddb

Browse files
committed
feat(sfc): (experimental) new ref sugar
1 parent 27104ea commit 562bddb

File tree

3 files changed

+294
-171
lines changed

3 files changed

+294
-171
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap

+73-42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`<script setup> ref sugar $computed declaration 1`] = `
4+
"import { computed as _computed } from 'vue'
5+
6+
export default {
7+
setup(__props, { expose }) {
8+
expose()
9+
10+
const a = _computed(() => 1)
11+
12+
return { a }
13+
}
14+
15+
}"
16+
`;
17+
18+
exports[`<script setup> ref sugar $raw 1`] = `
19+
"import { ref as _ref } from 'vue'
20+
21+
export default {
22+
setup(__props, { expose }) {
23+
expose()
24+
25+
let a = _ref(1)
26+
const b = (a)
27+
const c = ({ a })
28+
callExternal((a))
29+
30+
return { a, b, c }
31+
}
32+
33+
}"
34+
`;
35+
36+
exports[`<script setup> ref sugar $ref declarations 1`] = `
37+
"import { ref as _ref } from 'vue'
38+
39+
export default {
40+
setup(__props, { expose }) {
41+
expose()
42+
43+
let foo = _ref()
44+
let a = _ref(1)
45+
let b = _ref({
46+
count: 0
47+
})
48+
let c = () => {}
49+
let d
50+
51+
return { foo, a, b, c, d }
52+
}
53+
54+
}"
55+
`;
56+
357
exports[`<script setup> ref sugar accessing ref binding 1`] = `
458
"import { ref as _ref } from 'vue'
559
660
export default {
761
setup(__props, { expose }) {
862
expose()
963
10-
const a = _ref(1)
64+
let a = _ref(1)
1165
console.log(a.value)
1266
function get() {
1367
return a.value + 1
@@ -26,7 +80,7 @@ export default {
2680
setup(__props, { expose }) {
2781
expose()
2882
29-
const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()
83+
let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())
3084
const a = _ref(__a);
3185
const b = _ref(__b);
3286
const c = _ref(__c);
@@ -38,35 +92,29 @@ return { n, a, b, c }
3892
}"
3993
`;
4094
41-
exports[`<script setup> ref sugar convert ref declarations 1`] = `
42-
"import { ref as _ref } from 'vue'
95+
exports[`<script setup> ref sugar mixing $ref & $computed declarations 1`] = `
96+
"import { ref as _ref, computed as _computed } from 'vue'
4397
4498
export default {
4599
setup(__props, { expose }) {
46100
expose()
47101
48-
const foo = _ref()
49-
const a = _ref(1)
50-
const b = _ref({
51-
count: 0
52-
})
53-
let c = () => {}
54-
let d
102+
let a = _ref(1), b = _computed(() => a.value + 1)
55103
56-
return { foo, a, b, c, d }
104+
return { a, b }
57105
}
58106
59107
}"
60108
`;
61109
62-
exports[`<script setup> ref sugar multi ref declarations 1`] = `
110+
exports[`<script setup> ref sugar multi $ref declarations 1`] = `
63111
"import { ref as _ref } from 'vue'
64112
65113
export default {
66114
setup(__props, { expose }) {
67115
expose()
68116
69-
const a = _ref(1), b = _ref(2), c = _ref({
117+
let a = _ref(1), b = _ref(2), c = _ref({
70118
count: 0
71119
})
72120
@@ -83,8 +131,8 @@ export default {
83131
setup(__props, { expose }) {
84132
expose()
85133
86-
const a = _ref(1)
87-
const b = _ref({ count: 0 })
134+
let a = _ref(1)
135+
let b = _ref({ count: 0 })
88136
function inc() {
89137
a.value++
90138
a.value = a.value + 1
@@ -107,9 +155,9 @@ export default {
107155
setup(__props, { expose }) {
108156
expose()
109157
110-
const [{ a: { b: __b }}] = useFoo()
158+
let [{ a: { b: __b }}] = (useFoo())
111159
const b = _ref(__b);
112-
const { c: [__d, __e] } = useBar()
160+
let { c: [__d, __e] } = (useBar())
113161
const d = _ref(__d);
114162
const e = _ref(__e);
115163
console.log(b.value, d.value, e.value)
@@ -127,13 +175,13 @@ export default {
127175
setup(__props, { expose }) {
128176
expose()
129177
130-
const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()
178+
let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())
131179
const a = _ref(__a);
132180
const c = _ref(__c);
133181
const d = _ref(__d);
134182
const f = _ref(__f);
135183
const g = _ref(__g);
136-
const { foo: __foo } = useSomthing(() => 1);
184+
let { foo: __foo } = (useSomthing(() => 1));
137185
const foo = _ref(__foo);
138186
console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)
139187
@@ -143,40 +191,23 @@ return { n, a, c, d, f, g, foo }
143191
}"
144192
`;
145193
146-
exports[`<script setup> ref sugar should not convert non ref labels 1`] = `
147-
"export default {
148-
setup(__props, { expose }) {
149-
expose()
150-
151-
foo: a = 1, b = 2, c = {
152-
count: 0
153-
}
154-
155-
return { }
156-
}
157-
158-
}"
159-
`;
160-
161194
exports[`<script setup> ref sugar should not rewrite scope variable 1`] = `
162195
"import { ref as _ref } from 'vue'
163196
164197
export default {
165198
setup(__props, { expose }) {
166199
expose()
167200
168-
const a = _ref(1)
169-
const b = _ref(1)
170-
const d = _ref(1)
201+
let a = _ref(1)
202+
let b = _ref(1)
203+
let d = _ref(1)
171204
const e = 1
172205
function test() {
173206
const a = 2
174207
console.log(a)
175208
console.log(b.value)
176209
let c = { c: 3 }
177210
console.log(c)
178-
let $d
179-
console.log($d)
180211
console.log(d.value)
181212
console.log(e)
182213
}
@@ -200,7 +231,7 @@ export default _defineComponent({
200231
201232
const props = __props
202233
203-
const ids = _ref([])
234+
let ids = _ref([])
204235
205236
return { props, ids }
206237
}
@@ -215,7 +246,7 @@ export default {
215246
setup(__props, { expose }) {
216247
expose()
217248
218-
const a = _ref(1)
249+
let a = _ref(1)
219250
const b = { a: a.value }
220251
function test() {
221252
const { a } = b

0 commit comments

Comments
 (0)