Skip to content

Commit a370e80

Browse files
committed
perf(compiler-sfc): infer ref binding type for more built-in methods
1 parent 433a58c commit a370e80

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -1520,4 +1520,19 @@ describe('SFC genDefaultAs', () => {
15201520
)
15211521
assertCode(content)
15221522
})
1523+
1524+
test('binding type for edge cases', () => {
1525+
const { bindings } = compile(
1526+
`<script setup lang="ts">
1527+
import { toRef } from 'vue'
1528+
const props = defineProps<{foo: string}>()
1529+
const foo = toRef(() => props.foo)
1530+
</script>`
1531+
)
1532+
expect(bindings).toStrictEqual({
1533+
toRef: BindingTypes.SETUP_CONST,
1534+
props: BindingTypes.SETUP_REACTIVE_CONST,
1535+
foo: BindingTypes.SETUP_REF
1536+
})
1537+
})
15231538
})

packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ const props = defineProps({ foo: String })
581581
</script>
582582
`)
583583
expect(bindings).toStrictEqual({
584-
bar: BindingTypes.SETUP_MAYBE_REF,
584+
bar: BindingTypes.SETUP_REF,
585585
computed: BindingTypes.SETUP_CONST
586586
})
587587
})

packages/compiler-sfc/src/compileScript.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,16 @@ function walkDeclaration(
10951095
: BindingTypes.SETUP_CONST
10961096
} else if (isConst) {
10971097
if (
1098-
isCallOf(init, userImportAliases['ref']) ||
1099-
isCallOf(init, DEFINE_MODEL)
1098+
isCallOf(
1099+
init,
1100+
m =>
1101+
m === userImportAliases['ref'] ||
1102+
m === userImportAliases['computed'] ||
1103+
m === userImportAliases['shallowRef'] ||
1104+
m === userImportAliases['customRef'] ||
1105+
m === userImportAliases['toRef'] ||
1106+
m === DEFINE_MODEL
1107+
)
11001108
) {
11011109
bindingType = BindingTypes.SETUP_REF
11021110
} else {

0 commit comments

Comments
 (0)