Skip to content

Commit bf2589b

Browse files
committed
refactor(compiler-sfc): use shallowRef for ref sugar destructure
1 parent bc7dd93 commit bf2589b

File tree

6 files changed

+35
-35
lines changed

6 files changed

+35
-35
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ return { a, get }
7474
`;
7575
7676
exports[`<script setup> ref sugar array destructure 1`] = `
77-
"import { ref as _ref } from 'vue'
77+
"import { ref as _ref, shallowRef as _shallowRef } from 'vue'
7878
7979
export default {
8080
setup(__props, { expose }) {
8181
expose()
8282
8383
let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())
84-
const a = _ref(__a);
85-
const b = _ref(__b);
86-
const c = _ref(__c);
84+
const a = _shallowRef(__a);
85+
const b = _shallowRef(__b);
86+
const c = _shallowRef(__c);
8787
console.log(n.value, a.value, b.value, c.value)
8888
8989
return { n, a, b, c }
@@ -149,17 +149,17 @@ return { a, b, inc }
149149
`;
150150
151151
exports[`<script setup> ref sugar nested destructure 1`] = `
152-
"import { ref as _ref } from 'vue'
152+
"import { shallowRef as _shallowRef } from 'vue'
153153
154154
export default {
155155
setup(__props, { expose }) {
156156
expose()
157157
158158
let [{ a: { b: __b }}] = (useFoo())
159-
const b = _ref(__b);
159+
const b = _shallowRef(__b);
160160
let { c: [__d, __e] } = (useBar())
161-
const d = _ref(__d);
162-
const e = _ref(__e);
161+
const d = _shallowRef(__d);
162+
const e = _shallowRef(__e);
163163
console.log(b.value, d.value, e.value)
164164
165165
return { b, d, e }
@@ -169,20 +169,20 @@ return { b, d, e }
169169
`;
170170
171171
exports[`<script setup> ref sugar object destructure 1`] = `
172-
"import { ref as _ref } from 'vue'
172+
"import { ref as _ref, shallowRef as _shallowRef } from 'vue'
173173
174174
export default {
175175
setup(__props, { expose }) {
176176
expose()
177177
178178
let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())
179-
const a = _ref(__a);
180-
const c = _ref(__c);
181-
const d = _ref(__d);
182-
const f = _ref(__f);
183-
const g = _ref(__g);
179+
const a = _shallowRef(__a);
180+
const c = _shallowRef(__c);
181+
const d = _shallowRef(__d);
182+
const f = _shallowRef(__f);
183+
const g = _shallowRef(__g);
184184
let { foo: __foo } = (useSomthing(() => 1));
185-
const foo = _ref(__foo);
185+
const foo = _shallowRef(__foo);
186186
console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)
187187
188188
return { n, a, c, d, f, g, foo }

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ describe('<script setup> ref sugar', () => {
184184
`let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())`
185185
)
186186
expect(content).toMatch(`let { foo: __foo } = (useSomthing(() => 1))`)
187-
expect(content).toMatch(`\nconst a = _ref(__a);`)
188-
expect(content).not.toMatch(`\nconst b = _ref(__b);`)
189-
expect(content).toMatch(`\nconst c = _ref(__c);`)
190-
expect(content).toMatch(`\nconst d = _ref(__d);`)
191-
expect(content).not.toMatch(`\nconst e = _ref(__e);`)
192-
expect(content).toMatch(`\nconst f = _ref(__f);`)
193-
expect(content).toMatch(`\nconst g = _ref(__g);`)
194-
expect(content).toMatch(`\nconst foo = _ref(__foo);`)
187+
expect(content).toMatch(`\nconst a = _shallowRef(__a);`)
188+
expect(content).not.toMatch(`\nconst b = _shallowRef(__b);`)
189+
expect(content).toMatch(`\nconst c = _shallowRef(__c);`)
190+
expect(content).toMatch(`\nconst d = _shallowRef(__d);`)
191+
expect(content).not.toMatch(`\nconst e = _shallowRef(__e);`)
192+
expect(content).toMatch(`\nconst f = _shallowRef(__f);`)
193+
expect(content).toMatch(`\nconst g = _shallowRef(__g);`)
194+
expect(content).toMatch(`\nconst foo = _shallowRef(__foo);`)
195195
expect(content).toMatch(
196196
`console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)`
197197
)
@@ -216,9 +216,9 @@ describe('<script setup> ref sugar', () => {
216216
expect(content).toMatch(
217217
`let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())`
218218
)
219-
expect(content).toMatch(`\nconst a = _ref(__a);`)
220-
expect(content).toMatch(`\nconst b = _ref(__b);`)
221-
expect(content).toMatch(`\nconst c = _ref(__c);`)
219+
expect(content).toMatch(`\nconst a = _shallowRef(__a);`)
220+
expect(content).toMatch(`\nconst b = _shallowRef(__b);`)
221+
expect(content).toMatch(`\nconst c = _shallowRef(__c);`)
222222
expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
223223
expect(content).toMatch(`return { n, a, b, c }`)
224224
expect(bindings).toStrictEqual({
@@ -238,11 +238,11 @@ describe('<script setup> ref sugar', () => {
238238
</script>`)
239239
expect(content).toMatch(`let [{ a: { b: __b }}] = (useFoo())`)
240240
expect(content).toMatch(`let { c: [__d, __e] } = (useBar())`)
241-
expect(content).not.toMatch(`\nconst a = _ref(__a);`)
242-
expect(content).not.toMatch(`\nconst c = _ref(__c);`)
243-
expect(content).toMatch(`\nconst b = _ref(__b);`)
244-
expect(content).toMatch(`\nconst d = _ref(__d);`)
245-
expect(content).toMatch(`\nconst e = _ref(__e);`)
241+
expect(content).not.toMatch(`\nconst a = _shallowRef(__a);`)
242+
expect(content).not.toMatch(`\nconst c = _shallowRef(__c);`)
243+
expect(content).toMatch(`\nconst b = _shallowRef(__b);`)
244+
expect(content).toMatch(`\nconst d = _shallowRef(__d);`)
245+
expect(content).toMatch(`\nconst e = _shallowRef(__e);`)
246246
expect(content).toMatch(`return { b, d, e }`)
247247
expect(bindings).toStrictEqual({
248248
b: BindingTypes.SETUP_REF,

packages/compiler-sfc/src/compileScript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ export function compileScript(
643643
// append binding declarations after the parent statement
644644
s.appendLeft(
645645
statement.end! + startOffset,
646-
`\nconst ${nameId.name} = ${helper('ref')}(__${nameId.name});`
646+
`\nconst ${nameId.name} = ${helper('shallowRef')}(__${nameId.name});`
647647
)
648648
}
649649
}
@@ -677,7 +677,7 @@ export function compileScript(
677677
// append binding declarations after the parent statement
678678
s.appendLeft(
679679
statement.end! + startOffset,
680-
`\nconst ${nameId.name} = ${helper('ref')}(__${nameId.name});`
680+
`\nconst ${nameId.name} = ${helper('shallowRef')}(__${nameId.name});`
681681
)
682682
}
683683
}

packages/runtime-core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,6 @@ export const compatUtils = (
352352
__COMPAT__ ? _compatUtils : null
353353
) as typeof _compatUtils
354354

355-
// Ref macros ------------------------------------------------------------------
355+
// Ref sugar macros ------------------------------------------------------------
356356
// for dts generation only
357-
export { $ref, $computed, $raw, $fromRefs } from './helpers/refMacros'
357+
export { $ref, $computed, $raw, $fromRefs } from './helpers/refSugar'
File renamed without changes.

0 commit comments

Comments
 (0)