Skip to content

Commit

Permalink
fix(refSugar): should transform ref with typed declaration
Browse files Browse the repository at this point in the history
chore: improve RE

test: improve test case
  • Loading branch information
edison1105 committed Aug 26, 2021
1 parent 72d2b26 commit b4189ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Expand Up @@ -42,6 +42,21 @@ return { foo, a, b, c, d }
}"
`;

exports[`sfc ref transform usage /w typescript 1`] = `
"import { ref as _ref, defineComponent as _defineComponent } from 'vue'
export default _defineComponent({
setup(__props, { expose }) {
expose()
let msg = _ref<string | number>('foo');
return { msg }
}
})"
`;

exports[`sfc ref transform usage in normal <script> 1`] = `
"import { ref as _ref } from 'vue'
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts
Expand Up @@ -99,6 +99,17 @@ describe('sfc ref transform', () => {
assertCode(content)
})

test('usage /w typescript', () => {
const { content } = compileWithRefTransform(`
<script setup lang="ts">
let msg = $ref<string | number>('foo');
</script>
`)
expect(content).toMatch(`import { ref as _ref`)
expect(content).toMatch(`let msg = _ref<string | number>('foo')`)
assertCode(content)
})

test('usage with normal <script> + <script setup>', () => {
const { content, bindings } = compileWithRefTransform(`<script>
let a = $ref(0)
Expand Down
2 changes: 1 addition & 1 deletion packages/ref-transform/src/refTransform.ts
Expand Up @@ -25,7 +25,7 @@ import { babelParserDefaultPlugins } from '@vue/shared'
const TO_VAR_SYMBOL = '$'
const TO_REF_SYMBOL = '$$'
const shorthands = ['ref', 'computed', 'shallowRef']
const transformCheckRE = /[^\w]\$(?:\$|ref|computed|shallowRef)?\(/
const transformCheckRE = /[^\w]\$(?:\$|ref|computed|shallowRef)?(\(|\<)/

export function shouldTransform(src: string): boolean {
return transformCheckRE.test(src)
Expand Down

0 comments on commit b4189ed

Please sign in to comment.