Skip to content

Commit 33cf6c8

Browse files
authored
fix(ref-transform): should transform $ref when used with generic arguments (#4446)
fix #4442
1 parent a6e6253 commit 33cf6c8

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ return { foo, a, b, c, d }
4242
}"
4343
`;
4444

45+
exports[`sfc ref transform usage /w typescript 1`] = `
46+
"import { ref as _ref, defineComponent as _defineComponent } from 'vue'
47+
48+
export default _defineComponent({
49+
setup(__props, { expose }) {
50+
expose()
51+
52+
let msg = _ref<string | number>('foo');
53+
let bar = _ref <string | number>('bar');
54+
55+
return { msg, bar }
56+
}
57+
58+
})"
59+
`;
60+
4561
exports[`sfc ref transform usage in normal <script> 1`] = `
4662
"import { ref as _ref } from 'vue'
4763

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

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ describe('sfc ref transform', () => {
9999
assertCode(content)
100100
})
101101

102+
test('usage /w typescript', () => {
103+
const { content } = compileWithRefTransform(`
104+
<script setup lang="ts">
105+
let msg = $ref<string | number>('foo');
106+
let bar = $ref <string | number>('bar');
107+
</script>
108+
`)
109+
expect(content).toMatch(`import { ref as _ref`)
110+
expect(content).toMatch(`let msg = _ref<string | number>('foo')`)
111+
expect(content).toMatch(`let bar = _ref <string | number>('bar')`)
112+
assertCode(content)
113+
})
114+
102115
test('usage with normal <script> + <script setup>', () => {
103116
const { content, bindings } = compileWithRefTransform(`<script>
104117
let a = $ref(0)

packages/ref-transform/src/refTransform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { babelParserDefaultPlugins } from '@vue/shared'
2525
const TO_VAR_SYMBOL = '$'
2626
const TO_REF_SYMBOL = '$$'
2727
const shorthands = ['ref', 'computed', 'shallowRef']
28-
const transformCheckRE = /[^\w]\$(?:\$|ref|computed|shallowRef)?\(/
28+
const transformCheckRE = /[^\w]\$(?:\$|ref|computed|shallowRef)?\s*(\(|\<)/
2929

3030
export function shouldTransform(src: string): boolean {
3131
return transformCheckRE.test(src)

0 commit comments

Comments
 (0)